pom依赖如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.chhliu.springboot.jms</groupId>
  6. <artifactId>springboot-jms</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <name>springboot-jms</name>
  10. <description>Demo project for Spring Boot Jms</description>
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>1.4.3.RELEASE</version>
  15. <relativePath/> <!-- lookup parent from repository -->
  16. </parent>
  17. <properties>
  18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  20. <java.version>1.7</java.version>
  21. </properties>
  22. <dependencies>
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-activemq</artifactId>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.springframework.boot</groupId>
  29. <artifactId>spring-boot-starter-test</artifactId>
  30. <scope>test</scope>
  31. </dependency>
  32. </dependencies>
  33. <build>
  34. <plugins>
  35. <plugin>
  36. <groupId>org.springframework.boot</groupId>
  37. <artifactId>spring-boot-maven-plugin</artifactId>
  38. </plugin>
  39. </plugins>
  40. </build>
  41. </project>

四、修改application.properties配置文件

  1. ## URL of the ActiveMQ broker. Auto-generated by default. For instance `tcp://localhost:61616`
  2. # failover:(tcp://localhost:61616,tcp://localhost:61617)
  3. # tcp://localhost:61616
  4. spring.activemq.broker-url=tcp://localhost:61616
  5. spring.activemq.in-memory=true
  6. spring.activemq.pool.enabled=false //如果此处设置为true,需要加如下的依赖包,否则会自动配置失败,报JmsMessagingTemplate注入失败
  1. <dependency>
  2. <groupId>org.apache.activemq</groupId>
  3. <artifactId>activemq-pool</artifactId>
  4. <!-- <version>5.7.0</version> -->
  5. </dependency>

五、消息生产者

  1. package com.chhliu.springboot.jms;
  2. import javax.jms.Destination;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.jms.core.JmsMessagingTemplate;
  5. import org.springframework.stereotype.Service;
  6. @Service("producer")
  7. public class Producer {
  8. @Autowired // 也可以注入JmsTemplate,JmsMessagingTemplate对JmsTemplate进行了封装
  9. private JmsMessagingTemplate jmsTemplate;
  10. // 发送消息,destination是发送到的队列,message是待发送的消息
  11. public void sendMessage(Destination destination, final String message){
  12. jmsTemplate.convertAndSend(destination, message);
  13. }
  14. }

六、消息消费者

  1. package com.chhliu.springboot.jms;
  2. import org.springframework.jms.annotation.JmsListener;
  3. import org.springframework.stereotype.Component;
  4. @Component
  5. public class Consumer {
  6. // 使用JmsListener配置消费者监听的队列,其中text是接收到的消息
  7. @JmsListener(destination = "mytest.queue")
  8. public void receiveQueue(String text) {
  9. System.out.println("Consumer收到的报文为:"+text);
  10. }
  11. }

消费者2的代码同上,注意,消息消费者的类上必须加上@Component,或者是@Service,这样的话,消息消费者类就会被委派给Listener类,原理类似于使用SessionAwareMessageListener以及MessageListenerAdapter来实现消息驱动POJO

七、测试

  1. package com.chhliu.springboot.jms;
  2. import javax.jms.Destination;
  3. import org.apache.activemq.command.ActiveMQQueue;
  4. import org.junit.Test;
  5. import org.junit.runner.RunWith;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.boot.test.context.SpringBootTest;
  8. import org.springframework.test.context.junit4.SpringRunner;
  9. @RunWith(SpringRunner.class)
  10. @SpringBootTest
  11. public class SpringbootJmsApplicationTests {
  12. @Autowired
  13. private Producer producer;
  14. @Test
  15. public void contextLoads() throws InterruptedException {
  16. Destination destination = new ActiveMQQueue("mytest.queue");
  17. for(int i=0; i<100; i++){
  18. producer.sendMessage(destination, "myname is chhliu!!!");
  19. }
  20. }
  21. }

测试结果如下:

  1. Consumer2收到的报文为:myname is chhliu!!!
  2. Consumer收到的报文为:myname is chhliu!!!
  3. Consumer2收到的报文为:myname is chhliu!!!
  4. Consumer收到的报文为:myname is chhliu!!!
  5. Consumer2收到的报文为:myname is chhliu!!!
  6. Consumer收到的报文为:myname is chhliu!!!
  7. Consumer2收到的报文为:myname is chhliu!!!
  8. Consumer收到的报文为:myname is chhliu!!!
  9. Consumer2收到的报文为:myname is chhliu!!!
  10. Consumer收到的报文为:myname is chhliu!!!
  11. Consumer2收到的报文为:myname is chhliu!!!
  12. Consumer收到的报文为:myname is chhliu!!!
  13. Consumer2收到的报文为:myname is chhliu!!!

经过上面的几个步骤,spring boot和Jms就基本上整合完成了,是不是使用起来很方便了!

