Spring中的java.net.BindException

时间:2014-07-16 06:02:07

标签: spring hapi

我收到了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的Spring ApplicationListener<ContextRefreshedEvent>。此组件在tomcat启动时运行,但是当我运行单元测试时,它会再次运行Component。为什么会这样?

以下是供参考的组件 -

@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();
     }
}

1 个答案:

答案 0 :(得分: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