本篇开始将具体介绍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整合ActiveMQ的更多相关文章

  1. springboot2.0整合logback日志(详细)

    <div class="post"> <h1 class="postTitle"> springboot2.0整合logback日志(详 ...

  2. SpringBoot2.0 整合 QuartJob ,实现定时器实时管理

    一.QuartJob简介 1.一句话描述 Quartz是一个完全由java编写的开源作业调度框架,形式简易,功能强大. 2.核心API (1).Scheduler 代表一个 Quartz 的独立运行容 ...

  3. SpringBoot2.0 整合 Swagger2 ,构建接口管理界面

    一.Swagger2简介 1.Swagger2优点 整合到Spring Boot中,构建强大RESTful API文档.省去接口文档管理工作,修改代码,自动更新,Swagger2也提供了强大的页面测试 ...

  4. SpringBoot2.0 整合 Dubbo框架 ,实现RPC服务远程调用

    一.Dubbo框架简介 1.框架依赖 图例说明: 1)图中小方块 Protocol, Cluster, Proxy, Service, Container, Registry, Monitor 代表层 ...

  5. SpringBoot2.0 整合 Redis集群 ,实现消息队列场景

    本文源码:GitHub·点这里 || GitEE·点这里 一.Redis集群简介 1.RedisCluster概念 Redis的分布式解决方案,在3.0版本后推出的方案,有效地解决了Redis分布式的 ...

  6. SpringBoot2.0应用(三):SpringBoot2.0整合RabbitMQ

    如何整合RabbitMQ 1.添加spring-boot-starter-amqp <dependency> <groupId>org.springframework.boot ...

  7. SpringBoot2.0整合fastjson的正确姿势

            SpringBoot2.0如何集成fastjson?在网上查了一堆资料,但是各文章的说法不一,有些还是错的,可能只是简单测试一下就认为ok了,最后有没生效都不知道.恰逢公司项目需要将J ...

  8. Springboot2.0整合Redis(注解开发)

    一. pom.xm文件引入对应jar包 <dependency> <groupId>org.springframework.boot</groupId> <a ...

  9. Activiti工作流框架学习笔记(二)之springboot2.0整合工作流Activiti6.0

    以前在工作当中做过不少与工作流Activiti有关的工作,当时都是spring集成activiti5.22的项目,现在回过头去看,其实版本已经稍微老了,因此,基于先前的工作经验,决定用较新版本的技术来 ...

随机推荐

  1. Quartz.Net进阶之二:关于触发器的更多信息

    与作业一样,触发器相对容易使用,但是在您可以充分利用Quartz.NET之前,确实需要了解和理解各种可自定义的选项. 此外,如前所述,您可以选择不同类型的触发器来满足不同的调度需求. 1.常见触发器属 ...

  2. 30.Mysql常见问题和应用技巧

    30.Mysql常见问题和应用技巧30.1 忘记Mysql的root密码30.2 如何处理MyISAM存储引擎的表损坏 30.2.1 方法一:使用myisamchk工具 30.2.2 方法二:使用SQ ...

  3. spring mvc controller中的参数验证机制(一)

    一.验证用到的注解 @Valid 对传到后台的参数的验证 @BindingResult 配合@Valid使用,验证失败后的返回 二.示例 1.传统方式 @PostMapping public User ...

  4. 《笨方法学Python》加分题1

    1. 让你的脚本再多打印一行 我将这个题目理解为在脚本中增加一个空行,因此,在脚本中增加一条 print “\n”命令即可python中\为转义符,‘\n’为换行, ‘\t’是tab,‘\\’才是普通 ...

  5. Java集合:ConcurrentHashMap原理分析

    集合是编程中最常用的数据结构.而谈到并发,几乎总是离不开集合这类高级数据结构的支持.比如两个线程需要同时访问一个中间临界区(Queue),比如常会用缓存作为外部文件的副本(HashMap).这篇文章主 ...

  6. Note | LaTeX

    目录 一.TeX家族 1. TeX - LaTeX 2. pdfTeX - pdfLaTeX 3. XeTeX - XeLaTeX 4. CTeX - MiKTeX - TeX Live 二.入门 1 ...

  7. 1024 Palindromic Number int_string转换 大整数相加

    A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...

  8. day12_雷神_线程总结

    #线程 1. 多线程理论 0.进程只是一个资源单位,用来隔离资源,从执行角度是主线程. 1.多个线程共享一个进程的数据资源: 2.线程开销小: 2. 开线程的两种方式 0. 站在资源的角度,主进程:执 ...

  9. hiho 第七周 完全背包

    完全背包 #include<iostream> #include<memory.h> #include<cmath> using namespace std; #d ...

  10. Scrum 冲刺博客集合

    Day1 博客链接:http://www.cnblogs.com/coolgirls/p/8869839.html Day2 博客链接:http://www.cnblogs.com/coolgirls ...