首页 > 其他分享 >RabbitMQ Topic交换机

RabbitMQ Topic交换机

时间:2023-12-02 20:35:31浏览次数:25  
标签:topic hmall String queue2 RabbitMQ Topic 交换机 msg

 

代码示例:

1. 新建两个队列

 

2. 创建交换机,名字叫 hmall.topic,类型选择 topic

 

3. hmall.topic 交换机绑定第一步的两个队列,绑定过程中填写 RoutingKey

 

 

4. 编写消费者代码监听这两个队列

@RabbitListener(queues = "topic.queue1")
public void listenQueue05(String msg) {
System.out.println("消费者收到了topic.queue1的消息:" + msg);
}
@RabbitListener(queues = "topic.queue2")
public void listenQueue06(String msg) {
System.out.println("消费者收到了topic.queue2的消息:" + msg);
}

5. 编写发送者,给 hmall.topic 交换机发消息

@GetMapping("/mq04")
public void mq04(){
String exchangeName = "hmall.topic";
String msg = "hello, baby";
//三个参数:交换机名称、RoutingKey、要发送的消息
rabbitTemplate.convertAndSend(exchangeName, "Beijing.news", msg);
}

6. 因为 Beijing.news 符合 topic.queue2 队列的 RoutingKey,所以只有 topic.queue2 队列能收到消息

 

标签:topic,hmall,String,queue2,RabbitMQ,Topic,交换机,msg
From: https://www.cnblogs.com/gagaya2/p/17872177.html

相关文章

  • RabbitMQ Direct交换机
     代码示例:1.交换机绑定了两个队列,并给它们设置了RoutingKey2. publisher发送者给Direct交换机发消息时,第二个参数指定RoutingKey:@GetMapping("/mq03")publicvoidmq03(){StringexchangeName="hmall.direct";Stringmsg="hello,红色";//三个参数:......
  • RabbitMQ 发送消息到交换机
    发送消息到交换机的代码:@GetMapping("/mq02")//发送消息给交换机publicvoidmq02(){StringexchangeName="hmall.fanout";Stringmsg="hello,每个人";//三个参数:交换机名称、RoutingKey(暂时为空)、要发送的消息rabbitTemplate.convertAndSend(exchangeName,......
  • RabbitMQ Fanout交换机
     容易搞混的点:1.假如publisher给Fanout交换机发送了一条消息,那么Fanout交换机会给每一个绑定到它身上的队列都发送这条消息,也就是说有多少个队列跟它绑定了,这条消息就有几份,每个队列都收到一份。2.假如一个队列绑定了多个消费者,那么该队列在给消费者投递消息时就是轮询,一......
  • rabbitmq的推(push)拉(pull)模式介绍及代码实现
    在rabbitmq中有两种消息处理的模式,一种是推模式/订阅模式/投递模式(也叫push模式),消费者调用channel.basicConsume方法订阅队列后,由RabbitMQ主动将消息推送给订阅队列的消费者;另一种是拉模式/检索模式(也叫pull模式),需要消费者调用channel.basicGet方法,主动从指定队列中拉取消息。推......
  • RabbitMQ work模型
    默认情况下,MQ队列如果绑定了多个消费者,那么队列在投递消息时就是轮询,一人投递一个(并且一条消息只能投递给监听该队列的某一个消费者)在一个MQ队列上绑定多个消费者的目的是加快队列中消息的处理效率,防止队列中消息的堆积问题。 注:要在消费者的application.yml文件中加上这个......
  • RabbitMQ 接收队列的消息
     代码示例:注:要把这个类加上Component注解packagecom.itheima.amqp_listener;importorg.springframework.amqp.rabbit.annotation.RabbitListener;importorg.springframework.stereotype.Component;@ComponentpublicclassMQListener{@RabbitListener(queues="simpl......
  • RabbitMQ 发送消息到队列(交换机不参与的那种)
    1.导包<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency>2.在application.yml文件里编写配置信息spring:rabbitmq:host:192.168.88.130port:5672......
  • 【Spring】SpringBoot+RabbitMQ(direct/fanout/topic)の構築方法
     ■POM.xmlの中で、下記の内容を追加<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency><dependency>......
  • 使用RabbitMQ时使用MemoryPack序列化和反序列化对象
    [MemoryPackable]publicpartialclassUserEto{publicStringName{get;set;}} 发送端publicclassEventBus:IEventBus{publicvoidPublish(stringexchangeName,objecteventData){usingvarconnection=RabbitMQ......
  • RabbitMQ消息队列
    一.什么是消息队列1.简介在介绍消息队列之前,应该先了解什么是AMQP(AdvancedMessageQueuingProtocol,高级消息队列协议,点击查看)消息(Message)是指在应用间传送的数据,消息可以非常简单,比如只包含文本字符串,也可以更复杂,可能包含嵌入对象;而消息队列(MessageQueue)是一种应用间的......