SpringBoot JMS(ActiveMQ) 使用实践
ActiveMQ
1. 下载windows办的activeMQ后,在以下目录可以启动:

2. 启动后会有以下提示

3. 所以我们可以通过http://localhost:8161访问管理页面,通过tcp://localhost:61616来连接消息服务器,用到的用户名和密码都在以下文件中(默认为admin=admin)

springboot连接ActiveMQ
1. 加入依赖:
spring-boot-starter-activemq
2. 配置连接属性:
spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.user=admin
spring.activemq.password=admin
spring.activemq.pool.enabled=false
消息的发送和接收
生产者/消费者模式
1. 创建生产者
package com.example.demo8activemq.jms;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.jms.Destination;
/**
* @author Created by yawn on 2017-10-26 16:15
*/
@Service
public class Producer {
@Resource
private JmsMessagingTemplate jmsMessagingTemplate;
public void sendMsg(String destinationName, String message) {
System.out.println("============>>>>> 发送queue消息 " + message);
Destination destination = new ActiveMQQueue(destinationName);
jmsMessagingTemplate.convertAndSend(destination, message);
}
}
2. 创建消费者
package com.example.demo8activemq.jms;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Service;
/**
* @author Created by yawn on 2017-10-26 16:15
*/
@Service
public class Consumer {
@JmsListener(destination = "test.queue")
public void receiveMsg(String text) {
System.out.println("<<<<<<============ 收到消息: " + text);
}
}
注意: @JmsListener是一个可重复的注解,在java7及以下版本jdk中,可以使用@JmsListeners代替它。
3. 测试类
@RunWith(SpringRunner.class)
@SpringBootTest
public class Demo8ActivemqApplicationTests {
@Resource
private Producer producer;
@Test
public void contextLoads() {
for (int i = 0; i < 10; i++) {
producer.sendMsg("test.queue", "Queue Message " + i);
}
}
}
4. 运行测试

发布/订阅模式
1. 发布话题
package com.example.demo8activemq.jms;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.jms.Destination;
/**
* @author Created by yawn on 2017-10-28 17:09
*/
@Service
public class Publisher {
@Resource
private JmsMessagingTemplate jmsMessagingTemplate;
public void publish(String destinationName, String message) {
Destination destination = new ActiveMQTopic(destinationName);
System.out.println("============>>>>> 发布topic消息 " + message);
jmsMessagingTemplate.convertAndSend(destination, message);
}
}
2. 订阅话题
package com.example.demo8activemq.jms;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Service;
/**
* @author Created by yawn on 2017-10-28 17:15
*/
@Service
public class Subscriber {
@JmsListener(destination = "test.topic", containerFactory = "myJmsContainerFactory")
public void subscribe(String text) {
System.out.println("===========<<<<<<<<收到订阅的消息" + text);
}
}
注意: 在pub/sub模式中,对消息的监听需要对containerFactory进行以下配置
@Bean
JmsListenerContainerFactory<?> myJmsContainerFactory(ConnectionFactory connectionFactory){
SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
factory.setPubSubDomain(true);
return factory;
}
3. 测试
@Test
public void test() {
for (int i = 0; i < 10; i++) {
publisher.publish("test.topic", "Topic Message " + i);
}
}

应用
按照以上步骤,在springboot中很容易就实现类两种模式的消息发送和接收。但是jms具体的应用场景是在不同的应用程序之间,生产者和消费者往往是在不同的应用中的。此外,以上例子中的消息我们只发送字符串,其实还可以发送Object类型的消息,甚至可以使用messageCreator自定义消息的转换,而不使用convertAndSend方法默认转换。
多个应用程序之间发送消息
1. 先使用一个只有发送者,没有消费者或订阅者的应用发送两类消息各十条


2. 我们打开localhost:8161,可以看到


两类都曾有十条消息入队,但只有queues中还存留10条消息。
3. 现在我们启动包含消费者和订阅者的应用程序

果然,只有消费者收到了queues中的消息。
这说明订阅者接收topic是需要在topic发布之前订阅;而生产/消费模式下,消息发出后会存放在队列中,等待消费者消费。
4. 我们先启动两个包含订阅者和消费者的程序,再发布消息



