ActiveMQ与Spring集成
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ActiveMQ与Spring集成相关的知识,希望对你有一定的参考价值。
声明 转载请注明出处! Reprint please indicate the source!
http://www.hiknowledge.top/2017/04/03/activemq%ef%bc%88%e4%ba%8c%ef%bc%89%e4%b8%8espring%e9%9b%86%e6%88%90/
ActiveMQ与Spring集成
由于历史原因,JMS有4个版本。Spring提供了用于简化JMS API使用的抽象框架,并且对用户屏蔽了JMS API中1.0.2和1.1版本的差异。
JMS的功能大致上分为两块,叫做消息制造和消息消耗。JmsTemplate用于制造消息和同步消息接收。和J2EE的事件驱动Bean风格类似,对于异步接收消息,Spring提供了一些消息监听容器来创建消息驱动的POJO(MDP)。
ActiveMQ与Spring整合需要activemq-spring.jar包
Maven 整合Demo
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jahentao</groupId>
<artifactId>DEMO-ActiveMQ</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>DEMO-ActiveMQ</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--引入的若是activemq-all.jar,且工程中已经引入了SLF4J,会与activemq-all.jar中的SLF4J发生冲突。-->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-core</artifactId>
<version>5.7.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.24</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.24</version>
</dependency>
<!-- 与Spring集成 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-spring</artifactId>
<version>5.14.4</version>
</dependency>
</dependencies>
</project>
配置文件Demo
<?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-2.5.xsd">
<!-- 1.配置connectionFactory -->
<bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop">
<property name="connectionFactory">
<bean class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://192.168.235.100:61616"/>
</bean>
</property>
<property name="maxConnections" value="100"/>
</bean>
<!-- 2.定义消息目标 -->
<bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg index="0" value="queue_temp"/>
</bean>
<!-- 3.配置Spring JMS Template -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<!-- 3.1 注入jmsFactory -->
<property name="connectionFactory" ref="jmsFactory"/>
<!-- 3.2 配置消息目标 -->
<property name="defaultDestination" ref="destination" />
</bean>
<!-- 4.配置生产者 -->
<bean id="springProducer" class="com.jahentao.integration.SpringProducer">
<property name="jmsTemplate" ref="jmsTemplate"/>
</bean>
<!--<bean id="springProducer2" class="com.jahentao.integration.SpringProducer2">-->
<!--<property name="jmsTemplate" ref="jmsTemplate"/>-->
<!--</bean>-->
<!-- 5.配置消费者 -->
<bean id="springConsumer" class="com.jahentao.integration.SpringConsumer">
<property name="jmsTemplate" ref="jmsTemplate"/>
</bean>
<!--<bean id="springConsumer2" class="com.jahentao.integration.SpringConsumer2"/>-->
<!-- 6.消息监听容器 -->
<!--<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsFactory" />
<property name="destination" ref="destination" />
<property name="messageListener" ref="springConsumer2" />
</bean> -->
</beans>
生产者消费者Demo
SpringProducer.java
package com.jahentao.integration;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
/**
* Created with IntelliJ IDEA.<br>
* Description: Spring整合ActiveMQ 生成者<br>
* User: jahen<br>
* Date: 2017-04-03<br>
* Time: 14:42<br>
*/
public class SpringProducer {
//Spring的模板,封装了很多功能
private JmsTemplate jmsTemplate;
public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}
protected void send() {
//使用JMSTemplate可以很简单的实现发送消息
jmsTemplate.send(new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
System.out.println("发送消息: jmsTemplate send messages");
return session.createTextMessage("jmsTemplate send messages");
}
});
}
}
SpringConsumer.java
package com.jahentao.integration;
import org.springframework.jms.core.JmsTemplate;
import javax.jms.TextMessage;
/**
* Created with IntelliJ IDEA.<br>
* Description: Spring整合ActiveMQ 消费者<br>
* User: jahen<br>
* Date: 2017-04-03<br>
* Time: 14:44<br>
*/
public class SpringConsumer {
private JmsTemplate jmsTemplate;
public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}
//接收消息
protected void recive() {
while (true) {
try {
//使用JMSTemplate接收消息
TextMessage txtmsg = (TextMessage) jmsTemplate.receive();
if (null != txtmsg) {
System.out.println("--- 收到消息内容为: " + txtmsg.getText());
} else {
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
JMSTest.java
package com.jahentao.integration;
import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
import org.springframework.context.ApplicationContext;
/**
* Created with IntelliJ IDEA.<br>
* Description: 测试ActiveMQ与Spring集成<br>
* User: jahen<br>
* Date: 2017-04-03<br>
* Time: 15:21<br>
*/
public class JMSTest {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-jms.xml");
SpringProducer producer = (SpringProducer)applicationContext.getBean("springProducer");
producer.send();
SpringConsumer consumer = (SpringConsumer) applicationContext.getBean("springConsumer");
consumer.recive();
}
}
运行结果
消费者配置监听
Spring 配置文件 修改
<!-- ... -->
<!-- 4.配置生产者 -->
<!--<bean id="springProducer" class="com.jahentao.integration.SpringProducer">-->
<!--<property name="jmsTemplate" ref="jmsTemplate"/>-->
<!--</bean>-->
<bean id="springProducer2" class="com.jahentao.integration.SpringProducer2">
<property name="jmsTemplate" ref="jmsTemplate"/>
</bean>
<!-- 5.配置消费者 -->
<!--<bean id="springConsumer" class="com.jahentao.integration.SpringConsumer">-->
<!--<property name="jmsTemplate" ref="jmsTemplate"/>-->
<!--</bean>-->
<bean id="springConsumer2" class="com.jahentao.integration.SpringConsumer2"/>
<!-- 6.消息监听容器 -->
<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsFactory" />
<property name="destination" ref="destination" />
<property name="messageListener" ref="springConsumer2" />
</bean>
<!-- ... -->
SpringConsumer2.java
package com.jahentao.integration;
import org.springframework.jms.core.JmsTemplate;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
/**
* Created with IntelliJ IDEA.<br>
* Description: Spring整合ActiveMQ 消费者<br>
* 配置监听器
* User: jahen<br>
* Date: 2017-04-03<br>
* Time: 14:44<br>
*/
public class SpringConsumer2 implements MessageListener{
@Override
public void onMessage(Message message) {
TextMessage textMsg = (TextMessage) message;
try {
System.out.println("接收到了消息,消息内容是:" + textMsg.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
JMSTest2.java
package com.jahentao.integration;
import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
import org.springframework.context.ApplicationContext;
/**
* Created with IntelliJ IDEA.<br>
* Description: 测试ActiveMQ与Spring集成 消费者监听消息<br>
* User: jahen<br>
* Date: 2017-04-03<br>
* Time: 15:21<br>
*/
public class JMSTest2 {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-jms.xml");
SpringProducer2 producer = (SpringProducer2) applicationContext.getBean("springProducer2");
producer.send("你在哪里啊?");
producer.send("嗨,你好吗?");
// 消费者配置了消息监听
// 接收消息
}
}
运行结果
代码托管在码云上
参考
以上是关于ActiveMQ与Spring集成的主要内容,如果未能解决你的问题,请参考以下文章
Spring Boot学习笔记——Spring Boot与ActiveMQ的集成
干货 | 从零开始学 Java - Spring 集成 ActiveMQ 配置