第二篇: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的项目,现在回过头去看,其实版本已经稍微老了,因此,基于先前的工作经验,决定用较新版本的技术来 ...
随机推荐
- Spyder设置代码自动补全
1.spyder 代码自动补齐设置方式在tools->preferences->IPython console->advanced Settings 下面,把User the gre ...
- python中None与0、Null、false区别
None是Python中的一个关键字,None本身也是个一个数据类型,而这个数据类型就是None,它可0.空字符串以及false均不一样,这些都只是对象,而None也是一个类. 给个bool测试: v ...
- 《Linux就该这么学》第三天课程
秦时明月经典语录: 王道: 千里挥戈,万众俯首.四海江湖,百世王道.——项羽 今天主要介绍了常用系统工作的命令 如需进一步了解,请前往https://www.linuxcool.com(附带配音) r ...
- qhfl-3 Course模块
课程模块,包括免费课程以及专题课程两个,主要是课程的展示,点击课程进入课程详细页面 根据功能设计表结构 为了方便,每张表在数据库中添加了中文名 from django.db import models ...
- thinkphp 5.1 同时选中多个文件上传
<form id="ajaxform" enctype="multipart/form-data" class="form"> ...
- 02-jQuery的选择器
我们以前在CSS中学习的选择器有: 今天来学习一下jQuery 选择器. jQuery选择器是jQuery强大的体现,它提供了一组方法,让我们更加方便的获取到页面中的元素. 1.jQuery 的基本选 ...
- bzoj4556(sam)
二分答案,(具体可见http://blog.csdn.net/neither_nor/article/details/51669114),然后就是判定问题,sa和sam都可以做,用sam写了一下,先用 ...
- golang注意问题
关于slice 我们都知道slice是在通过参数传递的时候传递的是引用 slice的appen操作是有返回值的,并不改变原值 例如 b := [],,,} c:=append(b, ) // b 不变 ...
- zabbix学习
snmp 默认监控upd161端口 tcp 也有 [root@bogon ~]# netstat -nlutp|grep snmp tcp 0 0 127.0.0.1:199 0.0.0.0:* LI ...
- EF6学习笔记(六续) 复杂数据模型建表测试
测试以下几种模型关系: 1对1或0 . 1对多 . 多对多 1 对 1 或 0 如果直接定义两个模型,相互直接增加导航属性,会提示错误,必须为这个对应关系设定主副关系: public class ...