企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] # 简介 **模式匹配** * `* `表示⼀个词; * `#`表示零个或多个词 # 创建队列 Topic 是 RabbitMQ 中最灵活的⼀种⽅式,可以根据 routing\_key ⾃由的绑定不同的队列。 ⾸先对 Topic 规则配置,这⾥使⽤两个队列来测试: ~~~ import org.springframework.amqp.core.Binding; import org.springframework.amqp.core.BindingBuilder; import org.springframework.amqp.core.Queue; import org.springframework.amqp.core.TopicExchange; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class RabbitConfig { final static String message = "topic.message"; final static String messages = "topic.messages"; /** * 定义队列 */ @Bean public Queue queueMessage() { return new Queue(RabbitConfig.message); } @Bean public Queue queueMessages() { return new Queue(RabbitConfig.messages); } /** * 交换机 */ @Bean public TopicExchange exchange() { return new TopicExchange("topicExchange"); } /** * 将队列和交换机绑定 */ @Bean public Binding bindingExchangeMessage(Queue queueMessage, TopicExchange exchange) { //queueMessage 只匹配“topic.message”队列 return BindingBuilder.bind(queueMessage).to(exchange).with("topic.message"); } @Bean public Binding bindingExchangeMessages(Queue queueMessages, TopicExchange exchange) { //设计 queueMessages 同时匹配两个队列 return BindingBuilder.bind(queueMessages).to(exchange).with("topic.#"); } } ~~~ # 发送者 ~~~ import org.springframework.amqp.core.AmqpTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.Date; @Component public class TopicSender { @Autowired private AmqpTemplate rabbitTemplate; public void send1() { String context = "hi, i am message 1"; System.out.println("Sender : " + context); this.rabbitTemplate.convertAndSend("topicExchange", "topic.message", context); } public void send2() { String context = "hi, i am messages 2"; System.out.println("Sender : " + context); this.rabbitTemplate.convertAndSend("topicExchange", "topic.messages", context); } } ~~~ # 接收者 **接收者1** ~~~ import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; @Component @RabbitListener(queues = "topic.message") public class TopicReceiver { @RabbitHandler public void process(String message) { System.out.println("Topic Receiver1 : " + message); } } ~~~ **接收者2** ~~~ import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; @Component @RabbitListener(queues = "topic.messages") public class TopicReceiver2 { @RabbitHandler public void process(String message) { System.out.println("Topic Receiver2 : " + message); } } ~~~ # 测试 发送 send1 会匹配到 `topic.#` 和 topic.message 两个 Receiver 都可以收到消息 发送 send2 只有 `topic.#` 可 以匹配,Receiver2 监听到了消息 ~~~ @Autowired private TopicSender sender; @Test public void topic1() throws Exception { sender.send1(); Thread.sleep(1000L); } @Test public void topic2() throws Exception { sender.send2(); Thread.sleep(1000L); } ~~~