Spring AMQP实现RabbitMQ的5种消息模式

2024-01-09 06:48

本文主要是介绍Spring AMQP实现RabbitMQ的5种消息模式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、简单模式

 

简单模式是最简单的消息模式,它包含一个生产者、一个消费者和一个队列。生产者向队列里发送消息,消费者从队列中获取消息并消费。

 

1. 创建队列simple.hello2

import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class SimpleRabbitConfig {@Beanpublic Queue simpleHello(){return new Queue("simple.hello");}}

2. 创建生产者

@Component
public class SimpleHelloSender {@Autowiredprivate AmqpTemplate amqpTemplate;public void sendMessage(){//发送消息hello simpleamqpTemplate.convertAndSend("simple.hello", "hello simple");}
}

3. 创建消费者

@Component
@RabbitListener(queues = "simple.hello")
public class SimpleHelloReceiver {@RabbitHandlerpublic void handle(String in){System.out.println("我收到了消息:" + in);}
}

4. 测试类

@RestController
@RequestMapping("/rabbit")
public class RabbitTestController {@Autowiredprivate SimpleHelloSender simpleHelloSender;@RequestMapping("/simple")public String simpleSend(){simpleHelloSender.sendMessage();return "消息发送成功";}}

5. 测试结果

二、工作模式(为了方便,和simple方法写在了一起)

       工作模式是指向多个互相竞争的消费者发送消息的模式,它包含一个生产者、两个消费者和一个队列。两个消费者同时绑定到一个队列上去,当消费者获取消息处理耗时任务时,空闲的消费者从队列中获取并消费消息。

 

1. 创建队列

@Bean
public Queue workQueue(){return new Queue("work.queue");
}

2. 创建生产者

public void sendWorkMessage(){amqpTemplate.convertAndSend("work.queue", "hello work queue");
}

3. 创建消费者

@Component
public class RabbitReceiver {// 3个方法同时监听同一个队列@RabbitListener(queues = "work.queue")public void processOne(String in) {System.out.println("work.queue1" + in);}@RabbitListener(queues = "work.queue")public void processTwo(String in) {System.out.println("work.queue2" + in);}@RabbitListener(queues = "work.queue")public void processThree(String in) {System.out.println("work.queue3" + in);}}

4. 测试类

@RequestMapping("/work")
public String workSend(){simpleHelloSender.sendWorkMessage();return "消息发送成功";
}

5. 测试结果,发现是轮询消费,空闲的消费者轮询消费信息,也就是谁有空那就是谁去做事。

三、发布/订阅者模式(Publish/Subscribe)

       发布/订阅模式是指同时向多个消费者发送消息的模式(类似广播的形式),它包含一个生产者、两个消费者、两个队列和一个交换机。两个消费者同时绑定到不同的队列上去,两个队列绑定到交换机上去,生产者通过发送消息到交换机,所有消费者接收并消费消息。

 

1. 创建队列

@Configuration
public class FanoutRabbitConfig {// 创建队列@Beanpublic Queue publishOne(){return new Queue("queue.publish.one");}@Beanpublic Queue publishTwo(){return new Queue("queue.publish.two");}@Beanpublic Queue publishThree(){return new Queue("queue.publish.three");}// 创建交换机@Beanpublic FanoutExchange publishExchange(){return new FanoutExchange("publishExchange");}//绑定队列(不用指定routing key),参数名字要和bean名字一致@BeanBinding bingingPublishOne(Queue publishOne, FanoutExchange publishExchange){return BindingBuilder.bind(publishOne).to(publishExchange);}@BeanBinding bindingPublishTwo(Queue publishTwo, FanoutExchange publishExchange){return BindingBuilder.bind(publishTwo).to(publishExchange);}@BeanBinding bindingPublishThree(Queue publishThree, FanoutExchange publishExchange){return BindingBuilder.bind(publishThree).to(publishExchange);}
}

2. 创建生产者

public void sendPublishMessage(){amqpTemplate.convertAndSend("publishExchange","","发布消息");
}

3. 创建消费者

@RabbitListener(queues = "queue.publish.one")
public void publishOne(String in) {System.out.println("queue.publish.one:" + in);
}@RabbitListener(queues = "queue.publish.two")
public void publishTwo(String in) {System.out.println("queue.publish.two:" + in);
}@RabbitListener(queues = "queue.publish.three")
public void publishThree(String in) {System.out.println("queue.publish.three:" + in);
}

4. 测试类

@RequestMapping("/pulish")
public String pulishSend(){simpleHelloSender.sendPublishMessage();return "消息发送成功";
}

5. 测试结果(所有订阅者都能收到消息)

四、路由模式

