RabbitMq第三种模型--fanout
生活随笔
收集整理的這篇文章主要介紹了
RabbitMq第三种模型--fanout
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
fanout也稱為扇出模型,消息以廣播的方式傳播
生產者
消費者1
package com.zuoan.fanout;import com.rabbitmq.client.*; import com.zuoan.utils.RabbitMQUtils;import java.io.IOException;/*** @Description: TODO* @Author: 黃石軍* @CreateTime: 2022/4/10 13:55* @Company:*/ public class Consumer1 {public static void main(String[] args) throws IOException {Connection connection = RabbitMQUtils.getConnection("192.168.188.100");//創建通道Channel channel = connection.createChannel();//綁定交換機channel.exchangeDeclare("logs","fanout");//創建臨時隊列String queue = channel.queueDeclare().getQueue();//綁定交換機和隊列channel.queueBind(queue,"logs","");//消費消息channel.basicConsume(queue,true,new DefaultConsumer(channel){@Overridepublic void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {System.out.println("消費者1"+new String(body));}});} }消費者2
package com.zuoan.fanout;import com.rabbitmq.client.*; import com.zuoan.utils.RabbitMQUtils;import java.io.IOException;/*** @Description: TODO* @Author: 黃石軍* @CreateTime: 2022/4/10 14:22* @Company:*/ public class Consumer2 {public static void main(String[] args) throws IOException {Connection connection = RabbitMQUtils.getConnection("192.168.188.100");//創建通道Channel channel = connection.createChannel();//綁定交換機channel.exchangeDeclare("logs","fanout");//創建臨時隊列String queue = channel.queueDeclare().getQueue();//綁定交換機和隊列channel.queueBind(queue,"logs","");//消費消息channel.basicConsume(queue,true,new DefaultConsumer(channel){@Overridepublic void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {System.out.println("消費者2"+new String(body));}});} }其結果就是每個消費者都能收到廣播的消息
工具類在我博客分類MQ中第一章里
https://blog.csdn.net/weixin_45886609/article/details/124064801
總結
以上是生活随笔為你收集整理的RabbitMq第三种模型--fanout的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2022设计稿01
- 下一篇: 一个简单好看的备忘录