Spring Boot与ActiveMQ的集成
Spring Boot对JMS(Java Message Service,Java消息服务)也提供了自动配置的支持,其主要支持的JMS实现有ActiveMQ、Artemis等。本节中,将以ActiveMQ为例来讲解下Spring Boot与ActiveMQ的集成使用。
在Spring Boot中,已经内置了对ActiveMQ的支持。要在Spring Boot项目中使用ActiveMQ,只需在pom.xml中添加ActiveMQ的起步依赖即可。
添加ActiveMQ起步依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
创建消息队列对象:
在Application.java中编写一个创建消息队列的方法,其代码如下所示。
/**
* 创建消息队列的方法
*/
@Bean
public Queue queue() {
return new ActiveMQQueue("active.queue");
}
创建消息生产者:
创建一个队列消息的控制器类QueueController,并在类中编写发送消息的方法
package com.xc.springboot.controller; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import javax.jms.Queue; /**
* 队列消息的控制器类QueueController
*/
@RestController
public class QueueController { @Autowired
private JmsMessagingTemplate jmsMessagingTemplate; @Autowired
private Queue queue; /**
* 消息生产者
*/
@RequestMapping("/send")
public void send() {
// 指定消息发送的目的地及内容
jmsMessagingTemplate.convertAndSend(queue, "新发送的消息!");
} }
创建消息监听者:
创建一个客户控制器类CustomerController,并在类中编写监听和读取消息的方法
package com.xc.springboot.controller; import org.springframework.jms.annotation.JmsListener;
import org.springframework.web.bind.annotation.RestController; /**
* 客户控制器
*/
@RestController
public class CustomerController { /**
* 监听和读取消息
*/
@JmsListener(destination = "active.queue")
public void readActiveQueue(String message) {
System.out.println("接收到:" + message);
}
}
在上述代码中,@JmsListener是Spring 4.1所提供的用于监听JMS消息的注解,该注解的属性destination用于指定要监听的目的地。本案例中监听的是active.queue中的消息。
启动项目,测试应用:
启动Spring Boot项目,在浏览器中输入地址http://localhost:8081/send后,控制台将显示所接收到的消息
接收到:新发送的消息!
在实际开发中,ActiveMQ可能是单独部署在其他机器上的,如果要使用它,就需要实现对它的远程调用。要使用远程中的ActiveMQ其实很简单,只需在配置文件中指定ActiveMQ的远程主机地址以及服务端口号,其配置代码如下:
spring.activemq.broker-url=tcp://192.168.2.100:61616
在上述配置中,192.168.2.100是远程主机的IP地址,61616是ActiveMQ的服务端口号。
Spring Boot与ActiveMQ的集成的更多相关文章
- Spring Boot学习笔记——Spring Boot与ActiveMQ的集成
Spring Boot对JMS(Java Message Service,Java消息服务)也提供了自动配置的支持,其主要支持的JMS实现有ActiveMQ.Artemis等.这里以ActiveMQ为 ...
- 如何集成 Spring Boot 和 ActiveMQ?
对于集成 Spring Boot 和 ActiveMQ,我们使用依赖关系. 它只需要很少的配置,并且不需要样板代码.
- Spring Boot入门 and Spring Boot与ActiveMQ整合
1.Spring Boot入门 1.1什么是Spring Boot Spring 诞生时是 Java 企业版(Java Enterprise Edition,JEE,也称 J2EE)的轻量级代替品.无 ...
- Spring Boot微服务如何集成fescar解决分布式事务问题?
什么是fescar? 关于fescar的详细介绍,请参阅fescar wiki. 传统的2PC提交协议,会持有一个全局性的锁,所有局部事务预提交成功后一起提交,或有一个局部事务预提交失败后一起回滚,最 ...
- 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等后续再去学习 ...
- Spring Boot与ActiveMQ整合
Spring Boot与ActiveMQ整合 1使用内嵌服务 (1)在pom.xml中引入ActiveMQ起步依赖 <dependency> <groupId>org.spri ...
- Spring Boot 2.0 快速集成整合消息中间件 Kafka
欢迎关注个人微信公众号: 小哈学Java, 每日推送 Java 领域干货文章,关注即免费无套路附送 100G 海量学习.面试资源哟!! 个人网站: https://www.exception.site ...
- spring boot 整合activemq
1 Spring Boot与ActiveMQ整合 1.1使用内嵌服务 (1)在pom.xml中引入ActiveMQ起步依赖 <properties> <spring.version& ...
随机推荐
- 4.3 axios
axios全局拦截器:
- Django --- 常用字段及参数
1 ORM字段 AutoField int自增列,必须填入参数 primary_key=True.当model中如果没有自增列,则自动会创建一个列名为id的列. IntegerField 一个整数类型 ...
- NOIP 2017 PJ
T1:水 T2:水 T3:水 T4:水,二分+DP检测+单调队列优化,然而优化写炸了,还没暴力分高 所以爆炸 (民间)100 + 100 + 100 + 10 = 310 GAME OVER
- LightOJ - 1323 - Billiard Balls(模拟)
链接: https://vjudge.net/problem/LightOJ-1323 题意: You are given a rectangular billiard board, L and W ...
- am335x system upgrade kernel f-ram fm25l16b(十六)
1 Scope of Document This document describes SPI F-RAM hardware design 2 Requiremen 2.1 ...
- AtCoder Grand Contest 019 题解
传送门 \(A\) 咕咕 int a,b,c,d,n,t; int main(){ scanf("%d%d%d%d%d",&a,&b,&c,&d,& ...
- Python和多线程(multi-threading)。这是个好主意码?列举一些让Python代码以并行方式运行的方法。
Python并不支持真正意义上的多线程.Python中提供了多线程包,但是如果你想通过多线程提高代码的速度,使用多线程包并不是个好主意.Python中有一个被称为Global Interpreter ...
- 菜鸟的算法入门:java的链表操作
从C语言的指针开始,我的算法之路就结束了! 今天为了找个好的实习,不得不捡起来,写了三年的web,算法落下了太多了 今天在leetcode上刷题,难在了一个简单的链表上,因此记录一下 题目:给定两个非 ...
- redis-migrate-tool
一.简介 redis-migrate-tool是在redis之间迁移数据的一个方便且有用的工具.他会已服务方式不断同步两边的数据.等到合适时间,中断redis读写,对比双方数据,再替换redis地址即 ...
- Redis哨兵参数
一.常用命令 sentinel的基本状态信息INFO 列出所有被监视的主服务器,以及这些主服务器的当前状态SENTINEL masters 列出指定主redis的从节点状态情况SENTINEL sla ...