       路由模式是可以根据路由键选择性给多个消费者发送消息的模式,它包含一个生产者、两个消费者、两个队列和一个交换机。两个消费者同时绑定到不同的队列上去,两个队列通过路由键绑定到交换机上去,生产者发送消息到交换机,交换机通过路由键转发到不同队列,队列绑定的消费者接收并消费消息。

1. 创建队列

@Configuration
public class RoutingRabbitConfig {// 创建队列@Beanpublic Queue routingOne(){return new Queue("queue.routing.one");}@Beanpublic Queue routingTwo(){return new Queue("queue.routing.two");}@Beanpublic Queue routingThree(){return new Queue("queue.routing.three");}// 创建交换机@Beanpublic DirectExchange directExchange(){return new DirectExchange("routingExchange");}//绑定队列(@BeanBinding bingingRoutingOne(Queue routingOne, DirectExchange directExchange){return BindingBuilder.bind(routingOne).to(directExchange).with("1");}@BeanBinding bingingRoutingTwo(Queue routingTwo, DirectExchange directExchange){return BindingBuilder.bind(routingTwo).to(directExchange).with("2");}@BeanBinding bingingRoutingThree(Queue routingThree, DirectExchange directExchange){return BindingBuilder.bind(routingThree).to(directExchange).with("3");}
}

2. 创建生产者

public void sendRoutingMessage(String type){amqpTemplate.convertAndSend("routingExchange",type,"发布Routing消息" + type);
}

3. 创建消费者

@RabbitListener(queues = "queue.routing.one")
public void routingOne(String in) {System.out.println("queue.routing.one:" + in);
}@RabbitListener(queues = "queue.routing.two")
public void routingTwo(String in) {System.out.println("queue.routing.two:" + in);
}@RabbitListener(queues = "queue.routing.three")
public void routingThree(String in) {System.out.println("queue.routing.three:" + in);
}

4. 测试类

@RequestMapping("/routing/{type}")
public String routingSend(@PathVariable String type){simpleHelloSender.sendRoutingMessage(type);return "发送成功";
}

5. 测试结果(请求参数分别为1,2,3,只有路由键对应上的队列才能消费)

五、主题模式(Topic)

