Spring整合JMS

Posted gdwkong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring整合JMS相关的知识,希望对你有一定的参考价值。

 一、环境的搭建

(1)导入依赖

 1     <properties>
 2         <junit.version>4.12</junit.version>
 3         <spring.version>4.3.10.RELEASE</spring.version>
 4     </properties>
 5 
 6     <dependencies>
 7         <dependency>
 8             <groupId>junit</groupId>
 9             <artifactId>junit</artifactId>
10             <version>${junit.version}</version>
11 
12             <scope>test</scope>
13         </dependency>
14 
15         <!-- Spring -->
16         <dependency>
17             <groupId>org.springframework</groupId>
18             <artifactId>spring-context</artifactId>
19             <version>${spring.version}</version>
20         </dependency>
21         <dependency>
22             <groupId>org.springframework</groupId>
23             <artifactId>spring-beans</artifactId>
24             <version>${spring.version}</version>
25         </dependency>
26         <dependency>
27             <groupId>org.springframework</groupId>
28             <artifactId>spring-webmvc</artifactId>
29             <version>${spring.version}</version>
30         </dependency>
31         <dependency>
32             <groupId>org.springframework</groupId>
33             <artifactId>spring-jdbc</artifactId>
34             <version>${spring.version}</version>
35         </dependency>
36         <dependency>
37             <groupId>org.springframework</groupId>
38             <artifactId>spring-aspects</artifactId>
39             <version>${spring.version}</version>
40         </dependency>
41         <dependency>
42             <groupId>org.springframework</groupId>
43             <artifactId>spring-jms</artifactId>
44             <version>${spring.version}</version>
45         </dependency>
46         <dependency>
47             <groupId>org.springframework</groupId>
48             <artifactId>spring-context-support</artifactId>
49             <version>${spring.version}</version>
50         </dependency>
51         <dependency>
52             <groupId>org.springframework</groupId>
53             <artifactId>spring-test</artifactId>
54             <version>${spring.version}</version>
55         </dependency>
56         <!--activemq整合包-->
57         <dependency>
58             <groupId>org.springframework</groupId>
59             <artifactId>spring-jms</artifactId>
60             <version>${spring.version}</version>
61         </dependency>
62 
63         <!--activemq 依赖包-->
64         <dependency>
65             <groupId>org.apache.activemq</groupId>
66             <artifactId>activemq-client</artifactId>
67             <version>5.13.4</version>
68         </dependency>
69     </dependencies>

(2)创建配置文件

1、点对点模式配置文件

applicationContext-jms-producer-queue.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5 
 6        xsi:schemaLocation="http://www.springframework.org/schema/beans
 7        http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
 8 
 9     <context:component-scan base-package="com.cenobitor.demo"/>
10     <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供-->
11     <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
12         <property name="brokerURL" value="tcp://127.0.0.1:61616"/>
13     </bean>
14     <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
15     <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
16         <!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
17         <property name="targetConnectionFactory" ref="targetConnectionFactory"/>
18     </bean>
19     <!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 -->
20     <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
21         <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
22         <property name="connectionFactory" ref="connectionFactory"/>
23     </bean>
24     <!--这个是队列目的地,点对点的  文本信息-->
25    <bean id="queueTextDestination" class="org.apache.activemq.command.ActiveMQQueue">
26         <constructor-arg value="queue_text"/>
27     </bean>
28 </beans>

applicationContext-jms-consumer-queue.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5 
 6        xsi:schemaLocation="http://www.springframework.org/schema/beans
 7        http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
 8 
 9     <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供-->
10     <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
11         <property name="brokerURL" value="tcp://127.0.0.1:61616"/>
12     </bean>
13     <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
14     <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
15         <!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
16         <property name="targetConnectionFactory" ref="targetConnectionFactory"/>
17     </bean>
18 
19     <!--这个是队列目的地,点对点的  文本信息 与生产者中相应的配置一致-->
20     <bean id="queueTextDestination" class="org.apache.activemq.command.ActiveMQQueue">
21         <constructor-arg value="queue_text"/>
22     </bean>
23 
24     <!--我的监听类-->
25     <bean id="myMessageListener" class="com.cenobitor.demo.MyMessageListener"></bean>
26     <!--监听容器-->
27     <bean  class="org.springframework.jms.listener.DefaultMessageListenerContainer">
28         <property name="connectionFactory" ref="targetConnectionFactory"/>
29         <property name="destination" ref="queueTextDestination"/>
30         <property name="messageListener" ref="myMessageListener"/>
31     </bean>
32 </beans>

2、发布/订阅模式

