接上文 RabbitMQ-工作队列
1 发布订阅模式
将之前的配置类内容都替换掉
@Bean("fanoutExchange")public Exchange exchange(){//注意这里是fanoutExchangereturn ExchangeBuilder.fanoutExchange("amq.fanout").build();}@Bean("yydsQueue1")public Queue queue(){return QueueBuilder.nonDurable("yyds1").build();}@Bean("binding")public Binding binding(@Qualifier("fanoutExchange") Exchange exchange,@Qualifier("yydsQueue1") Queue queue){return BindingBuilder.bind(queue).to(exchange).with("yyds1").noargs();}@Bean("yydsQueue2")public Queue queue2(){return QueueBuilder.nonDurable("yyds2").build();}@Bean("binding2")public Binding binding2(@Qualifier("fanoutExchange") Exchange exchange,@Qualifier("yydsQueue2") Queue queue){return BindingBuilder.bind(queue).to(exchange).with("yyds2").noargs();}
两个消息队列(yyds1,yyds2)都绑定到fanoutExchange上,这样给fanoutExchange转发消息就会给到yyds1,yyds2两个队列上
重写监听器
@RabbitListener(queues = "yyds1")public void receiver(String data){System.out.println("一号消息队列监听器 "+data);}@RabbitListener(queues = "yyds2")public void receiver2(String data){System.out.println("二号消息队列监听器 "+data);}
启动服务,出现两个队列
进入交换机详情
发送消息
2 路由模式
关闭服务,删除yyds1,yyds2,yyds三个队列
修改配置类
@Configuration
public class RabbitConfiguration {@Bean("directExchange")public Exchange exchange(){return ExchangeBuilder.directExchange("amq.direct").build();}@Bean("yydsQueue")public Queue queue(){return QueueBuilder.nonDurable("yyds").build();}@Bean("binding") //使用yyds1绑定public Binding binding(@Qualifier("directExchange") Exchange exchange,@Qualifier("yydsQueue") Queue queue){return BindingBuilder.bind(queue).to(exchange).with("yyds1").noargs();}@Bean("binding2") //使用yyds2绑定public Binding binding2(@Qualifier("directExchange") Exchange exchange,@Qualifier("yydsQueue") Queue queue){return BindingBuilder.bind(queue).to(exchange).with("yyds2").noargs();}
}
监听器修改
启动服务,查看直连交换机amq.direct
发送消息