Java &引用;BindException:地址已在使用中“;在春季使用ApacheFTPServer
我按照如下方式配置了Apache FtpServer:Java &引用;BindException:地址已在使用中“;在春季使用ApacheFTPServer,java,spring,spring-boot,Java,Spring,Spring Boot,我按照如下方式配置了Apache FtpServer: @Component public class FtpDummyServer { private FtpServer server; @PostConstruct public void init() throws FtpException { ..some initialization this.server = serverFactory.createServer(); this.server.start()
@Component
public class FtpDummyServer {
private FtpServer server;
@PostConstruct
public void init() throws FtpException {
..some initialization
this.server = serverFactory.createServer();
this.server.start();
}
@PreDestroy
public void stop() {
this.server.stop();
}
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MainApplication.class)
@WebIntegrationTest
public class ApplicationTestX {
...
}
请注意,服务器是在@PostConstruct中自动启动的。我在SpringBoot中配置了不同的测试,如下所示:
@Component
public class FtpDummyServer {
private FtpServer server;
@PostConstruct
public void init() throws FtpException {
..some initialization
this.server = serverFactory.createServer();
this.server.start();
}
@PreDestroy
public void stop() {
this.server.stop();
}
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MainApplication.class)
@WebIntegrationTest
public class ApplicationTestX {
...
}
当我单独运行测试时,它们成功了。但是,当我将它们一起运行时,会得到一个
java.net.BindException:Address ready-in-use:bind
。如何避免这种情况?如果您查看异常堆栈跟踪,您将看到ftp服务器尝试使用的地址。类Smth
原因:org.apache.ftpserver.FtpServerConfigurationException:无法绑定到地址0.0.0/0.0.0.0:21,请检查配置
这可能是因为:
FtpServerFactory serverFactory = new FtpServerFactory();
ListenerFactory factory = new ListenerFactory();
factory.setPort(2222); //use alternative port instead of default 21
serverFactory.addListener("default", factory.createListener());
server = serverFactory.createServer();
server.start();
使用netstat找出保存地址的进程id。如果查看异常堆栈跟踪,您将看到ftp服务器尝试使用的地址。类Smth 原因:org.apache.ftpserver.FtpServerConfigurationException:无法绑定到地址0.0.0/0.0.0.0:21,请检查配置 这可能是因为:
FtpServerFactory serverFactory = new FtpServerFactory();
ListenerFactory factory = new ListenerFactory();
factory.setPort(2222); //use alternative port instead of default 21
serverFactory.addListener("default", factory.createListener());
server = serverFactory.createServer();
server.start();
使用netstat计算包含地址的进程id。对于每个测试,都会创建一个FtpDummyServer。然而,@PreDestroy从未被调用@DirtiesContext解决了这个问题。对于每个测试,都会创建一个FtpDummyServer。然而,@PreDestroy从未被调用@DirtiesContext解决了这个问题。将@DirtiesContext与@TestExecutionListeners结合使用是可行的,但不要忘记将您的上下文与默认上下文合并。 例如:
看看这里:使用@DirtiesContext和@TestExecutionListeners可以工作,但不要忘记将您的上下文与默认上下文合并。 例如:
看看那里:您的FtpDummyServer的范围是什么?我认为您在测试中多次注入FtpDummyServer,如果是这样,请尝试为所有测试用例共享一个FtpDummyServer。这是每个数据故障的单例。您可以发布您的测试用例吗?您的FtpDummyServer的范围是什么?我认为您在测试中多次注入FtpDummyServer,如果是这样,请尝试为所有测试用例共享一个FtpDummyServer。这是每个数据故障的单例。您可以发布您的测试用例吗?对于每个测试,都会创建一个FtpDummyServer。然而,
@PreDestroy
从未被调用@DirtiesContext
解决了这个问题。@RenéWinkler我不知怎么错过了它是test。对于每个测试,都会创建一个FtpDummyServer。然而,@PreDestroy
从未被调用@DirtiesContext
解决了这个问题。@RenéWinkler我不知怎么错过了测试