接着上一篇的博客,这一篇写分布订阅模式:
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.