       主题模式是可以根据路由键匹配规则选择性给多个消费者发送消息的模式,它包含一个生产者、两个消费者、两个队列和一个交换机。两个消费者同时绑定到不同的队列上去,两个队列通过路由键匹配规则绑定到交换机上去,生产者发送消息到交换机,交换机通过路由键匹配规则转发到不同队列,队列绑定的消费者接收并消费消息。

 

1. 创建队列

@Configuration
public class TopicRabbitConfig {// 创建队列@Beanpublic Queue topicOne(){return new Queue("queue.topic.one");}@Beanpublic Queue topicTwo(){return new Queue("queue.topic.two");}@Beanpublic Queue topicThree(){return new Queue("queue.topic.three");}// 创建交换机@Beanpublic TopicExchange topicExchange(){return new TopicExchange("topicExchange");}//绑定队列(@BeanBinding bingingTopicOne(Queue topicOne, TopicExchange topicExchange){return BindingBuilder.bind(topicOne).to(topicExchange).with("#.error");}@BeanBinding bingingTopicTwo(Queue topicTwo, TopicExchange topicExchange){return BindingBuilder.bind(topicTwo).to(topicExchange).with("#.log");}@BeanBinding bingingTopicThree(Queue topicThree, TopicExchange topicExchange){return BindingBuilder.bind(topicThree).to(topicExchange).with("test.#.time");}}

2. 创建生产者

public void sendTopicMessage(String topic){amqpTemplate.convertAndSend("topicExchange",topic,"发布Topic消息" + topic);
}

3. 创建消费者

@RabbitListener(queues = "queue.topic.one")
public void topicOne(String in) {System.out.println("queue.topic.one:" + in);
}@RabbitListener(queues = "queue.topic.two")
public void topicTwo(String in) {System.out.println("queue.topic.two:" + in);
}@RabbitListener(queues = "queue.topic.three")
public void topicThree(String in) {System.out.println("queue.topic.three:" + in);
}

4. 测试类

@RequestMapping("/topic/{type}")
public String send(@PathVariable String type){simpleHelloSender.sendTopicMessage(type);return "发送成功";
}

5. 测试结果

这篇关于Spring AMQP实现RabbitMQ的5种消息模式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/586249

相关文章

C++对象布局及多态实现探索之内存布局(整理的很多链接)

本文通过观察对象的内存布局,跟踪函数调用的汇编代码。分析了C++对象内存的布局情况,虚函数的执行方式,以及虚继承,等等 文章链接:http://dev.yesky.com/254/2191254.shtml      论C/C++函数间动态内存的传递 (2005-07-30)   当你涉及到C/C++的核心编程的时候,你会无止境地与内存管理打交道。 文章链接:http://dev.yesky

Java五子棋之坐标校正

上篇针对了Java项目中的解构思维,在这篇内容中我们不妨从整体项目中拆解拿出一个非常重要的五子棋逻辑实现:坐标校正,我们如何使漫无目的鼠标点击变得有序化和可控化呢? 目录 一、从鼠标监听到获取坐标 1.MouseListener和MouseAdapter 2.mousePressed方法 二、坐标校正的具体实现方法 1.关于fillOval方法 2.坐标获取 3.坐标转换 4.坐

Spring Cloud:构建分布式系统的利器

引言 在当今的云计算和微服务架构时代,构建高效、可靠的分布式系统成为软件开发的重要任务。Spring Cloud 提供了一套完整的解决方案,帮助开发者快速构建分布式系统中的一些常见模式(例如配置管理、服务发现、断路器等)。本文将探讨 Spring Cloud 的定义、核心组件、应用场景以及未来的发展趋势。 什么是 Spring Cloud Spring Cloud 是一个基于 Spring

Javascript高级程序设计(第四版)--学习记录之变量、内存

原始值与引用值 原始值:简单的数据即基础数据类型,按值访问。 引用值:由多个值构成的对象即复杂数据类型,按引用访问。 动态属性 对于引用值而言,可以随时添加、修改和删除其属性和方法。 let person = new Object();person.name = 'Jason';person.age = 42;console.log(person.name,person.age);//'J

java8的新特性之一(Java Lambda表达式)

1:Java8的新特性 Lambda 表达式: 允许以更简洁的方式表示匿名函数(或称为闭包)。可以将Lambda表达式作为参数传递给方法或赋值给函数式接口类型的变量。 Stream API: 提供了一种处理集合数据的流式处理方式,支持函数式编程风格。 允许以声明性方式处理数据集合(如List、Set等)。提供了一系列操作,如map、filter、reduce等,以支持复杂的查询和转

如何开启和关闭3GB模式

https://jingyan.baidu.com/article/4d58d5414dfc2f9dd4e9c082.html

Java面试八股之怎么通过Java程序判断JVM是32位还是64位

怎么通过Java程序判断JVM是32位还是64位 可以通过Java程序内部检查系统属性来判断当前运行的JVM是32位还是64位。以下是一个简单的方法: public class JvmBitCheck {public static void main(String[] args) {String arch = System.getProperty("os.arch");String dataM

详细分析Springmvc中的@ModelAttribute基本知识(附Demo)

目录 前言1. 注解用法1.1 方法参数1.2 方法1.3 类 2. 注解场景2.1 表单参数2.2 AJAX请求2.3 文件上传 3. 实战4. 总结 前言 将请求参数绑定到模型对象上,或者在请求处理之前添加模型属性 可以在方法参数、方法或者类上使用 一般适用这几种场景: 表单处理:通过 @ModelAttribute 将表单数据绑定到模型对象上预处理逻辑:在请求处理之前

eclipse运行springboot项目,找不到主类

解决办法尝试了很多种,下载sts压缩包行不通。最后解决办法如图: help--->Eclipse Marketplace--->Popular--->找到Spring Tools 3---->Installed。

JAVA读取MongoDB中的二进制图片并显示在页面上

1:Jsp页面: <td><img src="${ctx}/mongoImg/show"></td> 2:xml配置: <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001