日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

RabbitMq第三种模型--fanout

發布時間:2024/1/18 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 RabbitMq第三种模型--fanout 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

fanout也稱為扇出模型,消息以廣播的方式傳播


生產者

package com.zuoan.fanout;import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.zuoan.utils.RabbitMQUtils;import java.io.IOException;/*** @Description: TODO* @Author: 黃石軍* @CreateTime: 2022/4/10 13:33* @Company:*/ public class Provider {public static void main(String[] args) throws IOException {Connection connection = RabbitMQUtils.getConnection("192.168.188.100");//創建通道Channel channel = connection.createChannel();//聲明交換機//參數一:交換機名稱,參數二:交換機類型channel.exchangeDeclare("logs","fanout");//發送消息channel.basicPublish("logs","",null,"fanout type message".getBytes());RabbitMQUtils.closeChannelAndConnection(channel,connection);} }

消費者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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。