Spring JMS Listener Container只停止一半的监听器

时间:2013-08-23 10:21:29

标签: spring jms jboss7.x hornetq spring-jms

我遇到了JMS Listener容器的问题。如果我禁用了侦听器容器,则一半的消息由侦听器传递和处理。这是我的Spring配置:

<bean id="ConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="JmsXA" />
</bean>

<bean id="testQueue" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="queue/test" />
</bean>

<bean id="listener" class="eu.cuptech.jms.listener.ExampleListener" />

<bean id="listenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
    <property name="connectionFactory" ref="ConnectionFactory" />
    <property name="destination" ref="testQueue" />
    <property name="messageListener" ref="listener" />
    <property name="concurrency" value="1" />
</bean>

ExampleListener 在这里:

package eu.cuptech.jms.listener;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

public class ExampleListener implements MessageListener {

    public void onMessage(Message message) {
        try {
            String msg = ((TextMessage) message).getText();
            System.out.println("MESSAGE TEXT: " + msg);
        } catch (JMSException e) {
            throw new RuntimeException(e);
        }
    }

}

客户端是Spring MVC控制器,具有以下方法:

send10Messages 方法(通用JMS客户端):

@Resource(name="ConnectionFactory")
private ConnectionFactory connectionFactory;

@Resource(name="testQueue")
private Queue testQueue;

@RequestMapping(value="send10", method = RequestMethod.GET)
public String send10Messages(ModelMap model, HttpSession session) throws Exception {
    sendTextMessages(10, "Test message: ");
    return "redirect:/info";
}

