Java中间件JMS(二)之ActiveMQ整合spring(一)
原文链接:http://blog.csdn.net/dwc_fly/article/details/10306805
在上一章( Java中间件JMS之ActiveMQ入门http://blog.csdn.net/dengwanchuan/article/details/10241345)说到ActiveMQ能与spring进行整合,ActiveMQ与Spring进行整合有一定的好处,首先是可配置化,然后是能使用Spring的aop,tx等特性进行项目开发.
一.准备工作
我使用的是spring版本是4.0.0.M2,其他版本的也可以,只是配置不同,去Spring官网下载zip包,解开后将dist目录下的所有jar包(根据自己选择)拷贝到项目lib目录下并加入项目项目中的lib中,一般jms所需要的Spring的jar有:
二.代码开发
1.在src目录下新建applicationContext.xml文件并输入一下内容:
package jms.mq.spring; import java.util.Date; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.Session; import javax.jms.TextMessage; import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.core.MessageCreator; public class QueueProducerService{ JmsTemplate jmsTemplate; Destination destination; public void send() { MessageCreator messageCreator = new MessageCreator() { public Message createMessage(Session session) throws JMSException { TextMessage message = session.createTextMessage(); message.setText("QueueProducerService发送消息"+new Date()); return message; } }; jmsTemplate.send(this.destination,messageCreator); } public void setJmsTemplate(JmsTemplate jmsTemplate) { this.jmsTemplate = jmsTemplate; } public void setDestination(Destination destination) { this.destination = destination; } }
生产者编写完了,下面我们来编写消费者,上面说了,发送消息的时候,spring的beanfactory得到一个jmsTemplate的实例和消息目标的实例,然后发送,那么接受的时候肯定也是得到一个jmsTemplate的实例和消息目标的实例,然后接受,下面我们来看具体代码。
新建一个消费者类QueueConsumerService.java,具体代码如下:
package jms.mq.spring; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.TextMessage; import org.springframework.jms.core.JmsTemplate; public class QueueConsumerService{ JmsTemplate jmsTemplate; Destination destination; public void receive() { TextMessage message = (TextMessage) jmsTemplate.receive(); try { System.out.println("QueueConsumerService收到消息:"+message.getText()); } catch (JMSException e) { e.printStackTrace(); } } public void setJmsTemplate(JmsTemplate jmsTemplate) { this.jmsTemplate = jmsTemplate; } public void setDestination(Destination destination) { this.destination = destination; } }
代码编写完毕,下面要进行bean的配置,在applicationContext.xml中加入如下代码实例化对象和依赖注入:
package jms.mq.spring; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class QueueProducerTest { private static ApplicationContext appContext = new ClassPathXmlApplicationContext( "applicationContext.xml"); private static void send() { QueueProducerService producerService = (QueueProducerService) appContext.getBean("queueProducerService"); producerService.send(); } /** * @param args */ public static void main(String[] args) { send(); } }
再建一个消费者的测试类,QueueConsumerTest.java,具体代码如下:
package jms.mq.spring; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class QueueConsumerTest { private static ApplicationContext appContext = new ClassPathXmlApplicationContext( "applicationContext.xml"); private static void receive() { QueueConsumerService consumerService = (QueueConsumerService) appContext.getBean("queueConsumerService"); consumerService.receive(); } public static void main(String[] args) { receive(); } }
5、运行point-point(点对点)程序
所有代码都编写完了,我们来看一下我们的劳动成果。运行生产者测试类。控制台打印出如下内容,画线标注的就是我们发送的内容:
6、编写Publisher/Subscriber(发布/订阅者)代码
新建发布者TopicPublisherService.java,内容如下:
package jms.mq.spring; import java.util.Date; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.MapMessage; import javax.jms.Message; import javax.jms.Session; import javax.jms.TextMessage; import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.core.MessageCreator; import jms.spring.QueueProducerService; public class TopicPublisherService{ JmsTemplate jmsTemplate; Destination destination; public void send() { MessageCreator messageCreator = new MessageCreator() { public Message createMessage(Session session) throws JMSException { TextMessage message = session.createTextMessage(); message.setText("QueueProducerService发送消息"+new Date()); return message; } }; jmsTemplate.send(this.destination,messageCreator); } public void setJmsTemplate(JmsTemplate jmsTemplate) { this.jmsTemplate = jmsTemplate; } public void setDestination(Destination destination) { this.destination = destination; } }
再新建一个订阅者TopicSubscriberService.java,代码如下。
package jms.mq.spring; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.TextMessage; import org.springframework.jms.core.JmsTemplate; import jms.spring.QueueConsumerService; public class TopicSubscriberService{ JmsTemplate jmsTemplate; Destination destination; public void receive() { TextMessage message = (TextMessage) jmsTemplate.receive(); try { System.out.println("QueueConsumerService收到消息:"+message.getText()); } catch (JMSException e) { e.printStackTrace(); } } public void setJmsTemplate(JmsTemplate jmsTemplate) { this.jmsTemplate = jmsTemplate; } public void setDestination(Destination destination) { this.destination = destination; } }
在配置文件中applicationContext.xml增加如下配置:
package jms.mq.spring; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TopicPublisherTest { private static ApplicationContext appContext = new ClassPathXmlApplicationContext( "applicationContext.xml"); private static void send() { TopicPublisherService topicPublisherService = (TopicPublisherService) appContext.getBean("topicPublisherService"); topicPublisherService.send(); } public static void main(String[] args) { send(); } }
编写测试程序订阅者测试类,TopicSubscriberTest.java
package jms.mq.spring; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TopicSubscriberTest { private static ApplicationContext appContext = new ClassPathXmlApplicationContext( "applicationContext.xml"); private static void receive() { TopicSubscriberService topicSubscriberService = (TopicSubscriberService) appContext.getBean("topicSubscriberService"); topicSubscriberService.receive(); } public static void main(String[] args) { receive(); } }
7.Publisher/Subscriber(发布/订阅者)程序
先运行订阅者,再运行发布者,可以看到订阅者能打印信息;但是反之就不行,这就是Publisher/Subscriber(发布/订阅者)的特性;
跟Point-Point(点对点)对比的话,不管运行生存者还是消费者,都会打印信息,可以阅读前一章http://blog.csdn.net/dengwanchuan/article/details/10241345了解这两种模式的区别和联系。
附加完整的applicationContext.xml配置文件
- <?xml version="1.0" encoding="GBK"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd">
- <!-- 配置JMS连接工厂 -->
- <bean id="connectionFactory"
- class="org.apache.activemq.ActiveMQConnectionFactory">
- <property name="brokerURL" value="tcp://localhost:61616" />
- </bean>
- <!-- 发送消息的目的地(队列) -->
- <bean id="queueDest"
- class="org.apache.activemq.command.ActiveMQQueue">
- <!-- 设置消息队列的名字 -->
- <constructor-arg index="0" value="myQueue" />
- </bean>
- <!-- 配置Jms模板 -->
- <bean id="jmsQueueTemplate"
- class="org.springframework.jms.core.JmsTemplate">
- <property name="connectionFactory" ref="connectionFactory" />
- <property name="defaultDestination" ref="queueDest" />
- <!--<property name="receiveTimeout" value="10000" /> -->
- </bean>
- <!-- 发送消息的目的地(主题) -->
- <bean id="topicDest"
- class="org.apache.activemq.command.ActiveMQTopic">
- <!-- 设置消息队列的名字 -->
- <constructor-arg index="0" value="myTopic" />
- </bean>
- <!-- 配置TopicJms模板 -->
- <bean id="jmsTopicTemplate"
- class="org.springframework.jms.core.JmsTemplate">
- <property name="connectionFactory" ref="connectionFactory" />
- <property name="defaultDestination" ref="topicDest" />
- <!-- 配置是否为发布订阅者模式,默认为false -->
- <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" />
- </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"/>
- </bean>
- <bean id="topicSubscriberService" class="jms.mq.spring.TopicSubscriberService">
- <property name="jmsTemplate" ref="jmsTopicTemplate"/>
- <property name="destination" ref="topicDest"/>
- </bean>
- </beans>