我有一个JMS侦听容器weired问题。 在情况下,我禁用监听器容器,消息的一半被递送,并通过监听处理。 这里是我的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";
}
当我启动服务器,并送我得到这个日志消息(它是OK):
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
的问题是,每个奇数消息是由听众处理即使听者被禁用。 我expact所有消息将被听众进行传递和处理时启用监听容器,当我禁用它将被处理任何消息。
我使用Spring 3.2.4.RELEASE(我试图3.2.3.RELEASE太),HornetQ的2.3.0.Final远程JMS服务器和JBoss 7.3.1.Final作为应用服务器。