applicationContext-jms-producer-topic.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xsi:schemaLocation="http://www.springframework.org/schema/beans
 6        http://www.springframework.org/schema/beans/spring-beans.xsd 
 7     http://www.springframework.org/schema/context 
 8     http://www.springframework.org/schema/context/spring-context.xsd">
 9 
10     <context:component-scan base-package="com.cenobitor.demo"/>
11 
12     <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供-->
13     <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
14         <property name="brokerURL" value="tcp://127.0.0.1:61616"/>
15     </bean>
16     <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
17     <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
18         <!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
19         <property name="targetConnectionFactory" ref="targetConnectionFactory"/>
20     </bean>
21     <!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 -->
22     <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
23         <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
24         <property name="connectionFactory" ref="connectionFactory"/>
25     </bean>
26 
27     <!--这个是订阅模式  文本信息-->
28     <bean id="topicTextDestination" class="org.apache.activemq.command.ActiveMQTopic">
29         <constructor-arg value="topic_text"/>
30     </bean>
31 </beans>

applicationContext-jms-consumer-topic.xml

 1 <beans xmlns="http://www.springframework.org/schema/beans"
 2        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">
 3 
 4     <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供-->
 5     <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
 6         <property name="brokerURL" value="tcp://127.0.0.1:61616"/>
 7     </bean>
 8     <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
 9     <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
10         <!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
11         <property name="targetConnectionFactory" ref="targetConnectionFactory"/>
12     </bean>
13 
14     <!--这个是队列目的地,点对点的  文本信息-->
15     <bean id="topicTextDestination" class="org.apache.activemq.command.ActiveMQTopic">
16         <constructor-arg value="topic_text"/>
17     </bean>
18 
19     <!--我的监听类-->
20     <bean id="myMessageListener" class="com.cenobitor.demo.MyMessageListener"></bean>
21     <!--监听容器-->
22     <bean  class="org.springframework.jms.listener.DefaultMessageListenerContainer">
23         <property name="connectionFactory" ref="targetConnectionFactory"/>
24         <property name="destination" ref="topicTextDestination"/>
25         <property name="messageListener" ref="myMessageListener"/>
26     </bean>
27 
28 </beans>

二、点对点模式

1、创建消息生产者

 1 @Component
 2 public class QueueProducer {
 3 
 4     @Autowired
 5     private JmsTemplate jmsTemplate;
 6 
 7     @Autowired
 8     private Destination destination;
 9 
10     public void sendTextMessage(final String text){
11         jmsTemplate.send(destination, new MessageCreator() {
12             @Override
13             public Message createMessage(Session session) throws JMSException {
14                 return session.createTextMessage(text);
15             }
16         });
17     }
18 }

2、编写监听类

 1 public class MyMessageListener implements MessageListener {
 2 
 3     @Override
 4     public void onMessage(Message message) {
 5         TextMessage textMessage = (TextMessage) message;
 6         try {
 7             System.out.println("接收到消息:"+textMessage.getText());
 8         } catch (JMSException e) {
 9             e.printStackTrace();
10         }
11     }
12 }

3、创建测试类

1 @RunWith(SpringRunner.class)
2 @ContextConfiguration("classpath:applicationContext-jms-consumer-queue.xml")
3 public class TestQueue {
4 
5     @Test
6     public void onMessage() throws IOException {
7         System.in.read();
8     }
9 }

三、发布/订阅模式

1、创建消息生产者

 1 @Component
 2 public class TopicProducer {
 3     @Autowired
 4     private JmsTemplate jmsTemplate;
 5 
 6     @Autowired
 7     private Destination topicTextDestination;
 8 
 9     public void sendTextMessage(final String text){
10         //此方法等同于下面被注释部分
11         jmsTemplate.convertAndSend(topicTextDestination,text);
12 
13         /*jmsTemplate.send(topicTextDestination, new MessageCreator() {
14                 @Override
15                 public Message createMessage(Session session) throws JMSException {
16                      return session.createTextMessage(text);
17                 }
18           });*/
19     }
20 }

2、监听类同点对点

3、创建消费者测试类

1 @RunWith(SpringRunner.class)
2 @ContextConfiguration("classpath:applicationContext-jms-consumer-topic.xml")
3 public class TestTopic {
4 
5     @Test
6     public void onMessage() throws IOException {
7         System.in.read();
8     }
9 }
测试:同时运行三个消费者工程,在运行生产者工程,查看三个消费者工程的控制台输出。

以上是关于Spring整合JMS的主要内容,如果未能解决你的问题,请参考以下文章

Spring整合JMS

Spring整合JMS——MessageConverter介绍

JMS(java消息服务)整合Spring项目案例

Spring整合JMS——事务管理

Spring整合JMS——三种connectionFactory

JMS 之 Active MQ 的spring整合