引入相关的jar包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!-- activeMQ -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.11.2</version>
</dependency>
消息生产者的配置与使用
Spring配置文件
1.配置ConnectionFactory
<!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 -->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://192.168.25.129:61616" />
</bean>
<!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
<bean id="connectionFactory"
class="org.springframework.jms.connection.SingleConnectionFactory">
<!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
<property name="targetConnectionFactory" ref="targetConnectionFactory" />
</bean>
2.配置消息生产者,使用JMSTemplate对象
<!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
<property name="connectionFactory" ref="connectionFactory" />
</bean>
3.配置Destination,可以配置两个,一个是Queue,一个是Topic
<!--这个是队列目的地,点对点的,spring-queue就是队列的名称 -->
<bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg>
<value>spring-queue</value>
</constructor-arg>
</bean>
<!--这个是话题目的地,一对多的,topic就是队列的名称 -->
<bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg value="topic" />
</bean>
4.测试代码
@Test
public void sendMessageTest(){
//因为发送消息使用JmsTemplate模板,所以要从spring容器中得到对象,所以要初始化一个spring容器
ApplicationContext applicationContext = new
ClassPathXmlApplicationContext("classpath:spring/applicationContext-activeMq.xml");
//从spring容器中获得JmsTemplate对象
JmsTemplate jmsTemplate = applicationContext.getBean(JmsTemplate.class);
//从容器中获得一个Destination对象
//根据id从spring容器中取得一个Queue Destination对象
//Destination destination = (Destination) applicationContext.getBean("queueDestination");
//根据id从spring容器中取得一个Topic Destination对象
Destination destination = (Destination) applicationContext.getBean("topicDestination");
//发送消息(这里需要传入一个MessageCreator内部类,在里面可以获得Session对象,通过session对象创建TextMessage)
jmsTemplate.send(destination, new MessageCreator(){
@Override
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage("send activeMq spring");
}
});
}
分别获取Queue Destination对象和Topic Destination执行发送消息之后,就能在ActivaMq的后台管理,看到有名为 spring-queue 的消息队列和 名为 topic 的消息话题,详细内容参考:
https://blog.csdn.net/weixin_43231076/article/details/83217572
消息消费者的配置和使用
消息消费方,需要创建一个MessageListener的实现类,来监听消息的产生
1.配置文件如下:
<!-- 1.配置ConnectionFactory -->
<!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 -->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://192.168.25.129:61616" />
</bean>
<!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
<bean id="connectionFactory"
class="org.springframework.jms.connection.SingleConnectionFactory">
<!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
<property name="targetConnectionFactory" ref="targetConnectionFactory" />
</bean>
<!-- 2.配置Destination,可以配置两个,一个是Queue,一个是Topic -->
<!--这个是队列目的地,点对点的,spring-queue就是队列的名称 -->
<bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg>
<value>spring-queue</value>
</constructor-arg>
</bean>
<!--这个是话题目的地,一对多的,topic就是队列的名称 -->
<bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg value="topic" />
</bean>
<!-- 3.接收消息 -->
<!-- 配置监听器 -->
<bean id="myMessageListener" class="cn.dss.search.listener.MyMessageListener" />
<!-- 消息监听容器 -->
<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<!-- 这里表示引用Destination,可以配置Queue,也可以配置Topic -->
<!-- <property name="destination" ref="queueDestination" /> -->
<property name="destination" ref="topicDestination" />
<property name="messageListener" ref="myMessageListener" /> <!-- 引用监听器 -->
</bean>
2.自定义监听器代码
public class MyMessageListener implements MessageListener {
@Override
public void onMessage(Message message) {
TextMessage textMessage = (TextMessage) message;
//取消息内容
try {
String text = textMessage.getText();
System.out.println(text);
} catch (JMSException e) {
e.printStackTrace();
}
}
}
3.测试代码:
@Test
public void testQueueConsumer() throws Exception {
//初始化spring容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-activeMq.xml");
//等待
System.in.read();
}
当启动消息消费者后,如果不在控制台关闭消费者,它就会一直在等待消息的产生,此时,再使用消息生产的测试方法,消费者就能马上接收到消息,详情参考:https://blog.csdn.net/weixin_43231076/article/details/83217572