第二篇:SpringBoot2.0整合ActiveMQ
本篇开始将具体介绍SpringBoot如何整合其它项目。
如何创建SpringBoot项目
访问https://start.spring.io/。
依次选择构建工具Maven Project、语言java、Spring Boot版本2.0.5,点击Generate Project下载项目压缩包,解压后倒入到ide中即可。(idea集成了SpringBoot,可直接创建项目)
如何整合ActiveMQ
1、添加spring-boot-starter-activemq依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
2、添加配置
spring.activemq.in-memory=true
spring.activemq.pool.enabled=false
3、添加SampleActiveMQApplication类
@SpringBootApplication
@EnableJms
public class SampleActiveMQApplication {
@Bean
public Queue queue() {
return new ActiveMQQueue("sample.queue");
}
public static void main(String[] args) {
SpringApplication.run(SampleActiveMQApplication.class, args);
}
}
4、添加Consumer类
@Component
public class Consumer {
@JmsListener(destination = "sample.queue")
public void receiveQueue(String text) {
System.out.println(text);
}
}
5、添加Producer类
@Component
public class Producer implements CommandLineRunner {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired
private Queue queue;
@Override
public void run(String... args) throws Exception {
send("Sample message");
System.out.println("Message was sent to the Queue");
}
public void send(String msg) {
this.jmsMessagingTemplate.convertAndSend(this.queue, msg);
}
}
6、启动服务
控制台输出:
Message was sent to the Queue
Sample message
以上例子使用的是SpringBoot内部的ActiveMQ,实际使用时肯定会用外部的ActiveMQ。
如何连接外部的ActiveMQ
1、添加依赖
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
</dependency>
2、修改配置
spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.close-timeout=5000
spring.activemq.in-memory=false
spring.activemq.pool.enabled=true
spring.activemq.pool.max-connections=100
spring.activemq.send-timeout=3000
3、启动服务
首先,确保ActiveMQ已经启动。控制台输出:
Message was sent to the Queue
Sample message
如何使用topic
1、在SampleActiveMQApplication中添加:
@Bean
public Topic topic() {
return new ActiveMQTopic("sample.topic");
}
@Bean
public JmsListenerContainerFactory<?> jmsListenerContainerTopic(ConnectionFactory activeMQConnectionFactory) {
DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
bean.setPubSubDomain(true);
bean.setConnectionFactory(activeMQConnectionFactory);
return bean;
}
2、创建TopicConsumer类
@Component
public class TopicConsumer {
@JmsListener(destination = "sample.topic", containerFactory = "jmsListenerContainerTopic")
public void receiveTopic1(String text) {
System.out.println(text);
}
@JmsListener(destination = "sample.topic", containerFactory = "jmsListenerContainerTopic")
public void receiveTopic2(String text) {
System.out.println(text);
}
}
3、创建TopicProducer类
@Component
public class TopicProducer implements CommandLineRunner {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired
private Topic topic;
@Override
public void run(String... args) throws Exception {
send("Topic message");
System.out.println("Message was sent to the Topic");
}
public void send(String msg) {
this.jmsMessagingTemplate.convertAndSend(this.topic, msg);
}
}
4、启动服务
控制台输出:
Message was sent to the Topic
Topic message
Topic message
本篇到此结束,如果读完觉得有收获的话,欢迎点赞、关注、加公众号【贰级天災】,查阅更多精彩历史!!!

