Spring整合JMS

 一、环境的搭建

(1)导入依赖

 1     <properties>
 2         <junit.version>4.12junit.version>
 3         <spring.version>4.3.10.RELEASEspring.version>
 4     properties>
 5 
 6     <dependencies>
 7         <dependency>
 8             <groupId>junitgroupId>
 9             <artifactId>junitartifactId>
10             <version>${junit.version}version>
11 
12             <scope>testscope>
13         dependency>
14 
15         
16         <dependency>
17             <groupId>org.springframeworkgroupId>
18             <artifactId>spring-contextartifactId>
19             <version>${spring.version}version>
20         dependency>
21         <dependency>
22             <groupId>org.springframeworkgroupId>
23             <artifactId>spring-beansartifactId>
24             <version>${spring.version}version>
25         dependency>
26         <dependency>
27             <groupId>org.springframeworkgroupId>
28             <artifactId>spring-webmvcartifactId>
29             <version>${spring.version}version>
30         dependency>
31         <dependency>
32             <groupId>org.springframeworkgroupId>
33             <artifactId>spring-jdbcartifactId>
34             <version>${spring.version}version>
35         dependency>
36         <dependency>
37             <groupId>org.springframeworkgroupId>
38             <artifactId>spring-aspectsartifactId>
39             <version>${spring.version}version>
40         dependency>
41         <dependency>
42             <groupId>org.springframeworkgroupId>
43             <artifactId>spring-jmsartifactId>
44             <version>${spring.version}version>
45         dependency>
46         <dependency>
47             <groupId>org.springframeworkgroupId>
48             <artifactId>spring-context-supportartifactId>
49             <version>${spring.version}version>
50         dependency>
51         <dependency>
52             <groupId>org.springframeworkgroupId>
53             <artifactId>spring-testartifactId>
54             <version>${spring.version}version>
55         dependency>
56         
57         <dependency>
58             <groupId>org.springframeworkgroupId>
59             <artifactId>spring-jmsartifactId>
60             <version>${spring.version}version>
61         dependency>
62 
63         
64         <dependency>
65             <groupId>org.apache.activemqgroupId>
66             <artifactId>activemq-clientartifactId>
67             <version>5.13.4version>
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     
11     <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
12         <property name="brokerURL" value="tcp://127.0.0.1:61616"/>
13     bean>
14     
15     <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
16         
17         <property name="targetConnectionFactory" ref="targetConnectionFactory"/>
18     bean>
19     
20     <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
21         
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     
10     <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
11         <property name="brokerURL" value="tcp://127.0.0.1:61616"/>
12     bean>
13     
14     <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
15         
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     
13     <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
14         <property name="brokerURL" value="tcp://127.0.0.1:61616"/>
15     bean>
16     
17     <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
18         
19         <property name="targetConnectionFactory" ref="targetConnectionFactory"/>
20     bean>
21     
22     <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
23         
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     
 5     <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
 6         <property name="brokerURL" value="tcp://127.0.0.1:61616"/>
 7     bean>
 8     
 9     <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
10         
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)