【问题标题】:DefaultMessageListenerContainer does not stopDefaultMessageListenerContainer 不会停止
【发布时间】:2013-02-01 01:00:17
【问题描述】:

我将 Spring JMS 与以下上下文 XML 文件一起使用。

<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"
      p:brokerURL="tcp://localhost:61616" />

<bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop"
      p:connectionFactory-ref="connectionFactory" />

<bean id="queue1" class="org.apache.activemq.command.ActiveMQQueue">
    <constructor-arg value="foo.bar"/>
</bean>

<bean id="listenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer" destroy-method="destroy"
      p:autoStartup="true"
      p:connectionFactory-ref="pooledConnectionFactory"
      p:destination-ref="queue1"
      p:messageListener-ref="listener"
      p:acceptMessagesWhileStopping="false"
      p:sessionTransacted="true" />

我的应用是命令行独立的,看起来像这样:

public static void main(String[] args) throws JMSException, InterruptedException  {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("my-context.xml");
    context.registerShutdownHook();

    App app = context.getBean("app", App.class);
    app.start();
}

这里的问题是,当主线程结束时,进程并没有停止。我目前的猜测是侦听器容器没有停止。

这是最后的日志消息:

16:54:02,747 DEBUG [main] DefaultListableBeanFactory:246 - Returning cached instance of singleton bean 'app'
16:54:02,747  INFO [main] App:47 - Terminating...
16:54:02,747  INFO [main] App:51 - Terminated.
16:54:02,771 DEBUG [listenerContainer-1] TaskRunnerFactory:91 - Initialized TaskRunnerFactory[ActiveMQ Session Task] using ExecutorService: java.util.concurrent.ThreadPoolExecutor@6c63a721
16:54:03,783 DEBUG [listenerContainer-1] ActiveMQSession:559 - ID:Daniels-MacBook-Pro.local-56179-1359680042616-1:1:1 Transaction Commit :null
16:54:04,784 DEBUG [listenerContainer-1] ActiveMQSession:559 - ID:Daniels-MacBook-Pro.local-56179-1359680042616-1:1:1 Transaction Commit :null
16:54:05,785 DEBUG [listenerContainer-1] ActiveMQSession:559 - ID:Daniels-MacBook-Pro.local-56179-1359680042616-1:1:1 Transaction Commit :null
16:54:06,787 DEBUG [listenerContainer-1] ActiveMQSession:559 - ID:Daniels-MacBook-Pro.local-56179-1359680042616-1:1:1 Transaction Commit :null
16:54:07,788 DEBUG [listenerContainer-1] ActiveMQSession:559 - ID:Daniels-MacBook-Pro.local-56179-1359680042616-1:1:1 Transaction Commit :null
...

仅供参考,我尝试关闭自动启动(通过设置p:autoStartup="false")并在代码中手动启动/停止容器,但是它也不起作用。

【问题讨论】:

    标签: java spring jms


    【解决方案1】:

    你在退出之前应该 stop() 上下文:

    public static void main(String[] args) throws JMSException, InterruptedException  {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("my-    context.xml");
        context.registerShutdownHook();
    
        App app = context.getBean("app", App.class);
        app.start();
    
        context.stop();
    }
    

    【讨论】:

    • 不,没有用。另外,我已经打电话给context.registerShutdownHook()
    【解决方案2】:

    我通过在末尾添加context.close() 调用解决了这个问题。

    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("my-context.xml");
        context.registerShutdownHook();
    
        App app = context.getBean("app", App.class);
        app.start();
    
        context.close();
    }
    

    我留下了context.registerShutdownHook() 电话,以防应用程序在外部关闭(如Ctrl+C)。

    我做得对吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-30
      • 2013-09-01
      • 2013-01-21
      • 2010-11-05
      • 2013-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多