第二篇:SpringBoot2.0整合ActiveMQ的更多相关文章
- springboot2.0整合logback日志(详细)
<div class="post"> <h1 class="postTitle"> springboot2.0整合logback日志(详 ...
- SpringBoot2.0 整合 QuartJob ,实现定时器实时管理
一.QuartJob简介 1.一句话描述 Quartz是一个完全由java编写的开源作业调度框架,形式简易,功能强大. 2.核心API (1).Scheduler 代表一个 Quartz 的独立运行容 ...
- SpringBoot2.0 整合 Swagger2 ,构建接口管理界面
一.Swagger2简介 1.Swagger2优点 整合到Spring Boot中,构建强大RESTful API文档.省去接口文档管理工作,修改代码,自动更新,Swagger2也提供了强大的页面测试 ...
- SpringBoot2.0 整合 Dubbo框架 ,实现RPC服务远程调用
一.Dubbo框架简介 1.框架依赖 图例说明: 1)图中小方块 Protocol, Cluster, Proxy, Service, Container, Registry, Monitor 代表层 ...
- SpringBoot2.0 整合 Redis集群 ,实现消息队列场景
本文源码:GitHub·点这里 || GitEE·点这里 一.Redis集群简介 1.RedisCluster概念 Redis的分布式解决方案,在3.0版本后推出的方案,有效地解决了Redis分布式的 ...
- SpringBoot2.0应用(三):SpringBoot2.0整合RabbitMQ
如何整合RabbitMQ 1.添加spring-boot-starter-amqp <dependency> <groupId>org.springframework.boot ...
- SpringBoot2.0整合fastjson的正确姿势
SpringBoot2.0如何集成fastjson?在网上查了一堆资料,但是各文章的说法不一,有些还是错的,可能只是简单测试一下就认为ok了,最后有没生效都不知道.恰逢公司项目需要将J ...
- Springboot2.0整合Redis(注解开发)
一. pom.xm文件引入对应jar包 <dependency> <groupId>org.springframework.boot</groupId> <a ...
- Activiti工作流框架学习笔记(二)之springboot2.0整合工作流Activiti6.0
以前在工作当中做过不少与工作流Activiti有关的工作,当时都是spring集成activiti5.22的项目,现在回过头去看,其实版本已经稍微老了,因此,基于先前的工作经验,决定用较新版本的技术来 ...
随机推荐
- uboot——git代码仓
1,注册GitHub帐号,创建GitHub项目代码仓库 https://www.cnblogs.com/LoTGu/p/6075994.html 参考其第二段,注册账号,设置仓库. 2,上传代码 测试 ...
- centos 7 安装pip
1.首先检查centos 有没有安装python-pip 包, >>yum install python-pipnotice:NO package python-pip available ...
- Quartz.Net进阶之三:SimpleTrigger详述
以前都是将所有的内容放在一篇文章里,就会导致文章很长,对于学习的人来说,有时候这也是一个障碍.所以,以后我的写作习惯,我就会把我写的文章缩短,但是内容不会少,内容更集中.这样,学习起来也不会很累,很容 ...
- vs2015调试断点无法命中
问题描述: 我是从vc6.0的原工程导入的,用vs2015编译,在断点调试的时候遇到问题,断点无法命中; 修改方法:工程->属性->C/C++ 常规->调试信息格式->设置为C ...
- mvc输出json时报HTTP Status 406错误
1.mvc输出json时报HTTP Status 406错误 错误纠结了2天时间,今天总与整对了,少jackson-databind引用 对于Spring 4.1.x 和以上, jackson-dat ...
- loadtxt()函数的糟心历程
原计划:导入一个csv文件,然后算出平均值 import numpy as np c=np.loadtxt('d:\python36\data.csv', delimiter=',', usecols ...
- ios下面的按钮和inout框
在ios系统中,按钮和输入框,会默认给你加一个圆角和阴影,可以用css去掉这个自带的属性 input[type=button], input[type=submit], input[type=file ...
- Mac could not read from remote repository
IDE clone数据的时候要使用SSH,不使用HTTPS,就解决了问题
- Clion pycharm激活码(可使用到2019年2月)
D87IQPUU3Q-eyJsaWNlbnNlSWQiOiJEODdJUVBVVTNRIiwibGljZW5zZWVOYW1lIjoiTnNzIEltIiwiYXNzaWduZWVOYW1lIjoiI ...
- Maths | 离散K-L变换/ 主成分分析法
目录 1. 概述 2. K-L变换方法和原理推导 2.1. 向量分解 2.2. 向量估计及其误差 2.3. 寻找最小误差对应的正交向量系 3. K-L变换高效率的本质 4. PCA在编.解码应用上的进 ...