我收到 java.net.BindException。我正在使用 Spring 自动装配所有 bean,所以我不认为同一进程的两个线程正在运行。我也为所有进程使用不同的端口。
这个错误的可能原因是什么?有什么方法可以检查原因吗?
11:13:48,350 WARN [pool-2-thread-3] Service:215 - Thread exiting main loop due to exception:
java.lang.RuntimeException: java.net.BindException: Address already in use: JVM_Bind
at ca.uhn.hl7v2.app.AcceptorThread.afterStartup(AcceptorThread.java:106)
at ca.uhn.hl7v2.concurrent.Service.run(Service.java:202)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.net.BindException: Address already in use: JVM_Bind
at java.net.DualStackPlainSocketImpl.bind0(Native Method)
at java.net.DualStackPlainSocketImpl.socketBind(Unknown Source)
at java.net.AbstractPlainSocketImpl.bind(Unknown Source)
at java.net.PlainSocketImpl.bind(Unknown Source)
at java.net.ServerSocket.bind(Unknown Source)
at java.net.ServerSocket.bind(Unknown Source)
at ca.uhn.hl7v2.app.AcceptorThread.afterStartup(AcceptorThread.java:103)
编辑:我得到了错误的来源。我正在使用一个@Component
实现ApplicationListener<ContextRefreshedEvent>
. 该组件在 tomcat 启动时运行,但是当我运行单元测试时,它再次运行该组件。为什么会这样?
这是供参考的组件 -
@Component
public class RunBackgroundServices implements ApplicationListener<ContextRefreshedEvent> {
private final BackgroundServices backgroundServices;
private ExecutorService executor;
@Autowired
public RunBackgroundServices(BackgroundServices backgroundServices) {
this.backgroundServices= backgroundServices;
}
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
executor = Executors.newSingleThreadExecutor();
executor.submit(backgroundServices);
}
public void onApplicationEvent(ContextStoppedEvent event) {
executor.shutdown();
}
}