0

我对 JMS 侦听器容器有疑问。如果我禁用侦听器容器,一半的消息由侦听器传递和处理。这是我的 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";
}

启用监听器方法

@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 作为应用服务器。

4

1 回答 1

2

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

打开DEBUG日志记录org.springframework并检查 bean 初始化日志。

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

于 2013-08-23T11:55:12.930 回答