1、環境的搭建web
(1)導入依賴spring
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)建立配置文件apache
一、點對點模式配置文件session
applicationContext-jms-producer-queue.xmlmvc
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.xmlapp
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>
二、發佈/訂閱模式tcp
applicationContext-jms-producer-topic.xmlide
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>
2、點對點模式測試
一、建立消息生產者
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 }
二、編寫監聽類
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 }
三、建立測試類
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 }
3、發佈/訂閱模式
一、建立消息生產者
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 }
二、監聽類同點對點
三、建立消費者測試類
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 }
測試:同時運行三個消費者工程,在運行生產者工程,查看三個消費者工程的控制檯輸出。