两个订阅者都收到 topic message 1~9, 而消费者中,一个收到消息 1、3、5、7、9,另一个收到0、2、4、6、8。
这说明有多个消息接收者时,生产/消费模式下多个消费者会轮流消费队列中的消息,而pub/sub模式下所有订阅者都会得到所有的消息。
以上就是在多个应用程序之间验证了发布/订阅模式和生产/消费模式的不同特点。
SpringBoot JMS(ActiveMQ) 使用实践的更多相关文章
- 简单记录下springboot+jms+activemq
1. 安装ActiveMQ 到Apache官方网站下载最新的ActiveMQ的安装包,并解压到本地目录下后运行 2. pom.xml引入 springboot配置文件中填写相关配置 3.创建生产者 ...
- SpringBoot使用JMS(activeMQ)的两种方式 队列消息、订阅/发布
刚好最近同事问我activemq的问题刚接触所以分不清,前段时间刚好项目中有用到,所以稍微整理了一下,仅用于使用 1.下载ActiveMQ 地址:http://activemq.apache.org/ ...
- Web项目容器集成ActiveMQ & SpringBoot整合ActiveMQ
集成tomcat就是随项目启动而启动tomcat,最简单的方法就是监听器监听容器创建之后以Broker的方式启动ActiveMQ. 1.web项目中Broker启动的方式进行集成 在这里采用Liste ...
- springboot与ActiveMQ整合
前言 很多项目, 都不是一个系统就做完了. 而是好多个系统, 相互协作来完成功能. 那, 系统与系统之间, 不可能完全独立吧? 如: 在学校所用的管理系统中, 有学生系统, 资产系统, 宿舍系统等等. ...
- spring boot整合JMS(ActiveMQ实现)
pom依赖如下: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="ht ...
- SpringBoot集成ActiveMQ
前面提到了原生API访问ActiveMQ和Spring集成ActiveMQ.今天讲一下SpringBoot集成ActiveMQ.SpringBoot就是为了解决我们的Maven配置烦恼而生,因此使用S ...
- 使用SpringBoot集成ActiveMQ
SpringBoot是个好东西,好多java常用的东西都被集成进去了 JMS 在 Spring Boot 中的使用 使用Spring/Spring Boot集成JMS的陷阱 Spring-boot J ...
- SpringBoot系列八:SpringBoot整合消息服务(SpringBoot 整合 ActiveMQ、SpringBoot 整合 RabbitMQ、SpringBoot 整合 Kafka)
声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念:SpringBoot 整合消息服务 2.具体内容 对于异步消息组件在实际的应用之中会有两类: · JMS:代表作就是 ...
- SpringBoot整合ActiveMQ快速入门
Spring Boot 具有如下特性: 为基于 Spring 的开发提供更快的入门体验 开箱即用,没有代码生成,也无需 XML 配置.同时也可以修改默认值来满足特定的需求. 提供了一些大型项目中常见的 ...
随机推荐
- iOS监听模式系列之关于delegate(代理,委托)的学习
首先,大家应该都明白的是委托是协议的一种,顾名思义,就是委托他人帮自己去做什么事.也就是当自己做什么事情不方便的时候,就可以建立一个委托,这样就可以委托他人帮自己去实现什么方法. 其次,我简单的总结了 ...
- 如何在Eclipse CDT中编译含有多个main函数的项目
最近在杭电ACM上做题,使用的C++工具是Eclipse,但是Eclipse CDT不能同时存在多个main函数的文件,上网也搜了很多资料,但是按他们的步骤来,还是不能实现自己想要的效果.经过一下午的 ...
- 闫燕飞:Kafka的高性能揭秘及优化
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文首发在云+社区,未经许可,不得转载. 大家下午好,我是来自腾讯云基础架构部ckafka团队的高级工程师闫燕飞.今天在这里首先为大家先分享 ...
- 如何在asp.net mvc 中使用Autofac 控制反转(Ioc)
前言 最近看了一些关于Ioc方面的开源项目,里面的类跳来转去,看的迷迷糊糊的.项目里根本不需要这么“复杂的”设计,只需简单完成Ico,达到解耦的目的,并且能高效的完成项目.于是参考autofac的官网 ...
- use ECharts with Angular 2 and TypeScript
https://stackoverflow.com/questions/38158318/is-it-possible-to-use-echarts-baidu-with-angular-2-and- ...
- Django中的Python高级特性
本小文的内容实际是作为<Pro Django>第二版第二章的读书笔记简单总结. 1.类的构建:元类,使用带元类的基类----这个特性的案例主要就是models.Model类,用这种方式高效 ...
- 人脸姿态校正算法 附完整C++示例代码
在一些特殊情况下,经常需要依据图像中的人脸,对图片进行倾斜矫正. 例如拍照角度幅度过大之类的情况,而进行人工矫正确实很叫人头大. 那是不是可以有一种算法,可以根据人脸的信息对图片进行角度的修复呢? 答 ...
- Install and Configure Apache Kafka on Ubuntu 16.04
https://devops.profitbricks.com/tutorials/install-and-configure-apache-kafka-on-ubuntu-1604-1/ by hi ...
- 构建基础的SpringMVC+Hibernate+SpringloC项目
一. SpringMVC 阅读我的上一篇文章<使用MyEclipse2015构建SpringMVC项目>,知道基本的构建方法,先构建一个纯springmvc项目,再对web.xml按照本文 ...
- mac 登录亚马逊云服务器报错:Permission denied (publickey).
申请的亚马逊云服务器EC2,实例为ubuntu系统 一.打开终端,定位到放置密钥的文件夹: 二.确保私有秘钥不是公开可见的: p.p1 { margin: 0.0px 0.0px 0.0px 0.0p ...