Spring + ActiveMQ 整合实现点对点(point to point)消息发送案例

devtools/2024/10/18 4:04:07/

本节演示点对点模式的消息发送的 Spring + ActiveMQ 代码。

Spring + ActiveMQ 整合
1、依懒包

spring:4.2.5.RELEASE,activemq-all:5.15.0

<dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.0.1</version><scope>provided</scope></dependency><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-core</artifactId><version>2.9.1</version></dependency><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-api</artifactId><version>2.9.1</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.0</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>4.2.5.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.2.5.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>4.2.5.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>4.2.5.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>4.2.5.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>4.2.5.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>4.2.5.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>4.2.5.RELEASE</version></dependency><dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-all</artifactId><version>5.15.0</version></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId><version>2.4.2</version></dependency>
2、工程介绍
  • producer 生产者工程

  • consumer 消费者工程

3、整合关键类 JmsTemplate

Spring 官方提供了一个 JmsTemplate 的类,这个类就专门用来处理JMS的,在该类的Bean配置标签中有两个属性connectionFactory-ref 和 defaultDestination-ref 正好对应 JMS 中的 ConnectionFactory 和 Destination。

点对点(point to point)消息发送
1、生产者代码
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!-- 创建一个ConnectionFactory,为了提升性能用了连接池 --><bean id="connectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory"destroy-method="stop"><property name="connectionFactory"><bean class="org.apache.activemq.ActiveMQConnectionFactory"><property name="brokerURL"><value>tcp://localhost:61616</value></property></bean></property><property name="maxConnections" value="50" /></bean><!-- 创建消息目的地,constructor-arg是目的地的名称,此处为spring-queue --><bean id="destination" class="org.apache.activemq.command.ActiveMQQueue"><constructor-arg index="0" value="spring-queue" /></bean><!-- 构建JmsTemplate --><bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"><property name="connectionFactory" ref="connectionFactory" /><property name="defaultDestination" ref="destination" /><property name="messageConverter"><beanclass="org.springframework.jms.support.converter.SimpleMessageConverter" /></property></bean></beans>
  • 生产者关键代码:SpringMessageSender

