0

我收到 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();
     }
}
4

1 回答 1

1

您正在使用的端口是什么?看起来你阻止了那个端口并且你没有释放它。

尝试这个。

     Linux: netstat -lpn | grep "your port" Then with the pid. kill -9 pid

     Mac: lsof -i tcp:"your port" Then with the pid. kill -9 pid
于 2014-07-16T07:49:34.343 回答