ActiveMQ —— Spring 整合 ActiveMQ
前文
消息中间件 —— 简介
ActiveMQ 下载、安装
ActiveMQ —— Java 连接 ActiveMQ(点对点)
ActiveMQ —— Java 连接 ActiveMQ(发布订阅 Topic)
ActiveMQ —— Broker
文章目录
- 前文
- 添加依赖
- Spring 配置文件
- 队列
- 生产者
- 消费者
- 主题
- 修改 applicationContext.xml 文件
- 在 Spring 里面实现消费者不启动,直接通过配置监听完成
添加依赖
<dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.9.5</version>
</dependency><!-- ActiveMQ 对 JMS 支持,整合 Spring 和 ActiveMQ -->
<dependency><groupId>org.springframework</groupId><artifactId>spring-jms</artifactId><version>4.3.23.RELEASE</version>
</dependency><!-- ActiveMQ 所需要的 pool 包配置 -->
<dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-pool</artifactId><version>5.15.9</version>
</dependency><!-- Spring AOP 等相关 Jar 包 -->
<dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>4.3.25.RELEASE</version>
</dependency>
这里的 Spring-core 有版本问题,使用 5.0+ 的整合 ActiveMQ 会报错,使用 4.0 + 的就不会
Spring 配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/beans"><!-- 开启包的自动扫描 --><context:component-scan base-package="com.java.elasticsearch.activemq"/><!-- 配置生产者 --><bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop"><property name="connectionFactory"><!-- 真正可以产生 Connection 的 ConnectionFactory,由对应的 JMS 服务厂商提供 --><bean class="org.apache.activemq.ActiveMQConnectionFactory"><property name="brokerURL" value="tcp://localhost:61616"/></bean></property><!-- 最大连接数 --><property name="maxConnections" value="100"/></bean><!-- 这个是队列的目的地,点对点的 --><bean id="destinationQueue" class="org.apache.activemq.command.ActiveMQQueue"><!-- 队列名称 --><constructor-arg index="0" value="spring-active-queue"/></bean><!-- Spring 提供的 JMS 工具类,它可以进行消息发送,接收等 --><bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"><property name="connectionFactory" ref="jmsFactory"/><property name="defaultDestination" ref="destinationQueue"/><property name="messageConverter"><bean class="org.springframework.jms.support.converter.SimpleMessageConverter"/></property></bean></beans>
队列
生产者
package com.java.elasticsearch.activemq.service;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;/*** @author Woo_home* @create 2020/5/22 14:09*/@Service
public class SpringMQ_Produce {@Autowiredprivate JmsTemplate jmsTemplate;public static void main(String[] args){ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");// 相当于 new 一个 SpringMQ_Produce,但是这里用的是 Spring,所以这里就不用 new 了SpringMQ_Produce produce = (SpringMQ_Produce) ctx.getBean("springMQ_Produce");// 普通写法/*produce.jmsTemplate.send(new MessageCreator() {@Overridepublic Message createMessage(Session session) throws JMSException {TextMessage textMessage = session.createTextMessage("****** Spring 整合 ActiveMQ ******");return textMessage;}});*/// lambda 写法produce.jmsTemplate.send((session) -> {TextMessage textMessage = session.createTextMessage("****** Spring 整合 ActiveMQ ******");return textMessage;});}
}
执行程序之前先启动 ActiveMQ
执行主程序
刷新 admin 页面,队列已成功添加进来
消费者
消费者端代码非常简单,只需要接收就行了
package com.java.elasticsearch.activemq.service;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;
import javax.jms.TextMessage;/*** @author Woo_home* @create 2020/5/22 14:09*/@Service
public class SpringMQ_Consumer {@Autowiredprivate JmsTemplate jmsTemplate;public static void main(String[] args){ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");// 相当于 new 一个 SpringMQ_Consumer,但是这里用的是 Spring,所以这里就不用 new 了SpringMQ_Consumer consumer = (SpringMQ_Consumer) ctx.getBean("springMQ_Consumer");String retValue = (String) consumer.jmsTemplate.receiveAndConvert();System.out.println("****** 消费者收到消息 : " + retValue);}
}
执行程序
刷新 admin 页面
主题
修改 applicationContext.xml 文件
只需要将以下三处修改为 Topic 即可
生产者与消费者的代码不用修改,直接运行,要是你想修改的话,可以修改如下内容
因为这里使用的是 Topic,我们先启动消费端,此时的消费端还没收到信息
再启动生产端
此时,消费端接收到生产者发来的信息
在 Spring 里面实现消费者不启动,直接通过配置监听完成
修改 applicationContext.xml 文件
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/beans"><!-- 开启包的自动扫描 --><context:component-scan base-package="com.java.elasticsearch.activemq"/><!-- 配置生产者 --><bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop"><property name="connectionFactory"><!-- 真正可以产生 Connection 的 ConnectionFactory,由对应的 JMS 服务厂商提供 --><bean class="org.apache.activemq.ActiveMQConnectionFactory"><property name="brokerURL" value="tcp://localhost:61616"/></bean></property><!-- 最大连接数 --><property name="maxConnections" value="100"/></bean><!-- 这个是队列的目的地,点对点的 --><bean id="destinationQueue" class="org.apache.activemq.command.ActiveMQQueue"><!-- 队列名称 --><constructor-arg index="0" value="spring-active-queue"/></bean><!-- 这个是队列的目的地,主题的 --><bean id="destinationTopic" class="org.apache.activemq.command.ActiveMQTopic"><!-- 队列名称 --><constructor-arg index="0" value="spring-active-topic"/></bean><!-- Spring 提供的 JMS 工具类,它可以进行消息发送,接收等 --><bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"><property name="connectionFactory" ref="jmsFactory"/><property name="defaultDestination" ref="destinationTopic"/><property name="messageConverter"><bean class="org.springframework.jms.support.converter.SimpleMessageConverter"/></property></bean><!-- 配置监听程序 --><bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"><property name="connectionFactory" ref="jmsFactory"/><property name="destination" ref="destinationTopic"/><!-- public class MyMessageListener implements MessageListener {} --><property name="messageListener" ref="myMessageListener"/></bean></beans>
配置监听
这里还需要一个组件
package com.java.elasticsearch.activemq.service;import org.springframework.stereotype.Component;import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;/*** @author Woo_home* @create 2020/5/22 14:59*/@Component
public class MyMessageListener implements MessageListener {@Overridepublic void onMessage(Message message) {if (null != message && message instanceof TextMessage) {TextMessage textMessage = (TextMessage) message;try {System.out.println(textMessage.getText());} catch (JMSException e) {e.printStackTrace();}}}
}
先删除 Topic
启动生产者,可以发现,消费者马上就可以监听到消费者发送的消息
完整代码已上传至码云 代码下载地址
ActiveMQ —— Spring 整合 ActiveMQ相关推荐
- Spring整合ActiveMQ完成消息队列MQ编程
<–start–> 第一步:新建一个maven,将工程命名为activeMQ_spring.在pom.xml文件中导入相关jar包. ①spring开发和测试相关的jar包: spring ...
- java 消息队列详解_Java消息队列-Spring整合ActiveMq的详解
本篇文章主要介绍了详解Java消息队列-Spring整合ActiveMq ,小编觉得挺不错的,现在分享给大家,也给大家做个参考.一起跟随小编过来看看吧 1.概述 首先和大家一起回顾一下Java 消息服 ...
- activimq java集成_Java消息队列-Spring整合ActiveMq
1.概述 首先和大家一起回顾一下Java 消息服务,在我之前的博客<Java消息队列-JMS概述>中,我为大家分析了: 消息服务:一个中间件,用于解决两个活多个程序之间的耦合,底层由Jav ...
- Spring整合Activemq
目录 一.Pom.xml 二.Spring配置文件 三.队列 四.主题 一.Pom.xml <dependencies><!-- activemq核心依赖包 --><de ...
- Java消息队列-Spring整合ActiveMq
1.概述 首先和大家一起回顾一下Java 消息服务,在我之前的博客<Java消息队列-JMS概述>中,我为大家分析了: 消息服务:一个中间件,用于解决两个或多个程序之间的耦合,底层由Jav ...
- ActiveMQ学习总结(3)——spring整合ActiveMQ
2019独角兽企业重金招聘Python工程师标准>>> 1.参考文献 Spring集成ActiveMQ配置 Spring JMS异步发收消息 ActiveMQ 2.环境 在前面的一篇 ...
- Spring整合ActiveMQ接收消息
操作步骤 第一步:把Activemq相关的jar包,添加到工程中 第二步:创建一个MessageListener的实现类,负责监听 第三步:配置MessageListener监听器 第四步:初始化Sp ...
- Activemq -- Spring 整合
转自: https://www.cnblogs.com/jaycekon/p/ActiveMq.html 转载于:https://www.cnblogs.com/Jomini/p/9630352.ht ...
- spring整合activeMQ遇到异常:Error creating bean with name 'connectionFactory'
异常详情 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'connect ...
最新文章
- 计算机底层书籍三件套--大话计算机
- js小记 function 的 length 属性
- InnoDB的ib_logfile写入策略
- 关于 Http 协议,你必须要知道的
- BeetleX之XRPC远程委托调用
- I. Intersections
- VS插件的开发 - Visual Studio Addin
- 使用wireshark抓取3G包
- 【C语言】指针进阶 - 指针数组 数组指针 数组指针传参 函数指针 指向函数指针数组的指针
- 对称加密和非对称加密的理解
- 【Qt串口波形绘图】基于QCustomPlot的串口波形绘图上位机,源码开放
- caffe---之scale层
- 数据-第14课-栈的定义及实现
- excel离散度图表怎么算_怎么在excel中计算散点图的公式
- 解决虹软人脸识别打开摄像头黑屏的BUG
- HTML5制作诗歌锦集,【热门】诗歌作文锦集六篇
- docker stop 失败处理方法
- 使用echarts画日历热力图
- NR 5G 5G-GUTI解读
- STM32单片机初学心得