DefaultMessageListenerContainer不会停止

时间:2013-02-01 01:00:18

标签: java spring jms

我正在使用带有以下上下文XML文件的Spring JMS。

<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")并在代码中手动启动/停止容器,但是,它也不起作用。

2 个答案:

答案 0 :(得分:0)

在退出之前你应该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();
}

答案 1 :(得分:0)

我通过最后添加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来电。

我做得对吗?