1. 对 Spring 的整合
1.1 所需jar 包

  1. <!-- activeMQ jms 的支持 -->
  2. <dependency>
  3. <groupId>org.springframework</groupId>
  4. <artifactId>spring-jms</artifactId>
  5. <version>4.3.23.RELEASE</version>
  6. </dependency>
  7. <dependency> <!-- pool 池化包相关的支持 -->
  8. <groupId>org.apache.activemq</gro
  9. upId>
  10. <artifactId>activemq-pool</artifactId>
  11. <version>5.15.9</version>
  12. </dependency>
  13.  
  14. <!-- aop 相关的支持 -->
  15. <dependency>
  16. <groupId>org.springframework</groupId>
  17. <artifactId>spring-core</artifactId>
  18. <version>4.3.23.RELEASE</version>
  19. </dependency>
  20. <dependency>
  21. <groupId>org.springframework</groupId>
  22. <artifactId>spring-context</artifactId>
  23. <version>4.3.23.RELEASE</version>
  24. </dependency>

1.2 写xml 文件   (applicationContext.xml)

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://camel.apache.org/schema/spring"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://www.springframework.org/schema/context
  7. http://www.springframework.org/schema/context/spring-context.xsd
  8. http://www.springframework.org/schema/aop
  9. http://www.springframework.org/schema/aop/spring-aop.xsd
  10. http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
  11.  
  12. <context:commponent-scan base-package="com.at.activemq"/>
  13. <bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop">
  14. <property name="connectionFactory">
  15. <bean class="org.apache.activemq.ActiveMQConnectionFactory">
  16. <property name="brokerURL" value="tcp://192.168.17.3:61616"></property>
  17. </bean>
  18. </property>
  19. <property name="maxConnections" value="100"></property>
  20. </bean>
  21.  
  22. <!-- 队列目的地 -->
  23. <bean id="destinationQueue" class="org.apache.activemq.command.ActiveMQQueue">
  24. <constructor-arg index="0" value="spring-active-queue"></constructor-arg>
  25. </bean>
  26.  
  27. <!-- jms 的工具类 -->
  28. <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
  29. <property name="connectionFactory" ref="jmsFactory"/>
  30. <property name="defaultDestination" ref="destinationQueue"/>
  31. <property name="messageConverter">
  32. <bean class="org.springframework.jms.support.converter.SimpleMessageConverter"/>
  33. </property>
  34. </bean>
  35. </beans>

1.3  编写代码:

  1. @Service
  2. public class SpringMQ_producer {
  3. @Autowired
  4. private JmsTemplate jmsTemplate;
  5. public static void main(String[] args) {
  6. ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
  7. SpringMQ_producer producer = (SpringMQ_producer) ctx.getBean("springMQ_Producer");
  8. producer.jmsTemplate.send((session) -> {
  9. TextMessage textMessage = session.createTextMessage("spring 和 activemq 的整合");
  10. return textMessage;
  11. });
  12. System.out.println(" *** send task over ***");
  13. }
  14. }
  1. @Service
  2. public class Spring_MQConsummer {
  3. @Autowired
  4. private JmsTemplate jmsTemplate;
  5. public static void main(String[] args) {
  6. ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
  7. Spring_MQConsummer sm = (Spring_MQConsummer)ac.getBean("spring_MQConsummer");
  8.  
  9. String s = (String) sm.jmsTemplate.receiveAndConvert();
  10. System.out.println(" *** 消费者消息"+s);
  11. }
  12. }

并且可以在spring 中设置监听器,不用启动消费者,就可以自动监听到消息,并处理

2. Spring Boot 整合 ActiveMQ
2.1 建立boot 项目,配置 pom.xml 配置 application.yml 配置 bean
2.2 编写生产者 编写启动类 测试类

按键触发消息和定时发送消息的业务代码:

  1. // 调用一次一个信息发出
  2. public void produceMessage(){
  3. jmsMessagingTemplate.convertAndSend(queue,"****"+ UUID.randomUUID().toString().substring(0,6));
  4. }
  5.  
  6. // 带定时投递的业务方法
  7. @Scheduled(fixedDelay = 3000) // 每3秒自动调用
  8. public void produceMessageScheduled(){
  9. jmsMessagingTemplate.convertAndSend(queue,"** scheduled **"+ UUID.randomUUID().toString().substring(0,6));
  10. System.out.println(" produceMessage send ok ");
  11. }

对于消息消费者,在以前使用单独的监听器类,编写监听器代码,但是在spring boot 中,使用注解 JmsListener 即可:

  1. @Component
  2. public class Queue_consummer {
  3.  
  4. @JmsListener(destination = "${myqueue}") // 注解监听
  5. public void receive(TextMessage textMessage) throws Exception{
  6. System.out.println(" *** 消费者收到消息 ***"+textMessage.getText());
  7. }
  8. }

这些是之前(队列)消息发送者发送的消息

