接着上一篇的博客,這一篇寫分佈訂閱模式:
2.發佈/訂閱模式
2.1消息的發送者
(1)在工程 springjms_producer 的 applicationContext-jms-producer.xml 增加配置
<!--這個是訂閱模式 文本信息-->
<bean id="activeMQTopic" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg value="topic"/>
</bean>
(2)創建生產者類
package com.study.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
@Component
public class TopicProducer {
@Autowired
private JmsTemplate jmsTemplate;
@Autowired
private Destination activeMQTopic;
/**
* 發送文本消息
* @param text
*/
public void sendTextMessage(final String text){
jmsTemplate.send(activeMQTopic, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage(text);
}
});
}
}
(3)編寫測試類
import com.study.demo.TopicProducer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext-jms-producer.xml")
public class TestTopic {
@Autowired
private TopicProducer topicProducer;
@Test
public void sendTextQueue() {
topicProducer.sendTextMessage("發佈訂閱模式生產者發送消息");
}
}
2.2消息的消費者
1)在 activemq-spring-consumer 工 程 中 創 建 配 置 文 件applicationContext-jms-consumer-topic.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:amq="http://activemq.apache.org/schema/core"
xmlns:jms="http://www.springframework.org/schema/jms"
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">
<!-- 真正可以產生 Connection 的 ConnectionFactory,由對應的 JMS 服務廠商提供-->
<bean id="targetConnectionFactory"
class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://192.168.25.128:61616"/>
</bean>
<!-- Spring 用於管理真正的 ConnectionFactory 的 ConnectionFactory -->
<bean id="connectionFactory"
class="org.springframework.jms.connection.SingleConnectionFactory">
<!-- 目標 ConnectionFactory 對應真實的可以產生 JMS Connection 的 ConnectionFactory -->
<property name="targetConnectionFactory" ref="targetConnectionFactory"/>
</bean>
<!--這個是隊列目的地,發佈訂閱模式的文本信息-->
<bean id="ActiveMQTopic"
class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg value="topic"/>
</bean>
<!-- 我的監聽類 -->
<bean id="myMessageListener" class="com.study.demo.MyMessageListener"></bean>
<!-- 消息監聽容器 -->
<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="destination" ref="ActiveMQTopic"/>
<property name="messageListener" ref="myMessageListener"/>
</bean>
</beans>
(2)編寫測試類
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.io.IOException;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext-jms-consumer-topic.xml")
public class TestTopic {
@Test
public void testTopic() {
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
}
}
這只是其中一個測試類,爲了演示更好的效果,我們需要在複製兩份,改個測試類名字就可以使用了,通過測試發現我們需要先啓動消費者才能接收到消息,也就是說發佈訂閱模式需要生產者和消費者同時在線才能接收到消息,類似與我們生活中的廣播機制,然後運行多個消費者測試類會發現每個消費者都會接收到生產者發送的消息,運行效果圖如下:
下一篇博客寫寫全文檢索技術solr.