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. log4net 写日志

    转载地址:https://www.cnblogs.com/vichin/p/6022612.html   //基本使用 https://www.cnblogs.com/genesis/p/498562 ...

  2. Sublime Text快捷键与插件介绍

    Sublime Text快捷键: Ctrl+Shift+P:打开命令面板Ctrl+P:搜索项目中的文件Ctrl+G:跳转到第几行Ctrl+W:关闭当前打开文件Ctrl+Shift+W:关闭所有打开文件 ...

  3. C# 多线程编程,传参,接受返回值

    C# 多线程编程,传参,接受返回值 今天将多线程的知识有回顾了下,总结了几点: 新建一个线程(无参数,无返回值) Thread th = new Thread(new ThreadStart(Prin ...

  4. ABP框架系列之四:(Repositories-仓库)

    "Mediates between the domain and data mapping layers using a collection-like interface for acce ...

  5. ABP框架系列之五十二:(Validating-Data-Transfer-Objects-验证数据传输对象)

    Introduction to validation Inputs of an application should be validated first. This input can be sen ...

  6. [ 9.29 ]CF每日一题系列—— 765B字符串规律

    Description: 遇到了ogo可以变成***如果ogo后面有go统统忽略,输出结果 Solution: 哎如果我一开始对题意的解读如上的话,就不会被整的那么麻烦了 Code: #include ...

  7. Ngui使用随心记

    Ngui的一些基础使用心得! BB:首先BB一下我觉得NGUI和UGUI哪个好?我首推UGUI,先不说是官方内置,在使用的方便性上也要好很多,而且NGUI停止更新了!还有就是NGUI有BUG! Ngu ...

  8. C#基础-gc算法

    众所周知,c++是需要程序员手动管理内存的,然而手动释放内存很容易被程序员遗漏,从而导致资源浪费或内存泄露.为解决这个问题,垃圾回收器诞生了,代替程序员自动管理内存的释放.至于gc算法则是垃圾回收器清 ...

  9. python中除法的注意事项

    使用python数据处理,代码如下: import numpy as np fs = 5 ts = np.arange(-1,1+1/fs,1/fs) 发现了这样一个错误: Traceback (mo ...

  10. Maven - 实例-1-手工创建Maven项目

    1- 根据包结构创建maven项目目录 TestMaven - src - src/main/java/anliven/testmaven01/HelloMaven.java - src/test/j ...