spring整合JMS之异步消息监听机制
2016-08-19 10:39
866 查看
在上一节中,我们说到,使用receive方法来接收生产者消息的时候,只会运行一次,然后就会被阻塞,除非我们加个while(true),一直不停的轮询。今天就说说如何采用消息监听机制,来实现及时的收取消息。
消息监听器:MessageListener
JMS给我们提供了一个消息监听器的接口,只要我们实现了这个接口,就可以实现对消息的及时监听。实现代码如下:
消息监听器:MessageListener
JMS给我们提供了一个消息监听器的接口,只要我们实现了这个接口,就可以实现对消息的及时监听。实现代码如下:
public class ConsumerReceive implements MessageListener { @Override public void onMessage(Message message) { try { if(message instanceof TextMessage){ String receiveMessage = ((TextMessage) message).getText(); System.out.println("消费者收到的消息为:"+receiveMessage); } } catch (JMSException e) { e.printStackTrace(); } } }onMessage方法只接受一个Message对象的参数,所以为了更好的兼容想,我们在处理消息之前,可以使用instanceof来判断Message类型,然后根据具体的类型来做相应的处理。最后增加监听器配置文件如下:
<!-- 消息监听器 --> <bean id="consumerMessageListener" class="com.chhliu.myself.activemq.start.async.ConsumerReceive"/> <!-- 消息监听容器 --> <bean id="listenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory" /> <property name="destination" ref="queueDestination" /> <property name="messageListener" ref="consumerMessageListener" /> </bean>
相关文章推荐
- [置顶] Spring整合JMS异步消息
- 使用Spring JMS简化异步消息处理
- 用Spring JMS使异步消息变得简单
- Spring + JMS + ActiveMQ实现简单的消息队列(监听器异步实现)
- 使用Spring JMS简化异步消息处理
- 使用Spring JMS简化异步消息处理
- 使用Spring JMS简化异步消息处理
- 使用Spring JMS简化异步消息处理
- 使用Spring JMS简化异步消息处理
- 使用Spring JMS简化异步消息处理
- 用Spring JMS使异步消息变得简单
- JMS异步消息机制
- 使用Spring JMS简化异步消息处理
- 使用Spring JMS简化异步消息处理
- 使用Spring JMS简化异步消息处理
- 使用Spring JMS轻松实现异步消息传递
- 用Spring JMS使异步消息变得简单
- 转:用Spring JMS使异步消息变得简单
- 使用Spring JMS简化异步消息处理
- spring jms异步消息处理