spring boot整合JMS(ActiveMQ实现)的更多相关文章

  1. spring boot整合activemq消息中间件

    spring boot整合activemq消息中间件 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi ...

  2. activeMQ入门+spring boot整合activeMQ

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

  3. RabbitMQ使用及与spring boot整合

    1.MQ 消息队列(Message Queue,简称MQ)——应用程序和应用程序之间的通信方法 应用:不同进程Process/线程Thread之间通信 比较流行的中间件: ActiveMQ Rabbi ...

  4. 【ActiveMQ】2.spring Boot下使用ActiveMQ

    在spring boot下使用ActiveMQ,需要一下几个条件 1.安装并启动了ActiveMQ,参考:http://www.cnblogs.com/sxdcgaq8080/p/7919489.ht ...

  5. Spring Boot 监听 Activemq 中的特定 topic ,并将数据通过 RabbitMq 发布出去

    1.Spring Boot 和 ActiveMQ .RabbitMQ 简介 最近因为公司的项目需要用到 Spring Boot , 所以自学了一下, 发现它与 Spring 相比,最大的优点就是减少了 ...

  6. Spring Boot (十三): Spring Boot 整合 RabbitMQ

    1. 前言 RabbitMQ 是一个消息队列,说到消息队列,大家可能多多少少有听过,它主要的功能是用来实现应用服务的异步与解耦,同时也能起到削峰填谷.消息分发的作用. 消息队列在比较主要的一个作用是用 ...

  7. Spring Boot 整合 Elasticsearch,实现 function score query 权重分查询

    摘要: 原创出处 www.bysocket.com 「泥瓦匠BYSocket 」欢迎转载,保留摘要,谢谢! 『 预见未来最好的方式就是亲手创造未来 – <史蒂夫·乔布斯传> 』 运行环境: ...

  8. spring boot整合jsp的那些坑(spring boot 学习笔记之三)

    Spring Boot 整合 Jsp 步骤: 1.新建一个spring boot项目 2.修改pom文件 <dependency>            <groupId>or ...

  9. spring boot 系列之四:spring boot 整合JPA

    上一篇我们讲了spring boot 整合JdbcTemplate来进行数据的持久化, 这篇我们来说下怎么通过spring boot 整合JPA来实现数据的持久化. 一.代码实现 修改pom,引入依赖 ...

随机推荐

  1. Sum of Even Numbers After Queries LT985

    We have an array A of integers, and an array queries of queries. For the i-th query val = queries[i] ...

  2. HQL数据查询基础

    HQL定义 1.Hibernate Query Language, Hibernate查询语言 2.HQL是面向对象的查询语言(HQL查询的主体是映射配置的持久化类及其属性而SQL查询主体是数据库表) ...

  3. Selenium webdriver操作日历控件

    一般的日期控件都是input标签下弹出来的,如果使用webdriver 去设置日期, 1. 定位到该input 2. 使用sendKeys 方法 比如:使用定位: driver.findElement ...

  4. Win7 64位使用IDA Pro 6.8调试64位exe程序

    有点小坑,记录备忘. 首先搞个IDA Pro6.8,写本文时能找到的最高版本,试了下果然比6.6强大许多,其实6.6也没怎么用过...... 32位版本有个Local Win32 debugger,但 ...

  5. CCPC-2017-秦皇岛站

    10月25日 听说信用卡到了好兴奋,然而没有额度是啥情况啊qwq. 晚上坐飞机出发,成都-鄂尔多斯-石家庄-秦皇岛,队友吐槽鄂尔多斯到石家庄好近啊,然后过了一会儿我们因为石家庄大雾迫降在了济南.嘤嘤嘤 ...

  6. Beta阶段Scrum 冲刺博客合集

    Beta阶段博客链接集合 第一篇Scrum冲刺博客 第二篇Scrum冲刺博客-Day1 第三篇Scrum冲刺博客-Day2 第四篇Scrum冲刺博客-Day3 第五篇Scrum冲刺博客-Day4 第六 ...

  7. Python自动化开发 - RESTful API

    本节内容 1.  RESTful 简介 2.  RESTful 设计指南 3.  Django REST Framework 最佳实践 4.  理论拓展与开放平台 5.  API文档化与测试 一  R ...

  8. [转] 语音识别基本原理介绍----gmm-hmm中的embedded training (嵌入式训练)

    转自:http://blog.csdn.net/wbgxx333/article/details/38986507 本文是翻译Speech and Language Processing: An in ...

  9. ssh命令-使用密钥文件进行登陆

    在win上面可以使用XSHELL来登录类似于阿里云这样的安全服务器,在ubuntu上面就可以使用系统自带的命令工具来连接 使用命令 ssh -i key.pem [server] 实例如下: ssh ...

  10. 谷歌浏览器怎么FQ(一)(想使用谷歌浏览器应用商城的小伙伴这边看)

    谷歌浏览器的应用商城里本身有很多不错的扩展程序和插件,比如Wappalyzer(能够识别某个网站用的什么框架和库)广告终结者(能屏蔽大部分浮动,弹窗,甚至视频广告)等 但是谷歌因为某些原因需要FQ以后 ...