消息队列02SpringBoot搭建Rabbitmq与Fanout订阅模式使用
代码
消费者
@Componentpublic class FanoutReceiver {@RabbitListener(bindings = @QueueBinding(//绑定队列和交换机
value = @Queue(value = "Fanout-A"),//创建queuqe
exchange = @Exchange(value = "FanoutExchange",type = "fanout"//创建交换机
)
))
@RabbitHandler
public void fanoutReceiver01(Message message, Channel channel) throws Exception{
System.out.println("fanoutReceiver01:"+message.toString() );
System.out.println("fanoutReceiver01:"+message.getMessageProperties().getAppId() );
System.out.println("fanoutReceiver01:"+message.getMessageProperties().getMessageId() );
System.out.println("fanoutReceiver01:"+message.getMessageProperties().getReceivedExchange() );
System.out.println("fanoutReceiver01:"+message.getMessageProperties().getReceivedRoutingKey() );
System.out.println("fanoutReceiver01:"+message.getMessageProperties().getDeliveryTag() );
System.out.println("fanoutReceiver01:"+message.getMessageProperties().getHeaders() );
//应答消息队列
channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
}
@RabbitListener(bindings = @QueueBinding(//绑定队列和交换机
value = @Queue(value = "Fanout-B"),//创建queuqe
exchange = @Exchange(value = "FanoutExchange",type = "fanout"//创建交换机
)
))
@RabbitHandler
public void fanoutReceiver02(Message message, Channel channel) throws Exception{
System.out.println("fanoutReceiver02:"+message.toString() );
System.out.println("fanoutReceiver02:"+message.getMessageProperties().getAppId() );
System.out.println("fanoutReceiver02:"+message.getMessageProperties().getMessageId() );
System.out.println("fanoutReceiver02:"+message.getMessageProperties().getReceivedExchange() );
System.out.println("fanoutReceiver02:"+message.getMessageProperties().getReceivedRoutingKey() );
System.out.println("fanoutReceiver02:"+message.getMessageProperties().getDeliveryTag() );
System.out.println("fanoutReceiver02:"+message.getMessageProperties().getHeaders() );
//应答消息队列
channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
}
}
启动项目
可以看到交换机界面已经生成一个 叫“FanoutExchange”的Exchange
查看FanoutExchange 可以看到绑定了Fanout-A 和Fanout-B 2个队列
Queue界面可以看到Fanout-A 和Fanout-B 2个队列也已经生成
队列也绑定了
FanoutExchange
生产者
@Componentpublic class FanoutSender {@Autowired
private RabbitTemplate rabbitTemplate;
public void send() throws Exception {
String context = "hi, i am FanoutSender message ";
this.rabbitTemplate.convertAndSend("FanoutExchange", "", context);
}
}
调用send方法
可以看到2个队列都收到消息
以上是 消息队列02SpringBoot搭建Rabbitmq与Fanout订阅模式使用 的全部内容, 来源链接: utcz.com/z/516535.html