ActiveMQ整合Spring入门用法解析
#代码知识 发布时间: 2026-01-12
一.ActiveMQ整合Spring基础

ActiveMQ和Spring的整合,其实是把activemq的一些对象交给spring来管理,比如连接工厂,queue,top等等
二.依赖
除了activemq本身提供的jar包外,还需要两个spring整合activemq的jar:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<!-- activemq -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
</dependency>
三.在spring配置文件中,配置activemq相关的对象
》生产者需要配置的对象:activemq原生连接工厂,spring包装过的工厂,JmsTemplate(生产者),queue,topic
<!-- 配置activemq的连接工厂 --> <bean name="activemqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <constructor-arg index="0" value="tcp://192.168.xx.xxx:61616"></constructor-arg> </bean> <!-- 配置spring包装的连接工厂 --> <bean name="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"> <!-- 设置activemq的连接工厂 --> <property name="targetConnectionFactory" ref="activemqConnectionFactory"></property> </bean> <!-- 配置生产者对象:jmstemplate --> <bean name="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <!-- 设置工厂 --> <!-- <constructor-arg name="connectionFactory" ref="connectionFactory"></constructor-arg> --> <property name="connectionFactory" ref="connectionFactory"></property> </bean> <!-- 配置queue对象 --> <bean name="queueDestination" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg name="name" value="spring-active-queue"></constructor-arg> </bean> <!-- 配置topic对象 --> <bean name="topicDestination" class="org.apache.activemq.command.ActiveMQTopic"> <constructor-arg name="name" value="spring-active-topic"></constructor-arg> </bean>
测试代码:
//测试生产者发生queue
@Test
public void testJMSTemplateByQueue() throws Exception {
//创建spring容器
ApplicationContext ioc = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-activemq.xml");
//获取jmstemplate
JmsTemplate jmsTemplate = ioc.getBean(JmsTemplate.class);
//获取目的对象
Destination queue = (Destination) ioc.getBean("queueDestination");
//发送消息,并设置目的对象和消息
jmsTemplate.send(queue,new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
TextMessage message = session.createTextMessage();
message.setText("hello spring activemq");
return message;
}
});
小结:生产者是需要在特定的时间发送指定的消息内容,因此是需要书写代码的
》消费者需要配置的对象:activemq原生连接工厂,spring包装过的工厂,监听容器(消费者),监听器,queue,topic
》之前监听器我们是直接实现匿名内部类的方式,但是在spring这就不能了,还是书写一个实现类并继承messageListener接口
<!-- 配置activemq的连接工厂 --> <bean name="activemqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <constructor-arg index="0" value="tcp://192.168.xx.xxx:61616"></constructor-arg> </bean> <!-- 配置spring包装的连接工厂 --> <bean name="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"> <!-- 设置activemq的连接工厂 --> <property name="targetConnectionFactory" ref="activemqConnectionFactory"></property> </bean> <!-- 配置监听器 --> <bean name="myMessageListener" class="cn.e3mall.search.listener.MyMessageListener"></bean> <!-- 配置监听容器:消费者 --> <bean name="messageListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <!-- 设置连接工厂 --> <property name="connectionFactory" ref="connectionFactory"></property> <!-- 设置目的对象 --> <property name="destination" ref="queueDestination"></property> <!-- 设置监听器 --> <property name="messageListener" ref="myMessageListener"></property> </bean> <!-- 配置queue对象 --> <bean name="queueDestination" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg name="name" value="spring-active-queue"></constructor-arg> </bean> <!-- 配置topic对象 --> <bean name="topicDestination" class="org.apache.activemq.command.ActiveMQTopic"> <constructor-arg name="name" value="spring-active-topic"></constructor-arg> </bean>
测试代码:
@Test
public void testListener() throws IOException {
ApplicationContext ioc = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-activemq.xml");
System.in.read();
}
小结:我们的测试代码很简单,只要把容器启动了,监听器就会一致监听着。不需要写任何代码,监听器只要一直关注目的对象Destination是否有消息发送过来
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
代码知识SEO上一篇 : SpringBoot读写xml上传到AWS存储服务S3的示例
下一篇 : JVM内存区域划分相关原理详解
-
SEO外包最佳选择国内专业的白帽SEO机构,熟知搜索算法,各行业企业站优化策略!
SEO公司
-
可定制SEO优化套餐基于整站优化与品牌搜索展现,定制个性化营销推广方案!
SEO套餐
-
SEO入门教程多年积累SEO实战案例,从新手到专家,从入门到精通,海量的SEO学习资料!
SEO教程
-
SEO项目资源高质量SEO项目资源,稀缺性外链,优质文案代写,老域名提权,云主机相关配置折扣!
SEO资源
-
SEO快速建站快速搭建符合搜索引擎友好的企业网站,协助备案,域名选择,服务器配置等相关服务!
SEO建站
-
快速搜索引擎优化建议没有任何SEO机构,可以承诺搜索引擎排名的具体位置,如果有,那么请您多注意!专业的SEO机构,一般情况下只能确保目标关键词进入到首页或者前几页,如果您有相关问题,欢迎咨询!