2.3 编写消费者项目

2.4  编写主题的消息生产者和消费者项目,运行demo

代码地址:https://github.com/elstic/ActiveMQ

ActiveMQ与Spring / SpringBoot 整合(四)的更多相关文章

  1. ActiveMQ 笔记(四)Spring\SpringBoot 整合 Activemq

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 一.Spring 整合Activemq 1.所需jar包 <dependencies> &l ...

  2. activeMQ入门+spring boot整合activeMQ

    最近想要学习MOM(消息中间件:Message Oriented Middleware),就从比较基础的activeMQ学起,rabbitMQ.zeroMQ.rocketMQ.Kafka等后续再去学习 ...

  3. activeMQ和spring的整合

    http://www.cnblogs.com/shuai-server/p/8966299.html  这篇博客中介绍了activemq传递消息的两种方式,今天分享的是activemq框架和sprin ...

  4. 04.ActiveMQ与Spring JMS整合

        SpringJMS使用参考:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/jms.html ...

  5. Spring/SpringBoot整合QuartZ

    https://www.bilibili.com/video/av55637917/?p=2

  6. SpringBoot整合ActiveMQ和开启持久化

    一.点对点 1.提供者目录展示 2.导入依赖 <dependency> <groupId>org.springframework.boot</groupId> &l ...

  7. Web项目容器集成ActiveMQ & SpringBoot整合ActiveMQ

    集成tomcat就是随项目启动而启动tomcat,最简单的方法就是监听器监听容器创建之后以Broker的方式启动ActiveMQ. 1.web项目中Broker启动的方式进行集成 在这里采用Liste ...

  8. SpringBoot系列八:SpringBoot整合消息服务(SpringBoot 整合 ActiveMQ、SpringBoot 整合 RabbitMQ、SpringBoot 整合 Kafka)

    声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念:SpringBoot 整合消息服务 2.具体内容 对于异步消息组件在实际的应用之中会有两类: · JMS:代表作就是 ...

  9. SpringBoot整合ActiveMQ快速入门

    Spring Boot 具有如下特性: 为基于 Spring 的开发提供更快的入门体验 开箱即用,没有代码生成,也无需 XML 配置.同时也可以修改默认值来满足特定的需求. 提供了一些大型项目中常见的 ...

随机推荐

  1. WPF 使用Task代替ThreadPool和Thread

    一:Task的优势 ThreadPool相比Thread来说具备了很多优势,但是ThreadPool却又存在一些使用上的不方便.比如: 1: ThreadPool不支持线程的取消.完成.失败通知等交互 ...

  2. 匿名内部类 this.val$的问题

    一天偶尔在网上找到一个jar包,反编译后出现了如下的代码: public void defineAnonymousInnerClass(String name)  {    new Thread(na ...

  3. linux(centOS7)的基本操作(四) 定时任务——crontab

    概述 对于Java开发人员,定时任务并不陌生,无非是让系统在特定时间执行特定的命令或程序.例如spring提供的@Scheduled注解.OpenSymphony提供的quartz框架,都可以实现定时 ...

  4. 阶段3 2.Spring_01.Spring框架简介_03.spring概述

  5. 四十六:数据库之Flask-SQLAlchemy的使用

    一:连接数据库1.安装:pip install flask-sqlalchemy2.将数据库信息更新到app.config['SQLALCHEMY_DATABASE_URI']3.使用flask_sq ...

  6. 手机连接电脑,使用adb命令

    手机连接电脑使用adb命令,主要是有2种方式,其中最常见的就是第一种,用usb连线使用 1:adb usb - restarts the adbd daemon listening on USB ad ...

  7. C# 中的字符串内插

    $ 特殊字符将字符串文本标识为内插字符串. 内插字符串是可能包含内插表达式的字符串文本. 将内插字符串解析为结果字符串时,带有内插表达式的项会替换为表达式结果的字符串表示形式. 此功能在 C# 6 及 ...

  8. 深入理解java:2.3.6. 并发编程concurrent包 之管理类---线程池

    我们使用线程的时候就去创建一个线程,这样实现起来非常简便,但是就会有一个问题: 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了,这样频繁创建线程就会大大降低系统的效率,因为频繁 ...

  9. Mycat+Mysql主从复制实现双机热备

    Mycat+Mysql主从复制实现双机热备 一.mysql主从配置原理 双机热备的概念简单说一下,就是要保持两个数据库的状态自动同步.对任何一个数据库的操作都自动应用到另外一个数据库,始终保持两个数据 ...

  10. System.InsufficientMemoryException:无法分配536870912字节的托管内存缓冲区。可用内存量可能不足

    一个病人住院太久,一次性打印护理表单超过3000条时报如标题所示的错误, 个人查阅分析应该可以从如下几方面入手: 一:查看程序客户端和服务端的配置文件相关属性是否限制了缓存最大值 (应该不是这个问题, ...