@Configuration
public class FanoutConfig {
//交换机
@Bean
public FanoutExchange fanoutExchange(){
return new FanoutExchange("fanout");
}
//队列1
@Bean
public Queue queue1(){
return new Queue("fanout.queue1");
}
//队列2
@Bean
public Queue queue2(){
return new Queue("fanout.queue2");
}
//绑定队列1到交换机上
@Bean
public Binding binding1(FanoutExchange fanoutExchange,Queue queue1){
return BindingBuilder.bind(queue1)
.to(fanoutExchange);
}
//绑定队列2到交换机上
@Bean
public Binding binding2(FanoutExchange fanoutExchange,Queue queue2){
return BindingBuilder.bind(queue2)
.to(fanoutExchange);
}
}
运行项目查看是否注册绑定成功
2.publisher发布消息@RunWith(SpringRunner.class)
@SpringBootTest
@EnableRabbit
public class PublisherTest {
@Resource
private RabbitTemplate rabbitTemplate;
@Test
public void fanoutTest(){
//交换机名字
String exchangeName="fanout";
//消息
String mess="hello world!";
rabbitTemplate.convertAndSend(exchangeName,"",mess);
}
}
3.订阅Consumer监听器订阅
listener编写监听
@Component
public class MqListener {
@RabbitListener(queues = "xzh")
public void listenXzhQueue(String mess){
System.out.println("mq消费消息:"+mess);
}
@RabbitListener(queues = "fanout.queue1")
public void listenFanoutQueue1(String mess){
System.out.println("fanout1:"+mess);
}
@RabbitListener(queues = "fanout.queue2")
public void listenFanoutQueue2(String mess){
System.out.println("fanout2:"+mess);
}
}
结果
4.总结广播模式会将消息发送给每一个绑定好指定广播交换机的消息队列
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)