

配置类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| @Configuration public class RabbitMqConfig3 {
@Bean public Queue queueOne(){
return new Queue("queueOne"); }
@Bean public Queue queueTwo(){
return new Queue("queueTwo"); }
@Bean public DirectExchange directExchange(){
return new DirectExchange("directExchange"); }
@Bean public Binding queueOneBinding(){
return BindingBuilder.bind(queueOne()).to(directExchange()).with("one"); } @Bean public Binding queueTwoBinding(){
return BindingBuilder.bind(queueTwo()).to(directExchange()).with("two"); }
@Bean public Binding queueThreeBinding(){
return BindingBuilder.bind(queueTwo()).to(directExchange()).with("three"); } }
|
发送消息 - 生产者
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| @Service public class RabbitMqService3 {
@Autowired RabbitTemplate rabbitTemplate;
public void send(String routingKey){
rabbitTemplate.convertAndSend("directExchange",routingKey,"value:" + routingKey); } }
|
接收消息 - 消费者
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| @Component public class RabbitMqComponent3 { @RabbitListener(queues = "queueOne") public void listerQueueOne(String message){ System.out.print("queueOne" + message); }
@RabbitListener(queues = "queueTwo") public void listerQueueTwo(String message){ System.out.print("queueTwo" + message); } }
|



Author:
John Doe
Permalink:
http://yoursite.com/2019/08/10/消息队列/RabbitMQ/RabbitMQ整合/Router路由模式/
License:
Copyright (c) 2019 CC-BY-NC-4.0 LICENSE
Slogan:
Do you believe in DESTINY?