RabbitMq第三种模型--fanout
生活随笔
收集整理的這篇文章主要介紹了
RabbitMq第三种模型--fanout
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
fanout也稱為扇出模型,消息以廣播的方式傳播
生產(chǎn)者
消費(fèi)者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");//創(chuàng)建通道Channel channel = connection.createChannel();//綁定交換機(jī)channel.exchangeDeclare("logs","fanout");//創(chuàng)建臨時(shí)隊(duì)列String queue = channel.queueDeclare().getQueue();//綁定交換機(jī)和隊(duì)列channel.queueBind(queue,"logs","");//消費(fèi)消息channel.basicConsume(queue,true,new DefaultConsumer(channel){@Overridepublic void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {System.out.println("消費(fèi)者1"+new String(body));}});} }消費(fèi)者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");//創(chuàng)建通道Channel channel = connection.createChannel();//綁定交換機(jī)channel.exchangeDeclare("logs","fanout");//創(chuàng)建臨時(shí)隊(duì)列String queue = channel.queueDeclare().getQueue();//綁定交換機(jī)和隊(duì)列channel.queueBind(queue,"logs","");//消費(fèi)消息channel.basicConsume(queue,true,new DefaultConsumer(channel){@Overridepublic void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {System.out.println("消費(fèi)者2"+new String(body));}});} }其結(jié)果就是每個(gè)消費(fèi)者都能收到廣播的消息
工具類(lèi)在我博客分類(lèi)MQ中第一章里
https://blog.csdn.net/weixin_45886609/article/details/124064801
總結(jié)
以上是生活随笔為你收集整理的RabbitMq第三种模型--fanout的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 2022设计稿01
- 下一篇: 一个简单好看的备忘录