ActiveMQ —— Spring 整合 ActiveMQ

柔情只为你懂 2023-02-11 07:28 134阅读 0赞

前文

消息中间件 —— 简介

ActiveMQ 下载、安装

ActiveMQ —— Java 连接 ActiveMQ(点对点)

ActiveMQ —— Java 连接 ActiveMQ(发布订阅 Topic)

ActiveMQ —— Broker

文章目录

  • 前文
  • 添加依赖
  • Spring 配置文件
  • 队列
    • 生产者
    • 消费者
  • 主题
    • 修改 applicationContext.xml 文件
  • 在 Spring 里面实现消费者不启动,直接通过配置监听完成

添加依赖

  1. <dependency>
  2. <groupId>com.fasterxml.jackson.core</groupId>
  3. <artifactId>jackson-databind</artifactId>
  4. <version>2.9.5</version>
  5. </dependency>
  6. <!-- ActiveMQ 对 JMS 支持,整合 Spring 和 ActiveMQ -->
  7. <dependency>
  8. <groupId>org.springframework</groupId>
  9. <artifactId>spring-jms</artifactId>
  10. <version>4.3.23.RELEASE</version>
  11. </dependency>
  12. <!-- ActiveMQ 所需要的 pool 包配置 -->
  13. <dependency>
  14. <groupId>org.apache.activemq</groupId>
  15. <artifactId>activemq-pool</artifactId>
  16. <version>5.15.9</version>
  17. </dependency>
  18. <!-- Spring AOP 等相关 Jar 包 -->
  19. <dependency>
  20. <groupId>org.springframework</groupId>
  21. <artifactId>spring-core</artifactId>
  22. <version>4.3.25.RELEASE</version>
  23. </dependency>

这里的 Spring-core 有版本问题,使用 5.0+ 的整合 ActiveMQ 会报错,使用 4.0 + 的就不会

Spring 配置文件

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <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/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/beans">
  3. <!-- 开启包的自动扫描 -->
  4. <context:component-scan base-package="com.java.elasticsearch.activemq"/>
  5. <!-- 配置生产者 -->
  6. <bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop">
  7. <property name="connectionFactory">
  8. <!-- 真正可以产生 Connection 的 ConnectionFactory,由对应的 JMS 服务厂商提供 -->
  9. <bean class="org.apache.activemq.ActiveMQConnectionFactory">
  10. <property name="brokerURL" value="tcp://localhost:61616"/>
  11. </bean>
  12. </property>
  13. <!-- 最大连接数 -->
  14. <property name="maxConnections" value="100"/>
  15. </bean>
  16. <!-- 这个是队列的目的地,点对点的 -->
  17. <bean id="destinationQueue" class="org.apache.activemq.command.ActiveMQQueue">
  18. <!-- 队列名称 -->
  19. <constructor-arg index="0" value="spring-active-queue"/>
  20. </bean>
  21. <!-- Spring 提供的 JMS 工具类,它可以进行消息发送,接收等 -->
  22. <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
  23. <property name="connectionFactory" ref="jmsFactory"/>
  24. <property name="defaultDestination" ref="destinationQueue"/>
  25. <property name="messageConverter">
  26. <bean class="org.springframework.jms.support.converter.SimpleMessageConverter"/>
  27. </property>
  28. </bean>
  29. </beans>

队列

生产者

  1. package com.java.elasticsearch.activemq.service;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. import org.springframework.jms.core.JmsTemplate;
  6. import org.springframework.jms.core.MessageCreator;
  7. import org.springframework.stereotype.Service;
  8. import javax.jms.JMSException;
  9. import javax.jms.Message;
  10. import javax.jms.Session;
  11. import javax.jms.TextMessage;
  12. /** * @author Woo_home * @create 2020/5/22 14:09 */
  13. @Service
  14. public class SpringMQ_Produce {
  15. @Autowired
  16. private JmsTemplate jmsTemplate;
  17. public static void main(String[] args){
  18. ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
  19. // 相当于 new 一个 SpringMQ_Produce,但是这里用的是 Spring,所以这里就不用 new 了
  20. SpringMQ_Produce produce = (SpringMQ_Produce) ctx.getBean("springMQ_Produce");
  21. // 普通写法
  22. /*produce.jmsTemplate.send(new MessageCreator() { @Override public Message createMessage(Session session) throws JMSException { TextMessage textMessage = session.createTextMessage("****** Spring 整合 ActiveMQ ******"); return textMessage; } });*/
  23. // lambda 写法
  24. produce.jmsTemplate.send((session) -> {
  25. TextMessage textMessage = session.createTextMessage("****** Spring 整合 ActiveMQ ******");
  26. return textMessage;
  27. });
  28. }
  29. }

执行程序之前先启动 ActiveMQ
在这里插入图片描述
执行主程序
在这里插入图片描述
刷新 admin 页面,队列已成功添加进来
在这里插入图片描述

消费者

