RabbitMQ是比较常用的AMQP实现,这篇文章是一个简单的Spring boot整合RabbitMQ的教程。
安装ActiveMQ服务器,(也可以不安装,如果不安装,会使用内存mq)
构建Spring boot项目,增加依赖项,只需要添加这一项即可
1
2
3
4
5
|
<!-- 添加acitivemq依赖 -->
< dependency >
< groupId >org.springframework.boot</ groupId >
< artifactId >spring-boot-starter-activemq</ artifactId >
</ dependency >
|
增加Application类
1
2
3
4
5
6
7
|
@SpringBootApplication
@EnableScheduling //使用定时任务发送消息
public class MqTestApplication {
public static void main(String[] args) {
SpringApplication.run(MqTestApplication. class , args);
}
}
|
配置application.yml
1
2
3
4
5
|
spring:
activemq:
broker-url: tcp://127.0.01:61616
packages:
trust-all: true
|
构建一个数据Model,可以发送和消费的数据类型有: String, byte array, Map<String,?>, Serializable object.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
// 如果发送的消息是一个对象,必须implements Serializable接口
public class TModel implements Serializable {
private static final long serialVersionUID = -921008687184331557L;
private int count;
public TModel( int count) {
this .count = count;
}
@Override
public String toString() {
return "TModel [count=" + count + "]" ;
}
}
|
构建Producer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
@Component
public class Producer {
// 在Producer中注入JmsTemplate,我们可以通过这个template发送消息
private final JmsTemplate jmsTemplate;
private int count = 0 ;
@Autowired
public Producer(JmsTemplate jmsTemplate) {
this .jmsTemplate = jmsTemplate;
}
// 这里使用Spring Boot的定时任务发送消息
@Scheduled (fixedRate = 1000 )
public void create() {
// 使用convertAndSend发送消息
jmsTemplate.convertAndSend( "queue1" , new TModel(count++));
}
}
|
构建Consumer
1
2
3
4
5
6
7
|
@Component
public class Consumer {
@JmsListener (destination = "queue1" )
public void comsume(TModel content) {
System.out.println( "recive message from queue1 [" + content + "]" );
}
}
|
特别备注:如果我们的生产者和消费者在不同的Module中时,最好将要消费的数据抽象成公共Module.程序是通过Serializable来序列化和反序列化对象的。必须保证生产者和消费者的对象模型的serialVersionUID是一致的。
项目地址: https://github.com/ldwqh0/active-mq-spring.git
示例:配置rabbitmq ,增加一个队列
1
2
3
4
5
6
7
8
|
@Configuration
public class Aqueue {
@Bean
public Queue queue() {
return new Queue( "good" );
}
}
|
定义一个生产者.
当启用activemq之后,会自动创建一个AmqpTemplate ,可以被注入到任何需要的地方,我们可以通过这个AmqpTemplate发送消息到MQ中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
/**
* 定义一个生产者
* @author LiDong
*/
@RestController
@RequestMapping ( "/test" )
public class SendController {
@Autowired
private AmqpTemplate template;
@GetMapping
public String testSend() {
// 使用AmqpTemplate发送消息
template.convertAndSend( "good" , "good" );
return "success" ;
}
}
|
定义消费者,通过指定RabbitListener(queues='good')指定消费的队列
1
2
3
4
5
6
7
8
9
10
11
|
@Component
public class Consumer {
/**
* 定义一个消费者
* @param message
*/
@RabbitListener (queues = "good" )
public void handler(String message) {
System.out.println( "recive message from " + message);
}
}
|
启动测试,在浏览器中输入 http://localhost:8080/test 即可发送一条消息到队列中。 该对列可以被消费者处理
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:https://www.jianshu.com/p/b95dcc43a8a6
相关文章
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
如何利用Shopyy的功能进行有效的客户关系管理(CRM)?
2025-05-27 76 -
Linux使用libnet实现ARP攻击脚本原理分析以防被攻击
2025-05-27 62 -
2025-05-25 78
-
2025-05-25 94
-
2025-05-25 48