通過上一節的訊息中介軟體學習筆記六 – Spring JMS理論的學習,知道spring是如何整合了jms的,本節中我們將學習如何在spring中使用jms。 使用idea建立一個maven項目 引入依賴
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.winter.jms</groupId> <artifactId>spring-jms</artifactId> <version>1.0-SNAPSHOT</version> <properties> <spring.version>4.2.5.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-core</artifactId> <version>5.7.0</version> <exclusions> <exclusion><!-- 因為我們自己已經引用了這個包,並且這個包中也附帶了這個包,所以需要排除掉 --> <artifactId>spring-context</artifactId> <groupId>org.springframework</groupId> </exclusion> </exclusions> </dependency> </dependencies></project>項目結構
建立配置spring配置: producer.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 開啟註解的支援 --> <context:annotation-config/> <!-- ActiveMQ為我們提供的ConnectionFactory --> <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://127.0.0.1:61616" /> </bean> <!-- spring jms 為我們提供的串連池 --> <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"> <property name="targetConnectionFactory" ref="targetConnectionFactory"/> </bean> <!-- 一個隊列的目的地,點對點 --> <bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg value="queue"/> </bean> <!-- 配置JMSTemplate,用於發送訊息 --> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="connectionFactory"/> </bean> <bean class="com.winter.jms.producer.impl.ProducerServiceImpl"/></beans>
隊列模式 建立生產者服務
介面
package com.winter.jms.producer;/** * * Created by Administrator on 2017/11/2. */public interface ProducerService { void sendMessage(String message);}實現
package com.winter.jms.producer.impl;import com.winter.jms.producer.ProducerService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jms.core.JmsTemplate;import org.springframework.jms.core.MessageCreator;import javax.annotation.Resource;import javax.jms.*;/** * * Created by Administrator on 2017/11/2. */public class ProducerServiceImpl implements ProducerService { @Autowired JmsTemplate jmsTemplate; @Resource(name = "queueDestination") Destination destination; public void sendMessage(final String message) { jmsTemplate.send(destination, new MessageCreator() { public Message createMessage(Session session) throws JMSException { TextMessage textMessage = session.createTextMessage(message); System.out.println("發送訊息 = [" + textMessage.getText() + "]"); return textMessage; } }); }}啟動類
package com.winter.jms.producer;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * 生產者服務的啟動類 * Created by Administrator on 2017/11/2. */public class AppProducer { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("producer.xml"); ProducerService service = context.getBean(ProducerService.class); for (int i = 0; i < 100; i++) { service.sendMessage("test" + i); } context.close(); }}測試 開啟ActiveMQ的後台:http://localhost:8161/ 啟動剛才建立的生產者啟動類中的”main”方法
即可看到正常的輸出了。 建立消費者服務
建立新的包:consumer
現在看一下項目的結構:
建立新的spring設定檔:consumer.xml
由於一些配置兩個設定檔有公用的部分,所以我們添加了一個新的設定檔:common.xml 用於存放兩個設定檔中相同的配置,看一下更改之後的三個設定檔的內容吧
common.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 開啟註解的支援 --> <context:annotation-config/> <!-- ActiveMQ為我們提供的ConnectionFactory --> <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://127.0.0.1:61616" /> </bean> <!-- spring jms 為我們提供的串連池 --> <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"> <property name="targetConnectionFactory" ref="targetConnectionFactory"/> </bean> <!-- 一個隊列的目的地,點對點 --> <bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg value="queue"/> </bean></beans>
producer.xml
<?xml version="1.0" encoding="UTF-8"?><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"> <!-- 匯入公用配置 --> <import resource="common.xml"/> <!-- 配置JMSTemplate,用於發送訊息 --> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="connectionFactory"/> </bean> <bean class="com.winter.jms.producer.impl.ProducerServiceImpl"/></beans>
consumer.xml
<?xml version="1.0" encoding="UTF-8"?><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"> <!-- 匯入公用配置 --> <import resource="common.xml"/> <!-- 配置訊息監聽器 --> <bean id="consumerMessageListener" class="com.winter.jms.consumer.ConsumerMessageListener"/> <!-- 配置訊息監聽容器 --> <bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory"/> <!-- 訊息監聽地址 --> <property name="destination" ref="queueDestination"/> <!-- 訊息監聽器 --> <property name="messageListener" ref="consumerMessageListener"/> </bean></beans>
消費者
消費服務監聽器
ConsumerMessageListener
package com.winter.jms.consumer;import javax.jms.JMSException;import javax.jms.Message;import javax.jms.MessageListener;import javax.jms.TextMessage;/** * 訊息監聽器 * Created by Administrator on 2017/11/2. */public class ConsumerMessageListener implements MessageListener { public void onMessage(Message message) { TextMessage textMessage = (TextMessage) message; try { System.out.println("接收message: " + textMessage.getText()); } catch (JMSException e) { e.printStackTrace(); } }}
消費監聽器啟動類
AppConsumer
package com.winter.jms.consumer;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * 消費者啟動類 * Created by Administrator on 2017/11/2. */public class AppConsumer { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml"); }}測試
啟動”main”方法即可看到訊息被消費了,查看ActiveMQ後台可看到。由於之前我生產者啟動了兩次,導致生產了200條訊息,現在已經被全部消費掉了
如果我們同時啟動兩個消費者,會出現什麼樣的現象呢。
然後我們再啟動一個生產者。
可以看到控制台列印出的資訊:
消費者A(AppConsumer):
接收message: test0接收message: test2接收message: test4接收message: test6接收message: test8接收message: test10接收message: test12接收message: test14接收message: test16接收message: test18接收message: test20接收message: test22接收message: test24接收message: test26...
消費者B(AppConsumer):
接收message: test1接收message: test3接收message: test5接收message: test7接收message: test9接收message: test11接收message: test13接收message: test15接收message: test17接收message: test19接收message: test21接收message: test23接收message: test25接收message: test27...
可以看出隊列模式是平均消費的,被消費者A消費的訊息不能再被消費者B消費 主題模式 配置 在common.xml中加入:
<!-- 一個主題目的地,發布訂閱模式 --> <bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic"> <constructor-arg value="topic"/> </bean>
將 ProducerServiceImpl 中的:
@Resource(name = "queueDestination") Destination destination;
改成
@Resource(name = "topicDestination") Destination destination;
修改consumer.xml:
修改
<!-- 訊息監聽地址 --> <property name="destination" ref="queueDestination"/>
修改成:
<!-- 訊息監聽地址 --> <property name="destination" ref="topicDestination"/>
只需要三步就可以將 隊列模式 改成 主題模式 了。 測試:
啟動兩個消費者 AppConsumer, 然後啟動一個生產者 Producer 可以看到輸出了:
消費者A:
接收message: test0接收message: test1接收message: test2接收message: test3接收message: test4接收message: test5接收message: test6接收message: test7接收message: test8接收message: test9接收message: test10接收message: test11接收message: test12接收