SpringBoot 整合 ActiveMQ|学习笔记

简介: 快速学习 SpringBoot 整合 ActiveMQ

开发者学堂课程【SpringBoot 实战教程  SpringBoot 整合 ActiveMQ】学习笔记,与课程紧密联系,让用户快速学习知识。

课程地址:https://developer.aliyun.com/learning/course/651/detail/10816


SpringBoot 整合 ActiveMQ


1、ActiveMQ 是符合 gms 规范的一个消息管理者

2、以下是 springboot 提供的架包依赖放入工程中创建好工程做好 web 依赖

<!--整合 ActiveMQ 的依赖-->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-activemq</artifactId>

</dependency>

3、在 liunx 中安装好 ActiveMQ进行启动192.168.25.129是服务器地址输入root@ubuntu: , opt , apache activemq-5.12.0/bin# . /activemq status可以看到 activeMQ is running (pid' 1915 ‘)想使用 activeMQ需要配置 activeMQ 的 IP 地址要知道它的 IP 地址需要在全局配置文件中进行配置,application.properties。

输入以下代码

spring.activemq.broker-url=tcp://192.168.25.129:61616连接 activeMQ 的 IP 地址端口号是61616,192.168.25.129是 linux 的 IP 地址

spring.activemq.in-memory=true  内存

spring.activemq.user=admin访问 activemq 后台系统的用户名

spring.activemq.password=admin 访问 activemq 后台系统的密码

#如果此处设置为 true,需要加如下面的依赖包,否则会自动配置失败JmsMessagingTemplate

spring.activemq.pool.enabled=false指的是 activemq 的连接池如果设置为false不使用连接池就不需要做依赖

<dependency>

<groupId>org.apache.activemq</groupId>

<artifactId>activemq-pool</artifactId>

</dependency>

4、把代码加到配置文件中后如何实现消息的发送和消息的接收以点对点为例首先创建消息的发送者,Producer。

如何实现消息发送

@Component 创建对象加注解

Public class Producer{

@Autowired直接注入

private JmsMessagingTemplate jmsTemplate;消息发送的模版

//发送消息的功能

public void sendMessage (destinationdesstringmessage) {需要有消息发送的目的地在 gms 中的接口规范是 destination目的地可能是变化所以定义为单数消息的内容也是变化的所以都作为单数

jmsTemplate . convertAndSend (des, message )选择合适的参数发送目的地的参数使用 jmsTemplate 实现了消息的发送

5、写一个消费者也就是消息的接受者 Consumer

创建对象接收消息有一个特点它需要时刻监听着是否有消息

@Component

public class Consumer{

@JmsListener ( destination=‘’‘’)时刻监听是否有消息有很多消息队列所以需要指明哪个队列中指明消息接收的目的地

public void receiveMsg (String text )

如何接收到信息发送是字符串类型定义字符串参数这样发送的消息就传给text

{

System. out. println (text+’’.......’’);输出消息

}

6、写一个 controller实现消息的发送testcontroller

使用 producer 调用 sendmessage 实现消息的发送所以需要注入 producer发送消息需要指明目的地消息发送的目的地创建好后让 producer 调用 sendmessage 方法进行发送

package com. qianfeng. controller;

import org . springframework. beans. factory . annotation . Autowired;

import org . springframework. stereotype . Controller;

import com. qianfeng . activemq. Producer ;

@Controller

public class TestController {

@Autowired

Private Producer producer;

@RequestMapping ("/activemq")

@ResponseBody

public Stringtests ()

//点对点消息发的消息只能被一个进行接收

Destination des =newActiveMQQueue (‘’myqueues‘’)给消息队列取个名字

producer . sendMessage (des, "hello");

}

Return’’ok’’;

}

7、消息的发送者接受者以及 controller 都需要被扫描在启动类中扫描包

@SpringBootApplication(scanBasePackages="com. qianfeng")

8、在发消息时ActiveMQ 的名字是 myqueues接收消息需要从 myqueues 里进行接收接收方要时刻监听着是否有消息监听的是 myqueues 队列名字指明一下目的地

@JmsListener (destination= "myqueues")

public voidreceiveMsg (String text)

{

System. out. println (text+"......") ;

}

9、进行启动启动成功controller 的访问路径是 activemq输入localhost:8080/activemq可以看到 ok

image.png

查看控制台可以看到它有收到三次消息发送了三次消息三次消息都是 hello这就是在 springboot 中整合 activemq 的方式

image.png

相关文章
|
消息中间件 Java Maven
ActiveMQ系列:结合SpringBoot,基于 application.xml 使用ActiveMQ
ActiveMQ系列:结合SpringBoot,基于 application.xml 使用ActiveMQ
94 0
ActiveMQ系列:结合SpringBoot,基于 application.xml 使用ActiveMQ
|
SQL 监控 Java
SpringBoot的MyBatis工程配置|学习笔记
快速学习SpringBoot的MyBatis工程配置
190 0
SpringBoot的MyBatis工程配置|学习笔记
|
监控 Java 应用服务中间件
SpringBoot|学习笔记
快速学习SpringBoot
82 0
SpringBoot|学习笔记
|
存储 机器学习/深度学习 IDE
SpringBoot 项目与被开发快速迁移|学习笔记
快速学习 SpringBoot 项目与被开发快速迁移
157 0
SpringBoot 项目与被开发快速迁移|学习笔记
|
安全 Java 应用服务中间件
集中式整合之搭建Springboot环境|学习笔记
快速学习集中式整合之搭建Springboot环境
78 0
集中式整合之搭建Springboot环境|学习笔记
|
安全 Java 关系型数据库
SpringSecurity与SpringBoot在集中式项目中整合步骤说明|学习笔记
快速学习SpringSecurity与SpringBoot在集中式项目中整合步骤说明
108 0
|
消息中间件 Java 测试技术
SpringBoot 集成 JUNIT 测试下单基本流程|学习笔记
快速学习 SpringBoot 集成 JUNIT 测试下单基本流程
155 0
SpringBoot 集成 JUNIT 测试下单基本流程|学习笔记
|
消息中间件 Java RocketMQ
Springboot 集成 Rocketmq 消费者|学习笔记
快速学习 Springboot 集成 Rocketmq 消费者
1259 1
Springboot 集成 Rocketmq 消费者|学习笔记
|
消息中间件 IDE Java
Springboot 集成 Rocketmq 生产者|学习笔记
快速学习 Springboot 集成 Rocketmq 生产者
659 0
Springboot 集成 Rocketmq 生产者|学习笔记
|
Java 程序员 应用服务中间件
初识 SpringBoot|学习笔记
快速学习 初识 SpringBoot
108 0