消费者端代码非常简单,只需要接收就行了

  1. package com.java.elasticsearch.activemq.service;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. import org.springframework.jms.core.JmsTemplate;
  6. import org.springframework.stereotype.Service;
  7. import javax.jms.TextMessage;
  8. /** * @author Woo_home * @create 2020/5/22 14:09 */
  9. @Service
  10. public class SpringMQ_Consumer {
  11. @Autowired
  12. private JmsTemplate jmsTemplate;
  13. public static void main(String[] args){
  14. ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
  15. // 相当于 new 一个 SpringMQ_Consumer,但是这里用的是 Spring,所以这里就不用 new 了
  16. SpringMQ_Consumer consumer = (SpringMQ_Consumer) ctx.getBean("springMQ_Consumer");
  17. String retValue = (String) consumer.jmsTemplate.receiveAndConvert();
  18. System.out.println("****** 消费者收到消息 : " + retValue);
  19. }
  20. }

执行程序
在这里插入图片描述
刷新 admin 页面
在这里插入图片描述

主题

修改 applicationContext.xml 文件

只需要将以下三处修改为 Topic 即可
在这里插入图片描述
生产者与消费者的代码不用修改,直接运行,要是你想修改的话,可以修改如下内容
在这里插入图片描述
在这里插入图片描述
因为这里使用的是 Topic,我们先启动消费端,此时的消费端还没收到信息
在这里插入图片描述
在这里插入图片描述
再启动生产端
在这里插入图片描述
在这里插入图片描述
此时,消费端接收到生产者发来的信息
在这里插入图片描述

在 Spring 里面实现消费者不启动,直接通过配置监听完成

修改 applicationContext.xml 文件

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <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/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/beans">
  3. <!-- 开启包的自动扫描 -->
  4. <context:component-scan base-package="com.java.elasticsearch.activemq"/>
  5. <!-- 配置生产者 -->
  6. <bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop">
  7. <property name="connectionFactory">
  8. <!-- 真正可以产生 Connection 的 ConnectionFactory,由对应的 JMS 服务厂商提供 -->
  9. <bean class="org.apache.activemq.ActiveMQConnectionFactory">
  10. <property name="brokerURL" value="tcp://localhost:61616"/>
  11. </bean>
  12. </property>
  13. <!-- 最大连接数 -->
  14. <property name="maxConnections" value="100"/>
  15. </bean>
  16. <!-- 这个是队列的目的地,点对点的 -->
  17. <bean id="destinationQueue" class="org.apache.activemq.command.ActiveMQQueue">
  18. <!-- 队列名称 -->
  19. <constructor-arg index="0" value="spring-active-queue"/>
  20. </bean>
  21. <!-- 这个是队列的目的地,主题的 -->
  22. <bean id="destinationTopic" class="org.apache.activemq.command.ActiveMQTopic">
  23. <!-- 队列名称 -->
  24. <constructor-arg index="0" value="spring-active-topic"/>
  25. </bean>
  26. <!-- Spring 提供的 JMS 工具类,它可以进行消息发送,接收等 -->
  27. <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
  28. <property name="connectionFactory" ref="jmsFactory"/>
  29. <property name="defaultDestination" ref="destinationTopic"/>
  30. <property name="messageConverter">
  31. <bean class="org.springframework.jms.support.converter.SimpleMessageConverter"/>
  32. </property>
  33. </bean>
  34. <!-- 配置监听程序 -->
  35. <bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
  36. <property name="connectionFactory" ref="jmsFactory"/>
  37. <property name="destination" ref="destinationTopic"/>
  38. <!-- public class MyMessageListener implements MessageListener {} -->
  39. <property name="messageListener" ref="myMessageListener"/>
  40. </bean>
  41. </beans>

配置监听
在这里插入图片描述
这里还需要一个组件

  1. package com.java.elasticsearch.activemq.service;
  2. import org.springframework.stereotype.Component;
  3. import javax.jms.JMSException;
  4. import javax.jms.Message;
  5. import javax.jms.MessageListener;
  6. import javax.jms.TextMessage;
  7. /** * @author Woo_home * @create 2020/5/22 14:59 */
  8. @Component
  9. public class MyMessageListener implements MessageListener {
  10. @Override
  11. public void onMessage(Message message) {
  12. if (null != message && message instanceof TextMessage) {
  13. TextMessage textMessage = (TextMessage) message;
  14. try {
  15. System.out.println(textMessage.getText());
  16. } catch (JMSException e) {
  17. e.printStackTrace();
  18. }
  19. }
  20. }
  21. }

先删除 Topic
在这里插入图片描述
启动生产者,可以发现,消费者马上就可以监听到消费者发送的消息
在这里插入图片描述
在这里插入图片描述


完整代码已上传至码云 代码下载地址

发表评论

表情:
评论列表 (有 0 条评论,134人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Spring 整合 ActiveMQ

    Spring 是J2EE 最重要的框架,ActiveMQ 是Jms的框架,用于两个程序、系统中的异步通信,两者的用途都挺广泛。上一篇博文介绍的是发布-订阅形式,今次以点-点形式