spring boot整合JMS(ActiveMQ实现)
pom依赖如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.chhliu.springboot.jms</groupId>
- <artifactId>springboot-jms</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>jar</packaging>
- <name>springboot-jms</name>
- <description>Demo project for Spring Boot Jms</description>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>1.4.3.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
- <java.version>1.7</java.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-activemq</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
- </project>
四、修改application.properties配置文件
- ## URL of the ActiveMQ broker. Auto-generated by default. For instance `tcp://localhost:61616`
- # failover:(tcp://localhost:61616,tcp://localhost:61617)
- # tcp://localhost:61616
- spring.activemq.broker-url=tcp://localhost:61616
- spring.activemq.in-memory=true
- spring.activemq.pool.enabled=false //如果此处设置为true,需要加如下的依赖包,否则会自动配置失败,报JmsMessagingTemplate注入失败
- <dependency>
- <groupId>org.apache.activemq</groupId>
- <artifactId>activemq-pool</artifactId>
- <!-- <version>5.7.0</version> -->
- </dependency>
五、消息生产者
- package com.chhliu.springboot.jms;
- import javax.jms.Destination;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.jms.core.JmsMessagingTemplate;
- import org.springframework.stereotype.Service;
- @Service("producer")
- public class Producer {
- @Autowired // 也可以注入JmsTemplate,JmsMessagingTemplate对JmsTemplate进行了封装
- private JmsMessagingTemplate jmsTemplate;
- // 发送消息,destination是发送到的队列,message是待发送的消息
- public void sendMessage(Destination destination, final String message){
- jmsTemplate.convertAndSend(destination, message);
- }
- }
六、消息消费者
- package com.chhliu.springboot.jms;
- import org.springframework.jms.annotation.JmsListener;
- import org.springframework.stereotype.Component;
- @Component
- public class Consumer {
- // 使用JmsListener配置消费者监听的队列,其中text是接收到的消息
- @JmsListener(destination = "mytest.queue")
- public void receiveQueue(String text) {
- System.out.println("Consumer收到的报文为:"+text);
- }
- }
消费者2的代码同上,注意,消息消费者的类上必须加上@Component,或者是@Service,这样的话,消息消费者类就会被委派给Listener类,原理类似于使用SessionAwareMessageListener以及MessageListenerAdapter来实现消息驱动POJO
七、测试
- package com.chhliu.springboot.jms;
- import javax.jms.Destination;
- import org.apache.activemq.command.ActiveMQQueue;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
- import org.springframework.test.context.junit4.SpringRunner;
- @RunWith(SpringRunner.class)
- @SpringBootTest
- public class SpringbootJmsApplicationTests {
- @Autowired
- private Producer producer;
- @Test
- public void contextLoads() throws InterruptedException {
- Destination destination = new ActiveMQQueue("mytest.queue");
- for(int i=0; i<100; i++){
- producer.sendMessage(destination, "myname is chhliu!!!");
- }
- }
- }
测试结果如下:
- Consumer2收到的报文为:myname is chhliu!!!
- Consumer收到的报文为:myname is chhliu!!!
- Consumer2收到的报文为:myname is chhliu!!!
- Consumer收到的报文为:myname is chhliu!!!
- Consumer2收到的报文为:myname is chhliu!!!
- Consumer收到的报文为:myname is chhliu!!!
- Consumer2收到的报文为:myname is chhliu!!!
- Consumer收到的报文为:myname is chhliu!!!
- Consumer2收到的报文为:myname is chhliu!!!
- Consumer收到的报文为:myname is chhliu!!!
- Consumer2收到的报文为:myname is chhliu!!!
- Consumer收到的报文为:myname is chhliu!!!
- Consumer2收到的报文为:myname is chhliu!!!
经过上面的几个步骤,spring boot和Jms就基本上整合完成了,是不是使用起来很方便了!
spring boot整合JMS(ActiveMQ实现)的更多相关文章
- spring boot整合activemq消息中间件
spring boot整合activemq消息中间件 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi ...
- activeMQ入门+spring boot整合activeMQ
最近想要学习MOM(消息中间件:Message Oriented Middleware),就从比较基础的activeMQ学起,rabbitMQ.zeroMQ.rocketMQ.Kafka等后续再去学习 ...
- RabbitMQ使用及与spring boot整合
1.MQ 消息队列(Message Queue,简称MQ)——应用程序和应用程序之间的通信方法 应用:不同进程Process/线程Thread之间通信 比较流行的中间件: ActiveMQ Rabbi ...
- 【ActiveMQ】2.spring Boot下使用ActiveMQ
在spring boot下使用ActiveMQ,需要一下几个条件 1.安装并启动了ActiveMQ,参考:http://www.cnblogs.com/sxdcgaq8080/p/7919489.ht ...
- Spring Boot 监听 Activemq 中的特定 topic ,并将数据通过 RabbitMq 发布出去
1.Spring Boot 和 ActiveMQ .RabbitMQ 简介 最近因为公司的项目需要用到 Spring Boot , 所以自学了一下, 发现它与 Spring 相比,最大的优点就是减少了 ...
- Spring Boot (十三): Spring Boot 整合 RabbitMQ
1. 前言 RabbitMQ 是一个消息队列,说到消息队列,大家可能多多少少有听过,它主要的功能是用来实现应用服务的异步与解耦,同时也能起到削峰填谷.消息分发的作用. 消息队列在比较主要的一个作用是用 ...
- Spring Boot 整合 Elasticsearch,实现 function score query 权重分查询
摘要: 原创出处 www.bysocket.com 「泥瓦匠BYSocket 」欢迎转载,保留摘要,谢谢! 『 预见未来最好的方式就是亲手创造未来 – <史蒂夫·乔布斯传> 』 运行环境: ...
- spring boot整合jsp的那些坑(spring boot 学习笔记之三)
Spring Boot 整合 Jsp 步骤: 1.新建一个spring boot项目 2.修改pom文件 <dependency> <groupId>or ...
- spring boot 系列之四:spring boot 整合JPA
上一篇我们讲了spring boot 整合JdbcTemplate来进行数据的持久化, 这篇我们来说下怎么通过spring boot 整合JPA来实现数据的持久化. 一.代码实现 修改pom,引入依赖 ...
随机推荐
- 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] ...
- HQL数据查询基础
HQL定义 1.Hibernate Query Language, Hibernate查询语言 2.HQL是面向对象的查询语言(HQL查询的主体是映射配置的持久化类及其属性而SQL查询主体是数据库表) ...
- Selenium webdriver操作日历控件
一般的日期控件都是input标签下弹出来的,如果使用webdriver 去设置日期, 1. 定位到该input 2. 使用sendKeys 方法 比如:使用定位: driver.findElement ...
- Win7 64位使用IDA Pro 6.8调试64位exe程序
有点小坑,记录备忘. 首先搞个IDA Pro6.8,写本文时能找到的最高版本,试了下果然比6.6强大许多,其实6.6也没怎么用过...... 32位版本有个Local Win32 debugger,但 ...
- CCPC-2017-秦皇岛站
10月25日 听说信用卡到了好兴奋,然而没有额度是啥情况啊qwq. 晚上坐飞机出发,成都-鄂尔多斯-石家庄-秦皇岛,队友吐槽鄂尔多斯到石家庄好近啊,然后过了一会儿我们因为石家庄大雾迫降在了济南.嘤嘤嘤 ...
- Beta阶段Scrum 冲刺博客合集
Beta阶段博客链接集合 第一篇Scrum冲刺博客 第二篇Scrum冲刺博客-Day1 第三篇Scrum冲刺博客-Day2 第四篇Scrum冲刺博客-Day3 第五篇Scrum冲刺博客-Day4 第六 ...
- Python自动化开发 - RESTful API
本节内容 1. RESTful 简介 2. RESTful 设计指南 3. Django REST Framework 最佳实践 4. 理论拓展与开放平台 5. API文档化与测试 一 R ...
- [转] 语音识别基本原理介绍----gmm-hmm中的embedded training (嵌入式训练)
转自:http://blog.csdn.net/wbgxx333/article/details/38986507 本文是翻译Speech and Language Processing: An in ...
- ssh命令-使用密钥文件进行登陆
在win上面可以使用XSHELL来登录类似于阿里云这样的安全服务器,在ubuntu上面就可以使用系统自带的命令工具来连接 使用命令 ssh -i key.pem [server] 实例如下: ssh ...
- 谷歌浏览器怎么FQ(一)(想使用谷歌浏览器应用商城的小伙伴这边看)
谷歌浏览器的应用商城里本身有很多不错的扩展程序和插件,比如Wappalyzer(能够识别某个网站用的什么框架和库)广告终结者(能屏蔽大部分浮动,弹窗,甚至视频广告)等 但是谷歌因为某些原因需要FQ以后 ...