private void sendTextMessages(int count, final String prefix) throws Exception {
    Connection connection = null;
    Session session = null;
    MessageProducer messageProducer = null;
    try {
        connection = connectionFactory.createConnection();

        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        messageProducer = session.createProducer(testQueue);
        connection.start();
        TextMessage message = session.createTextMessage();

        int i = 0;
        while (i < count) {
            message.setText(prefix + ++i);
            messageProducer.send(message);
            Thread.sleep(250);
            System.out.println("Message " + prefix + i + " sent.");
        }

    } finally {
        try {
            if (messageProducer != null)
                messageProducer.close();
            if (connection != null)
                connection.close();
            if (session != null)
                session.close();
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}

disableListener 方法:

@Resource(name="listenerContainer")
private DefaultMessageListenerContainer listenerContainer;

@RequestMapping(value="disableListener", method = RequestMethod.GET)
public String disableListener(ModelMap model, HttpSession session) {
    listenerContainer.stop(new Runnable() {
        public void run() {
            System.out.println("JMS Listener stopped.");
        }
    });
    return "redirect:/info";
}

enableListener 方法

@Resource(name="listenerContainer")
private DefaultMessageListenerContainer listenerContainer;

@RequestMapping(value="enableListener", method = RequestMethod.GET)
public String enableListener(ModelMap model, HttpSession session) {
    listenerContainer.start();
    return "redirect:/info";
}

当我启动服务器并发送消息时,我得到了这个日志(没关系):

INFO [stdout] (listenerContainer-1) MESSAGE TEXT: Test message: 1
INFO [stdout] (http-localhost/127.0.0.1:8080-1) Message Test message: 1 sent.
INFO [stdout] (listenerContainer-1) MESSAGE TEXT: Test message: 2
INFO [stdout] (http-localhost/127.0.0.1:8080-1) Message Test message: 2 sent.
INFO [stdout] (listenerContainer-1) MESSAGE TEXT: Test message: 3
INFO [stdout] (http-localhost/127.0.0.1:8080-1) Message Test message: 3 sent.
INFO [stdout] (listenerContainer-1) MESSAGE TEXT: Test message: 4
INFO [stdout] (http-localhost/127.0.0.1:8080-1) Message Test message: 4 sent.
INFO [stdout] (listenerContainer-1) MESSAGE TEXT: Test message: 5
INFO [stdout] (http-localhost/127.0.0.1:8080-1) Message Test message: 5 sent.
INFO [stdout] (listenerContainer-1) MESSAGE TEXT: Test message: 6
INFO [stdout] (http-localhost/127.0.0.1:8080-1) Message Test message: 6 sent.
INFO [stdout] (listenerContainer-1) MESSAGE TEXT: Test message: 7
INFO [stdout] (http-localhost/127.0.0.1:8080-1) Message Test message: 7 sent.
INFO [stdout] (listenerContainer-1) MESSAGE TEXT: Test message: 8
INFO [stdout] (http-localhost/127.0.0.1:8080-1) Message Test message: 8 sent.
INFO [stdout] (listenerContainer-1) MESSAGE TEXT: Test message: 9
INFO [stdout] (http-localhost/127.0.0.1:8080-1) Message Test message: 9 sent.
INFO [stdout] (listenerContainer-1) MESSAGE TEXT: Test message: 10
INFO [stdout] (http-localhost/127.0.0.1:8080-1) Message Test message: 10 sent.

当我禁用侦听器容器并再次发送消息时,我得到了这个:

INFO [stdout] (listenerContainer-1) JMS Listener stopped.
INFO [stdout] (listenerContainer-1) MESSAGE TEXT: Test message: 1
INFO [stdout] (http-localhost/127.0.0.1:8080-1) Message Test message: 1 sent.
INFO [stdout] (http-localhost/127.0.0.1:8080-1) Message Test message: 2 sent.
INFO [stdout] (listenerContainer-1) MESSAGE TEXT: Test message: 3
INFO [stdout] (http-localhost/127.0.0.1:8080-1) Message Test message: 3 sent.
INFO [stdout] (http-localhost/127.0.0.1:8080-1) Message Test message: 4 sent.
INFO [stdout] (listenerContainer-1) MESSAGE TEXT: Test message: 5
INFO [stdout] (http-localhost/127.0.0.1:8080-1) Message Test message: 5 sent.
INFO [stdout] (http-localhost/127.0.0.1:8080-1) Message Test message: 6 sent.
INFO [stdout] (listenerContainer-1) MESSAGE TEXT: Test message: 7
INFO [stdout] (http-localhost/127.0.0.1:8080-1) Message Test message: 7 sent.
INFO [stdout] (http-localhost/127.0.0.1:8080-1) Message Test message: 8 sent.
INFO [stdout] (listenerContainer-1) MESSAGE TEXT: Test message: 9
INFO [stdout] (http-localhost/127.0.0.1:8080-1) Message Test message: 9 sent.
INFO [stdout] (http-localhost/127.0.0.1:8080-1) Message Test message: 10 sent.

当我再次启用侦听器容器时,我得到了这个:

INFO [stdout] (listenerContainer-1) MESSAGE TEXT: Test message: 2
INFO [stdout] (listenerContainer-1) MESSAGE TEXT: Test message: 4
INFO [stdout] (listenerContainer-1) MESSAGE TEXT: Test message: 6
INFO [stdout] (listenerContainer-1) MESSAGE TEXT: Test message: 8
INFO [stdout] (listenerContainer-1) MESSAGE TEXT: Test message: 10

问题是即使监听器被禁用,每个奇怪的消息也会被监听器处理。我说明当我启用侦听器容器时,侦听器将传递和处理所有消息,并且在禁用它时不会处理任何消息。

我正在使用Spring 3.2.4.RELEASE(我也尝试3.2.3.RELEASE),HornetQ 2.3.0.Final作为远程JMS服务器和JBoss 7.3.1.Final作为App服务器。

1 个答案:

答案 0 :(得分:2)

我的猜测是您将容器加载到Web上下文(DispatcherServlet的上下文)和根上下文(ContextLoaderListener的上下文)中。这意味着你有2个容器,而你只是在servlet上下文中停止它。

打开DEBUG的{​​{1}}日志记录并检查bean初始化日志。

它可能只是在根上下文中。