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,引入依赖 ...
随机推荐
- log4net 写日志
转载地址:https://www.cnblogs.com/vichin/p/6022612.html //基本使用 https://www.cnblogs.com/genesis/p/498562 ...
- Sublime Text快捷键与插件介绍
Sublime Text快捷键: Ctrl+Shift+P:打开命令面板Ctrl+P:搜索项目中的文件Ctrl+G:跳转到第几行Ctrl+W:关闭当前打开文件Ctrl+Shift+W:关闭所有打开文件 ...
- C# 多线程编程,传参,接受返回值
C# 多线程编程,传参,接受返回值 今天将多线程的知识有回顾了下,总结了几点: 新建一个线程(无参数,无返回值) Thread th = new Thread(new ThreadStart(Prin ...
- ABP框架系列之四:(Repositories-仓库)
"Mediates between the domain and data mapping layers using a collection-like interface for acce ...
- ABP框架系列之五十二:(Validating-Data-Transfer-Objects-验证数据传输对象)
Introduction to validation Inputs of an application should be validated first. This input can be sen ...
- [ 9.29 ]CF每日一题系列—— 765B字符串规律
Description: 遇到了ogo可以变成***如果ogo后面有go统统忽略,输出结果 Solution: 哎如果我一开始对题意的解读如上的话,就不会被整的那么麻烦了 Code: #include ...
- Ngui使用随心记
Ngui的一些基础使用心得! BB:首先BB一下我觉得NGUI和UGUI哪个好?我首推UGUI,先不说是官方内置,在使用的方便性上也要好很多,而且NGUI停止更新了!还有就是NGUI有BUG! Ngu ...
- C#基础-gc算法
众所周知,c++是需要程序员手动管理内存的,然而手动释放内存很容易被程序员遗漏,从而导致资源浪费或内存泄露.为解决这个问题,垃圾回收器诞生了,代替程序员自动管理内存的释放.至于gc算法则是垃圾回收器清 ...
- python中除法的注意事项
使用python数据处理,代码如下: import numpy as np fs = 5 ts = np.arange(-1,1+1/fs,1/fs) 发现了这样一个错误: Traceback (mo ...
- Maven - 实例-1-手工创建Maven项目
1- 根据包结构创建maven项目目录 TestMaven - src - src/main/java/anliven/testmaven01/HelloMaven.java - src/test/j ...