package producer;import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;/*** 点对点(point to point)消息发送,spring整合* * @author JPM*/
public class SpringMessageSender {public static void main(String[] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/springContext-activemq.xml");JmsTemplate jmsTemplate = (JmsTemplate) context.getBean("jmsTemplate");jmsTemplate.send(new MessageCreator() {public Message createMessage(Session session) throws JMSException {TextMessage message = session.createTextMessage();message.setText("hello,spring-queue!");return message;}});context.close();}
}

运行 SpringMessageSender 类,查看 ActiveMQ 管理界面

4f77713cb10940dbc7621a07062c708a.png

说明消息已经发送到了 spring-queue 中。

2、消费者代码(receive方法获取消息)
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="connectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory"destroy-method="stop"><property name="connectionFactory"><bean class="org.apache.activemq.ActiveMQConnectionFactory"><property name="brokerURL"><value>tcp://localhost:61616</value></property></bean></property><property name="maxConnections" value="50" /></bean><bean id="destination" class="org.apache.activemq.command.ActiveMQQueue"><constructor-arg index="0" value="spring-queue" /></bean><bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"><property name="connectionFactory" ref="connectionFactory" /><property name="defaultDestination" ref="destination" /><property name="messageConverter"><beanclass="org.springframework.jms.support.converter.SimpleMessageConverter" /></property></bean></beans>
  • 消费者关键代码:SpringMessageReceiver

package consumer;import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;/*** 点对点(point to point)消息接收,spring整合* * @author JPM*/
public class SpringMessageReceiver {public static void main(String[] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/springContext-activemq.xml");JmsTemplate jmsTemplate = (JmsTemplate) context.getBean("jmsTemplate");String message = (String) jmsTemplate.receiveAndConvert();System.out.println(message);context.close();}
}

运行 SpringMessageReceiver 类,查看控制台和 ActiveMQ 管理界面

8df5e77ec7856bf93799de06e324520e.png

538e7417b74350ce06937cfb237984c4.png

说明消费者读取到了消息,并打印到控制台显示。

3、消费者代码(使用消息监听器获取消息)
  • 使用刚才的生产者,再次发送一条消息

20c896f723403991c3d7a89768a52fa4.png

spring 关键代码:springContext-activemq.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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="connectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory"destroy-method="stop"><property name="connectionFactory"><bean class="org.apache.activemq.ActiveMQConnectionFactory"><property name="brokerURL"><value>tcp://localhost:61616</value></property></bean></property><property name="maxConnections" value="50" /></bean><bean id="destination" class="org.apache.activemq.command.ActiveMQQueue"><constructor-arg index="0" value="spring-queue" /></bean><bean id="jmsContainer"class="org.springframework.jms.listener.DefaultMessageListenerContainer"><property name="connectionFactory" ref="connectionFactory" /><property name="destination" ref="destination" /><property name="messageListener" ref="messageListener" /></bean><bean id="messageListener" class="consumer.SpringMessageListener" /></beans>

消费者关键代码:SpringMessageListener

java">package consumer;import java.io.IOException;import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;import org.springframework.context.support.ClassPathXmlApplicationContext;/*** 点对点(point to point)消息接收,spring整合,使用Listener* * @author JPM*/
public class SpringMessageListener implements MessageListener {public void onMessage(Message message) {String msg = null;try {msg = ((TextMessage) message).getText();} catch (JMSException e) {e.printStackTrace();}System.out.println(msg);}public static void main(String[] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/springContext-activemq.xml");try {System.in.read();} catch (IOException e) {e.printStackTrace();}context.close();}}

运行 SpringMessageListener 类,查看控制台和 ActiveMQ 管理界面

c0996282bd3284fb313dbec78fdb1f9d.png

47a60cea2c7750514d9114c581537555.png

说明消费者读取到了消息,并打印到控制台显示。

使用 Listener 和 receive方法的区别在于 Listener 会一直运行,主动监听消息的变化,及时消费。

Listener 验证,再次运行生产者,观察 ActiveMQ 管理界面

2baa2228b56ac534ffd779f873e01c12.png


http://www.ppmy.cn/devtools/118121.html

相关文章

go webapi上传文件 部属到linux

go厉害的地方&#xff0c;linux服务器上无需安装任何依赖就可以运行&#xff0c;大赞&#xff01; 一、编译 #在Goland中cmd中执行 go env -w GOARCHamd64 go env -w GOOSlinux go build main.go # 切换回来 否则无法运行 go env -w GOOSwindows go run main.go 拷贝到linux服…

一款好用的多种格式电子书制作软件

在数字化阅读日益普及的今天&#xff0c;电子书已经成为人们日常生活中不可或缺的一部分。而一款功能强大、操作简便的电子书制作软件&#xff0c;无疑是满足广大用户需求的最佳选择。 这款软件名为“FLBOOK在线制作电子杂志平台”&#xff0c;它支持多种格式输入&#xff0c;如…

如何组织一场考试并筛选未参加答题的考生?

&#x1f64b;频繁有小伙伴咨询&#xff1a;我组织了一场答题活动&#xff0c;导出考试成绩时只有参加了答题的人&#xff0c;但我想要找到哪些人没答题 此前我们会建议小伙伴逐人排查&#xff0c;但这建议被反复吐槽&#x1f926; 确实&#xff0c;如果只有十几个人逐人排查还…

Mac pnpm安装

安装pnpm的时候一定要把npm更新到最新版 不然pnpm下载不成功。 &#xff08;更新npm&#xff09;&#xff1a;sudo npm install -g npm (安装pnpm:) sudo npm install -g pnpm 检验安装是否成功&#xff1a;pnpm --version 项目内安装依赖&#xff1a;pnpm install / 运行项目&…

【Redis入门到精通八】Redis事务与MySQL事务对比

目录 事务 1.MySQL中事务的特性 2.Redis事务与MySQL事务的区别 3.Redis事务操作演示 事务 什么是事务呢&#xff1f;事务的概念其实就是把一系列操作绑定成一组&#xff0c;让这一组操作能够批量执行&#xff0c;不过在MySQL中有复杂的机制能够保证这一组操作执行并且一定能…

Python | 第五章节 | 进制

P40 标识符 2024/8/15 1、Python对各种变量、函数和类等命名时使用的字符序列称为标识符 2、凡是自己可以起名字的地方都叫标识符num1 90 一、标识符的命名规则 1、由26个英文字母大小写&#xff0c;0-9 ,_组成 2、数字不可以开头 3、不可以使用关键字,但能包含关键字 4、Pyth…

新React v19特性

服务器组件(RSC)&#xff1a;经过多年的开发&#xff0c;React 引入了服务器组件&#xff0c;而不是需要借助Next.js 动作(Action)&#xff1a;Action也将彻底改变我们与 DOM 元素的交互方式。 增强的 hooks&#xff1a;引入了很多新 hooks&#xff0c;将改变我们的编码体验。…

社团周报系统可行性研究-web后端框架对比-springboot,django,gin

对于目前市面上web后端框架&#xff0c;我主要了解到的就是springboot&#xff0c;django gin等&#xff0c;分别对应java python go三种语言&#xff0c;目前我比较熟悉的就是springboot 目录 spring boot框架 简介 优点 缺点 适用场景 与需求匹配度 django框架 简介…