RabbitMQ使用场景练习:发布/订阅(三)

  • 发布/订阅
     即实现单点发送消息,多点接收。使用fanout转发器,广播到所有它知道的队列上 

  • 注意要点

fanout转发器中不需要routingKey,指定也无效  

创建fanout转发器
Java代码   收藏代码
  1. channel.exchangeDeclare("fanout_logs""fanout");  
  2. channel.basicPublish("fanout_logs""" , null, SerializationUtils.serialize(object));  

临时队列的创建
Java代码   收藏代码
  1. //创建一个临时队列,返回队列名称  
  2. String queueName=channel.queueDeclare().getQueue();  

转发器与队列的绑定
Java代码   收藏代码
  1. //绑定临时队列和转发器logs  
  2. hannel.queueBind(queueName, "fanout_logs""");  

  • 消息发送类

Java代码   收藏代码
  1. package com.demo.mq.rabbitmq.example03;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.Serializable;  
  5.   
  6. import org.apache.commons.lang3.SerializationUtils;  
  7.   
  8. import com.demo.mq.rabbitmq.MqManager;  
  9. import com.rabbitmq.client.Channel;  
  10. import com.rabbitmq.client.Connection;  
  11.   
  12. /** 
  13.  * 发送消息类 
  14.  * @author sheungxin 
  15.  * 
  16.  */  
  17. public class Send{  
  18.   
  19.     /** 
  20.      * 发送消息,fanout转发器,广播到所有在此转发器上的队列 
  21.      * @param object 消息主体 
  22.      * @throws IOException 
  23.      */  
  24.     public static void sendAToB(Serializable object) throws Exception{  
  25.         Connection conn=MqManager.newConnection();  
  26.         Channel channel=conn.createChannel();  
  27.         channel.exchangeDeclare("fanout_logs""fanout");  
  28.         channel.basicPublish("fanout_logs""" , null, SerializationUtils.serialize(object));  
  29.         System.out.println("A Send :'"+object+"'");  
  30.         channel.close();  
  31.         conn.close();  
  32.     }  
  33.       
  34.     public static void main(String[] args) throws Exception {  
  35.         for(int i=0;i<4;i++){  
  36.             sendAToB("Hello World "+i+"!");  
  37.         }  
  38.     }  
  39. }  

  • 消息接收类

Java代码   收藏代码
  1. package com.demo.mq.rabbitmq.example03;  
  2.   
  3. import java.io.IOException;  
  4. import org.apache.commons.lang3.SerializationUtils;  
  5. import com.demo.mq.rabbitmq.MqManager;  
  6. import com.rabbitmq.client.AMQP;  
  7. import com.rabbitmq.client.Channel;  
  8. import com.rabbitmq.client.Connection;  
  9. import com.rabbitmq.client.Consumer;  
  10. import com.rabbitmq.client.DefaultConsumer;  
  11. import com.rabbitmq.client.Envelope;  
  12.   
  13. /** 
  14.  * 接收消息类 
  15.  * @author sheungxin 
  16.  * 
  17.  */  
  18. public class Recv {  
  19.       
  20.     /** 
  21.      * 用于接收消息,创建一个临时队列,绑定在转发器fanout上 
  22.      * @throws Exception 
  23.      */  
  24.     public static void recvAToB() throws Exception{  
  25.         Connection conn=MqManager.newConnection();  
  26.         Channel channel=conn.createChannel();  
  27.         channel.exchangeDeclare("fanout_logs""fanout");  
  28.         //创建一个临时队列  
  29.         String queueName=channel.queueDeclare().getQueue();  
  30.         //绑定临时队列和转发器logs  
  31.         channel.queueBind(queueName, "fanout_logs""");  
  32.         Consumer consumer=new DefaultConsumer(channel){  
  33.             @Override  
  34.             public void handleDelivery(String consumerTag,Envelope envelope,AMQP.BasicProperties properties,byte[] body) throws IOException{  
  35.                 String mes=SerializationUtils.deserialize(body);  
  36.                 System.out.println("B Received :'"+mes+"'...");  
  37.             }  
  38.         };  
  39.         //关闭自动应答机制,默认开启;这时候需要手动进行应该  
  40.         channel.basicConsume(queueName, true, consumer);  
  41.     }  
  42.       
  43.     public static void main(String[] args) throws Exception {  
  44.         recvAToB();  
  45.     }  
  46.   
  47. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值