//MQ配置:
@Configuration
public class RabbitTopicConfig {
@Bean
public TopicExchange topicExchange() {
return new TopicExchange ("topicExchange");
}
@Bean
public Queue topicTp1() {
return new Queue("topic.tp2.one");
}
@Bean
public Queue topicTp2() {
return new Queue("topic.tp2.two");
}
@Bean
public Binding binding1() {
return BindingBuilder.bind(topicTp1()).to(topicExchange()).with("topic.tp2.*"); //* 后面匹配单个key
}
@Bean
public Binding binding2() {
return BindingBuilder.bind(topicTp2()).to(topicExchange()).with("topic.tp2.#"); //# 后面匹配多个key
}
}
//发送者
@GetMapping("/topic")
public String sendTopicMsg(@RequestParam("msg") String msg) {
rabbitTemplate.convertAndSend("topicExchange","topic.tp2.all", msg);
return "success";
}
//接收者
@Component
public class TopicRecerver {
@RabbitListener(queues = "topic.tp2.one")
@RabbitHandler
public void process(String strs) {
System.out.println("topic.tp2.one 接收到:" + strs);
}
@RabbitListener(queues = "topic.tp2.two")
@RabbitHandler
public void process2(String strs) {
System.out.println("topic.tp2.two 接收到:" + strs);
}
}
Rabbit MQ Config配置 -立哥开发
最新推荐文章于 2024-03-13 12:16:56 发布