`
dengwanchuan
  • 浏览: 46753 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
最近访客 更多访客>>
社区版块
存档分类
最新评论

Java中间件JMS(四)之ActiveMQ整合spring之类转换

阅读更多

原文链接:http://blog.csdn.net/dwc_fly/article/details/11096071

前几章都是直接发送MapMessage类型的数据,拿前面的例子来讲,如果生产者发送的是TextMessage,消费者也是必须TextMessage;如果我们自己要发送的数据不是TextMessage类型,而消费者还是TextMessage的,那该怎么办?难道每次接受后都要增加一个转换方法么?其实spring早就考虑到这种情况了。转化器在很多组件中都是必不缺少的东西Spring的MessageConverter接口提供了对消息转换的支持。

 

1、转换类的相关代码POJO

新建一个类MsgPoJo,就是一个简单的Pojo类。具体代码如下:

[java] view plaincopy
 
  1. package jms.mq.spring;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. public class MsgPoJo implements Serializable{  
  6.     private String id;  
  7.     private String text;  
  8.     public String getId() {  
  9.         return id;  
  10.     }  
  11.     public void setId(String id) {  
  12.         this.id = id;  
  13.     }  
  14.     public String getText() {  
  15.         return text;  
  16.     }  
  17.     public void setText(String text) {  
  18.         this.text = text;  
  19.     }     
  20. }  

 

2.转换类的实现

新建一个类MsgConverter.java,实现MessageConverter接口。生成的代码如下

 

[java] view plaincopy
 
  1. package jms.mq.spring;  
  2.   
  3. import javax.jms.JMSException;  
  4. import javax.jms.Message;  
  5. import javax.jms.Session;  
  6. import javax.jms.TextMessage;  
  7.   
  8. import org.springframework.jms.support.converter.MessageConversionException;  
  9. import org.springframework.jms.support.converter.MessageConverter;  
  10.   
  11. public class MsgConverter implements MessageConverter{  
  12.   
  13.     @Override  
  14.     public Object fromMessage(Message message) throws JMSException,  
  15.     MessageConversionException {  
  16.         if (!(message instanceof TextMessage)) {  
  17.             throw new MessageConversionException("Message is not TextMessage");  
  18.         }  
  19.         System.out.println("--转换接收的消息--");  
  20.         TextMessage textMessage = (TextMessage) message;  
  21.         MsgPoJo msgPojo = new MsgPoJo();  
  22.         String[] texts=textMessage.getText().split(",");  
  23.         msgPojo.setId(texts[0]);  
  24.         msgPojo.setText(texts[1]);  
  25.         return msgPojo;  
  26.     }  
  27.   
  28.     @Override  
  29.     public Message toMessage(Object object, Session session) throws JMSException,  
  30.     MessageConversionException {  
  31.         if (!(object instanceof MsgPoJo)) {  
  32.             throw new MessageConversionException("obj is not MsgPojo");  
  33.         }  
  34.         System.out.println("--转换发送的消息--");  
  35.         MsgPoJo msgPojo = (MsgPoJo) object;  
  36.         TextMessage textMessage = session.createTextMessage();  
  37.         textMessage.setText(msgPojo.getId()+","+msgPojo.getText());  
  38.         return  textMessage;  
  39.     }  
  40. }  

 

 

代码很简单就是做些转换,有fromMessage和toMessage两个方法,真好对应发送转换toMessage和接受转换fromMessage。此时,发送和接收消息要换成template.convertAndSend(message);template.receiveAndConvert()。接下来我做一些配置,让spring知道我们的转换类。修改applicationContext.xml中jms模版配置的代码,修改后的代码如下:

[html] view plaincopy
 
  1. <!-- 类转换 -->  
  2. <bean id="msgConverter" class="jms.mq.spring.MsgConverter"></bean>  
  3.   
  4. <!-- 配置Jms模板 -->  
  5. <bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">  
  6.     <property name="connectionFactory" ref="connectionFactory" />  
  7.     <property name="defaultDestination" ref="queueDest" />  
  8.     <!--<property name="receiveTimeout" value="10000" /> -->  
  9.     <!-- 类转换 -->  
  10.     <property name="messageConverter" ref="msgConverter"></property>  
  11. </bean>  

注意:如果你有队列监听容器配置,配置jmsQueueTemplate和jmsTopicTemplate可能与队列容器配置冲突。

3、业务相关代码和配置

在QueueProducerService.java增加convertAndSend()方法并在其实现类中实现,实现类的代码如下:

 

[java] view plaincopy
 
  1. package jms.mq.spring;  
  2.   
  3. import java.util.Date;  
  4.   
  5. import javax.jms.Destination;  
  6. import javax.jms.JMSException;  
  7. import javax.jms.Message;  
  8. import javax.jms.Session;  
  9. import javax.jms.TextMessage;  
  10. import org.springframework.jms.core.JmsTemplate;  
  11. import org.springframework.jms.core.MessageCreator;  
  12.   
  13. public class QueueProducerService{  
  14.     JmsTemplate jmsTemplate;  
  15.   
  16.     Destination destination;  
  17.   
  18.     public void send() {  
  19.         MessageCreator messageCreator = new MessageCreator() {  
  20.             public Message createMessage(Session session) throws JMSException {  
  21.                 TextMessage message = session.createTextMessage();  
  22.                 message.setText("QueueProducerService发送消息"+new Date());  
  23.                 return message;  
  24.             }  
  25.   
  26.         };  
  27.         jmsTemplate.send(this.destination,messageCreator);  
  28.     }  
  29.       
  30.     public void convertAndSend(){  
  31.         MsgPoJo msgPojo = new MsgPoJo();  
  32.         msgPojo.setId("1");  
  33.         msgPojo.setText("first msg");  
  34.         System.out.println("--发送消息:msgPojo.id为"+msgPojo.getId()+";msgPojo.text为"+msgPojo.getText());  
  35.         jmsTemplate.convertAndSend(this.destination, msgPojo);  
  36.     }  
  37.   
  38.   
  39.     public void setJmsTemplate(JmsTemplate jmsTemplate) {  
  40.         this.jmsTemplate = jmsTemplate;  
  41.     }  
  42.       
  43.     public void setDestination(Destination destination) {  
  44.         this.destination = destination;  
  45.     }  
  46. }  

 

同样在QueueConsumerService.java中增加receiveAndConvert()方法并在其实现类中实现,实现类的代码如下:

 

[java] view plaincopy
 
  1. package jms.mq.spring;  
  2.   
  3. import javax.jms.Destination;  
  4. import javax.jms.JMSException;  
  5. import javax.jms.TextMessage;  
  6. import org.springframework.jms.core.JmsTemplate;  
  7.   
  8.   
  9. public class QueueConsumerService{  
  10.   
  11.     JmsTemplate jmsTemplate;  
  12.   
  13.     Destination destination;  
  14.   
  15.     public void receive() {  
  16.         TextMessage message = (TextMessage) jmsTemplate.receive();  
  17.         try {  
  18.             System.out.println("QueueConsumerService收到消息:"+message.getText());  
  19.   
  20.         } catch (JMSException e) {  
  21.             e.printStackTrace();  
  22.         }  
  23.     }  
  24.   
  25.     public void receiveAndConvert() {  
  26.         MsgPoJo msgPojo = (MsgPoJo)jmsTemplate.receiveAndConvert();  
  27.         if(msgPojo!=null){  
  28.             System.out.println("--收到消息:msgPojo.id为"+msgPojo.getId()+";msgPojo.text为"+msgPojo.getText());  
  29.         }  
  30.     }  
  31.   
  32.     public void setJmsTemplate(JmsTemplate jmsTemplate) {  
  33.         this.jmsTemplate = jmsTemplate;  
  34.     }  
  35.   
  36.     public void setDestination(Destination destination) {  
  37.         this.destination = destination;  
  38.     }  
  39. }  

 

 

修改我们的两个测试类,增加对转换方法的调用,不再赘述,直接上代码:

QueueConsumerTest.java测试类

[java] view plaincopy
 
  1. package jms.mq.spring;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. public class QueueConsumerTest {  
  7.     private static ApplicationContext appContext = new ClassPathXmlApplicationContext( "applicationContext.xml");  
  8.   
  9.     private static void receive() {  
  10.         QueueConsumerService consumerService = (QueueConsumerService) appContext.getBean("queueConsumerService");  
  11.         consumerService.receive();  
  12.     }  
  13.   
  14.     private static void receiveAndConvert() {  
  15.         QueueConsumerService consumerService = (QueueConsumerService) appContext.getBean("queueConsumerService");  
  16.         consumerService.receiveAndConvert();  
  17.     }  
  18.   
  19.   
  20.     public static void main(String[] args) {  
  21.         //receive();  
  22.         receiveAndConvert();  
  23.     }  
  24. }  

QueueProducerTest.java测试类

[java] view plaincopy
 
  1. package jms.mq.spring;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. public class QueueProducerTest {  
  7.     private static ApplicationContext appContext = new ClassPathXmlApplicationContext( "applicationContext.xml");  
  8.   
  9.     private static void send() {  
  10.         QueueProducerService producerService = (QueueProducerService) appContext.getBean("queueProducerService");  
  11.         producerService.send();  
  12.     }  
  13.       
  14.     private static void convertAndSend() {  
  15.         QueueProducerService producerService = (QueueProducerService) appContext.getBean("queueProducerService");  
  16.         producerService.convertAndSend();  
  17.     }  
  18.   
  19.     public static void main(String[] args) {  
  20.         //send();  
  21.         convertAndSend();  
  22.     }  
  23.   
  24. }  

 

代码编写完毕,我们看一下我们的劳动成果。首先运行生产者类和消费者控制台信息如下:

 

收到的内容与发的内容相同,说明转换成功了。如果这一部分的程序使用的队列跟上面的一样,那你会发现发送的时候打印出的信息不值上面的一个,还包括一个接收的信息,这是为什么呢?了解spring原理的人应该知道,spring是把所有类都加载到内容中,当然也包括我们上门写的按个实现MessageListener的一个消费者类,他们也在运行,如果监听的地址跟你送的地址正好相同的话,他也有可能收到这个信息。所以在测试的时候要注意修改配置文件。

 

[html] view plaincopy
 
  1. <bean id="queueProducerService" class="jms.mq.spring.QueueProducerService">  
  2.     <property name="jmsTemplate" ref="jmsQueueTemplate" />  
  3.     <property name="destination" ref="queueDest" />    
  4. </bean>  
  5.   
  6. <bean id="queueConsumerService" class="jms.mq.spring.QueueConsumerService">  
  7.     <property name="jmsTemplate" ref="jmsQueueTemplate" />  
  8.     <property name="destination" ref="queueDest" />   
  9. </bean>  

 

4、监听器上的使用方式

我再来学习一下跟监听器联合使用的方式,只在发布订阅者模式上演示一下。我们先来修改发布者的实现方式,在发布者中增加convertAndSend方法并在其实现类中实现,订阅者监听器没有类转换,不用修改,发布者修改后的代码如下:

 

[java] view plaincopy
 
  1. package jms.mq.spring;  
  2.   
  3. import java.util.Date;  
  4.   
  5. import javax.jms.Destination;  
  6. import javax.jms.JMSException;  
  7. import javax.jms.MapMessage;  
  8. import javax.jms.Message;  
  9. import javax.jms.Session;  
  10. import javax.jms.TextMessage;  
  11.   
  12. import org.springframework.jms.core.JmsTemplate;  
  13. import org.springframework.jms.core.MessageCreator;  
  14.   
  15. import jms.spring.QueueProducerService;  
  16.   
  17. public class TopicPublisherService{  
  18.     JmsTemplate jmsTemplate;  
  19.        
  20.     Destination destination;  
  21.   
  22.     public void send() {  
  23.         MessageCreator messageCreator = new MessageCreator() {  
  24.   
  25.             public Message createMessage(Session session) throws JMSException {  
  26.                 TextMessage message = session.createTextMessage();  
  27.                 message.setText("QueueProducerService发送消息"+new Date());  
  28.                 return message;  
  29.             }  
  30.         };  
  31.         jmsTemplate.send(this.destination,messageCreator);  
  32.     }  
  33.   
  34.     public void convertAndSend(Object obj) {  
  35.         System.out.println("--发送PoJo对象...");  
  36.         jmsTemplate.convertAndSend(destination, obj);  
  37.     }  
  38.   
  39.       
  40.     public void setJmsTemplate(JmsTemplate jmsTemplate) {  
  41.         this.jmsTemplate = jmsTemplate;  
  42.     }  
  43.   
  44.     public void setDestination(Destination destination) {  
  45.         this.destination = destination;  
  46.     }  
  47.   
  48. }  

 

发布订阅者配置文件如下

 

[html] view plaincopy
 
  1. <!-- 配置TopicJms模板 -->  
  2. <bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate">  
  3.     <property name="connectionFactory" ref="connectionFactory" />  
  4.     <property name="defaultDestination" ref="topicDest" />  
  5.     <!-- 配置是否为发布订阅者模式,默认为false -->  
  6.     <property name="pubSubDomain" value="true" />  
  7.     <!--<property name="receiveTimeout" value="10000" /> -->  
  8.     <property name="messageConverter" ref="msgConverter"></property>  
  9. </bean>  
[html] view plaincopy
 
  1. <bean id="topicPublisherService" class="jms.mq.spring.TopicPublisherService">  
  2.         <property name="jmsTemplate" ref="jmsTopicTemplate" />  
  3.         <!-- <property name="destination" ref="topicDest" /> -->  
  4.         <property name="destination" ref="topicSubscriberMessageListenerDest" />   
  5.     </bean>  
  6.   
  7.     <bean id="topicSubscriberService" class="jms.mq.spring.TopicSubscriberService">  
  8.         <property name="jmsTemplate" ref="jmsTopicTemplate" />  
  9.         <property name="destination" ref="topicDest" />  
  10.     </bean>  

 

修改上面的发布测试类,修改增加对新增方法的调用,修改后的内容如下:

[java] view plaincopy
 
  1. package jms.mq.spring;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. public class TopicPublisherTest {  
  7.     private static ApplicationContext appContext = new ClassPathXmlApplicationContext( "applicationContext.xml");  
  8.   
  9.     private static void send() {  
  10.         TopicPublisherService topicPublisherService = (TopicPublisherService) appContext.getBean("topicPublisherService");  
  11.         topicPublisherService.send();  
  12.     }  
  13.     private static void convertAndSend() {  
  14.         TopicPublisherService topicPublisherService = (TopicPublisherService) appContext.getBean("topicPublisherService");  
  15.         MsgPoJo msgPoJo = new MsgPoJo();  
  16.         msgPoJo.setId("1");  
  17.         msgPoJo.setText("测试内容");  
  18.         topicPublisherService.convertAndSend(msgPoJo);  
  19.     }  
  20.   
  21.   
  22.     public static void main(String[] args) {  
  23.         //send();  
  24.         convertAndSend();  
  25.     }  
  26. }  

运行发布测试类,运行结果如下:

写在到这里,ActiveMQ与spring整合就讲完了,主要讲了ActiveMQ与spring的简单整合,监听器和类转换这些主要功能.

呵呵,写到不好,请大家不要拍砖。

0
1
分享到:
评论
1 楼 zn100200 2013-11-28  
从一到五我都配置成功了也能正常运行,很好,虽然还是有些不明白的但谢谢分享。

相关推荐

    ActiveMQ整合spring的Demo

    通过这个Demo,开发者可以学习到如何在实际项目中整合Spring和ActiveMQ,从而利用消息中间件提升系统的可伸缩性和可靠性。在理解这些核心概念后,你可以根据项目需求进行更复杂的配置和优化,例如使用Spring Boot...

    activemq与spring整合发送jms消息入门实例

    在Java世界中,ActiveMQ和Spring的整合是企业级应用中常见的消息中间件解决方案,用于实现JMS(Java Message Service)消息传递。本教程将深入探讨如何将这两个强大的工具结合在一起,以创建一个简单的发送JMS消息的...

    activeMq整合spring所需jar包

    1. **ActiveMQ**: ActiveMQ是Apache软件基金会开发的一款开源消息中间件,它遵循Java Message Service (JMS) 规范,提供可靠的消息传递服务,支持多种协议,如OpenWire、AMQP、STOMP等。在Spring框架中整合ActiveMQ...

    activemq与spring整合源代码

    Spring还提供了丰富的模块,如数据访问、Web、测试等,其中Spring JMS模块专门用于集成消息中间件,使得与ActiveMQ的整合变得简单。 三、ActiveMQ与Spring的整合 1. 添加依赖:首先,在项目中引入ActiveMQ和Spring...

    activemq与spring的整合案例

    将ActiveMQ与Spring进行整合,可以方便地在Spring应用中使用消息队列功能。 本案例主要展示了如何在Spring应用中集成ActiveMQ,实现消息的发送和接收。首先,我们需要在项目中引入ActiveMQ的相关依赖。在Maven工程...

    activeMQ+spring整合

    ActiveMQ作为Apache软件基金会的一个项目,是一款开源的消息中间件,支持多种协议,如AMQP、STOMP、MQTT等,并且与Java平台紧密结合。Spring框架则是一个广泛使用的Java企业级应用开发框架,提供了一整套的解决方案...

    ActiveMQ与spring整合封装

    首先,ActiveMQ与Spring的整合主要基于Spring的JMS(Java Message Service)支持。Spring通过`ConnectionFactory`和`Destination`接口来抽象JMS提供者的具体实现,使得我们能够轻松地更换消息中间件,而无需修改大量...

    spring整合jms+activemq

    在IT行业中,Spring框架是Java领域最广泛应用的轻量级框架之一,而JMS(Java Message Service)则是一种标准接口,用于在分布式系统中进行异步消息传递。ActivemQ是Apache软件基金会的一个项目,它实现了JMS规范,...

    消息中间件ActiveMQ及Spring整合JMS.docx

    【ActiveMQ和Spring整合JMS】的文档主要介绍了消息中间件的基本概念,特别是重点讨论了ActiveMQ和JMS的相关知识。消息中间件是用于不同分布式系统之间数据交流的工具,通过消息传递机制来扩展进程间的通信。ActiveMQ...

    activemq整合spring完整实例代码(内含所有相关jar包)

    Apache ActiveMQ是开源的、高性能的消息中间件,它支持多种消息协议,如OpenWire、AMQP、STOMP等。在企业级应用中,ActiveMQ常被用来实现应用程序间的异步通信,提高系统的响应速度和并发处理能力。Spring框架是Java...

    java中间件之activemq

    Java中间件领域的ActiveMQ是一款由Apache开发的开源消息中间件,它为企业级应用提供高效、可扩展、稳定且安全的消息通信服务。ActiveMQ的核心目标是实现标准的、面向消息的集成,支持多语言环境,确保不同平台之间的...

    spring整合JMS-居于ActiveMQ实现

    Spring整合JMS基于ActiveMQ实现是一项常见的企业级应用开发任务,它涉及到Spring框架、Java消息服务(JMS)以及ActiveMQ消息中间件的使用。在本文中,我们将深入探讨这三个关键概念,以及如何将它们有效地结合在一起...

    ActiveMQ整合Spring(多消费者)

    Spring提供了JMS模块,通过`org.springframework.jms`包中的类和接口,简化了与JMS提供者(如ActiveMQ)的交互。例如,`JmsTemplate`是核心工具类,用于发送和接收消息。 2. **配置ActiveMQ**: 在Spring配置文件...

    ActiveMQ整合spring、SpringBoot完整源码

    首先,ActiveMQ整合Spring涉及到的是Spring的JMS(Java Message Service)模块。Spring对JMS提供了一套抽象层,简化了与消息中间件的交互。在配置中,我们需要在Spring的XML配置文件或Java配置类中定义一个...

    jms Spring+ActiveMQ 5.4.2

    标题 "jms Spring+ActiveMQ 5.4.2" 涉及的是Java消息服务(JMS)在Spring框架中的应用,以及ActiveMQ作为消息代理的使用。在这个主题下,我们将深入探讨JMS的基本概念、Spring对JMS的支持以及ActiveMQ 5.4.2版本的...

    activemq5.5.1 Spring模板

    《ActiveMQ 5.5.1与Spring模板的深度整合》 在当今的企业级应用开发中,消息中间件起着至关重要的作用,它能够有效地解耦应用程序,提高系统的可扩展性和可靠性。Apache ActiveMQ作为开源社区中最受欢迎的消息...

    spring 与ACTIVEMQ整合

    在IT行业中,Spring框架是Java领域最常用的轻量级应用框架之一,而ActiveMQ则是Apache组织开发的一款开源消息中间件,常用于实现应用程序间的异步通信。本文将深入探讨如何将Spring与ActiveMQ进行整合,以提升系统的...

    JMS+ActiveMQ+Spring 完整样例代码

    **JMS+ActiveMQ+Spring 完整样例代码详解** 在Java世界中,消息队列(Message Queue,简称MQ)是一种重要的中间件技术,它允许应用程序之间通过异步通信来解耦系统组件。Java消息服务(Java Message Service,简称...

    JMS_ActiveMQ_Spring.rar

    将JMS、ActiveMQ和Spring整合,首先需要在Spring配置文件中定义ConnectionFactory,这是与消息服务器建立连接的工厂类。接着,创建目的地(Destination)配置,包括队列(Queue)和主题(Topic)。然后,定义Message...

Global site tag (gtag.js) - Google Analytics