对于让spring管理监听的实现方式有两种方法,一种是自己写监听器,然后交给spring的监听适配器管理,再由监听容器管理监听适配器,另一种是写一个实现MessageListener接口的类。第一种在第一章涉及到,但是没有交给spring托管.其实实现的方法很简单,在j2ee mvc模式中,用到spring的aop特性.这章讲第
二种方法,这样消费者可以时时接受到生产者的消息,订阅者可以时时接受到发布者的消息.不用循环接受。
1.Queue(点对点)方式
消费者QueueConsumerMessageListener类,具体如下:
package jms.mq.spring; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.TextMessage; public class QueueConsumerMessageListener implements MessageListener{ public void onMessage(Message msg) { if(msg instanceof TextMessage){ TextMessage textMessage = (TextMessage) msg; try { System.out.println("--队列 MessageListener收到信息:"+textMessage.getText()); } catch (JMSException e) { e.printStackTrace(); } } } }
2.Topic(发布/订阅)方式
订阅者实现类TopicSubscriberMessageListener类,具体如下:
package jms.mq.spring; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.TextMessage; public class TopicSubscriberMessageListener implements MessageListener{ public void onMessage(Message msg) { if(msg instanceof TextMessage){ TextMessage textMessage = (TextMessage) msg; try { System.out.println("--订阅者 MessageListener收到信息:"+textMessage.getText()); } catch (JMSException e) { e.printStackTrace(); } } } }
3.修改配置文件
<!-- 实现接口的方式 --> <bean id="queueConsumerMessageListener" class="jms.mq.spring.QueueConsumerMessageListener"> </bean> <!-- 新增一个队列地址名字不能跟上面的重复 --> <bean id="queueConsumerMessageListenerDest" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg index="0" value="myMessageListenerQueue" /> </bean> <bean id="myMsgQueuelistenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory" /> <property name="destination" ref="queueConsumerMessageListenerDest" /> <property name="messageListener" ref="queueConsumerMessageListener" /> <property name="receiveTimeout" value="10000" /> </bean> <bean id="topicSubscriberMessageListener" class="jms.mq.spring.TopicSubscriberMessageListener"></bean> <bean id="topicSubscriberMessageListenerDest" class="org.apache.activemq.command.ActiveMQTopic"> <constructor-arg index="0" value="myMessageListenerTopic" /> </bean> <bean id="myMsgTopiclistenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory" /> <property name="destination" ref="topicSubscriberMessageListenerDest" /> <property name="messageListener" ref="topicSubscriberMessageListener" /> <property name="pubSubDomain" value="true" /> <property name="receiveTimeout" value="10000" /> </bean>
<bean id="queueProducerService" class="jms.mq.spring.QueueProducerService"> <property name="jmsTemplate" ref="jmsQueueTemplate" /> <!--<property name="destination" ref="queueDest" /> --> <property name="destination" ref="queueConsumerMessageListenerDest" /> </bean> <bean id="queueConsumerService" class="jms.mq.spring.QueueConsumerService"> <property name="jmsTemplate" ref="jmsQueueTemplate" /> <property name="destination" ref="queueDest" /> </bean> <bean id="topicPublisherService" class="jms.mq.spring.TopicPublisherService"> <property name="jmsTemplate" ref="jmsTopicTemplate" /> <!-- <property name="destination" ref="topicDest" /> --> <property name="destination" ref="topicSubscriberMessageListenerDest" /> </bean> <bean id="topicSubscriberService" class="jms.mq.spring.TopicSubscriberService"> <property name="jmsTemplate" ref="jmsTopicTemplate" /> <property name="destination" ref="topicDest" /> </bean>
注意是修改 bean id为queueProducerService和的topicPublisherService的destination
4.运行
Queue(点对点)方式,只运行QueueProducerTest.java,结果如下
Topic(发布/订阅)方式,只运行TopicPublisherTest.java,结果如下
ok,如果Queue(点对点)方式和Topic(发布/订阅)方式运行的结果如上图所示,说明你成功了.