2

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

4

2 回答 2

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();
}
于 2013-02-01T02:49:37.597 回答
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) ,我会留下电话。

我做对了吗?

于 2013-02-01T06:41:21.390 回答