RabbitMQ使用场景练习:入门实例(一)

  • 注意要点

在生产消息和消费消息的时候,我们都习惯先queueDeclare一个队列,但是如果你先后执行下面这两行,会报错。

channel.queueDeclare("test", true, false, false, null);

channel.queueDeclare("test", false, false, false, null);

因为你定义第二个队列的时候,队列名称不一样,但是队列的属性却不一样。

  • 消息发送类

Java代码  收藏代码

  1. package com.demo.mq.rabbitmq.example01;  
  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.demo.mq.rabbitmq.UserBean;  
  10. import com.rabbitmq.client.Channel;  
  11. import com.rabbitmq.client.Connection;  
  12.   
  13. /** 
  14.  * 发送消息类 
  15.  * @author sheungxin 
  16.  * 
  17.  */  
  18. public class Send{  
  19.   
  20.     /** 
  21.      * 单发送、单接收场景,无特别处理,用于接收消息 
  22.      * @param queue 队列名称 
  23.      * @param object 消息主体 
  24.      * @throws IOException 
  25.      */  
  26.     public static void sendAToB(String queue,Serializable object) throws Exception{  
  27.         Connection conn=MqManager.newConnection();  
  28.         Channel channel=conn.createChannel();  
  29.         channel.queueDeclare(queue, falsefalsefalsenull);  
  30.         channel.basicPublish("", queue, null, SerializationUtils.serialize(object));  
  31.         System.out.println("A Send :'"+object+"'");  
  32.         channel.close();  
  33.         conn.close();  
  34.     }  
  35.       
  36.     public static void main(String[] args) throws Exception {  
  37.         String channel="hello";  
  38. //      sendAToB(channel, new String("Hello World!".getBytes(),"UTF-8"));  
  39.         UserBean user=new UserBean();  
  40.         user.setId("0001");  
  41.         user.setName("测试001");  
  42.         sendAToB(channel, user);  
  43.     }  
  44. }  
  • 消息接收类

 

Java代码  收藏代码

  1. package com.demo.mq.rabbitmq.example01;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import org.apache.commons.lang3.SerializationUtils;  
  6.   
  7. import com.demo.mq.rabbitmq.MqManager;  
  8. import com.demo.mq.rabbitmq.UserBean;  
  9. import com.rabbitmq.client.AMQP;  
  10. import com.rabbitmq.client.Channel;  
  11. import com.rabbitmq.client.Connection;  
  12. import com.rabbitmq.client.Consumer;  
  13. import com.rabbitmq.client.DefaultConsumer;  
  14. import com.rabbitmq.client.Envelope;  
  15.   
  16. /** 
  17.  * 接收消息类 
  18.  * @author sheungxin 
  19.  * 
  20.  */  
  21. public class Recv {  
  22.       
  23.     /** 
  24.      * 单发送、单接收场景,无特别处理,用于接收消息 
  25.      * 注意:同时多个接收实体,依次接收消息,同一消息只有一个实体接收 
  26.      * @param queue 
  27.      * @throws Exception 
  28.      */  
  29.     public static void recvAToB(String queue) throws Exception{  
  30.         Connection conn=MqManager.newConnection();  
  31.         Channel channel=conn.createChannel();  
  32.         //此处声明队列为了防止接收者先运行,队列还不存在时创建队列(同一队列只会创建一次)  
  33.         channel.queueDeclare(queue, falsefalsefalsenull);  
  34.         Consumer consumer=new DefaultConsumer(channel){  
  35.             @Override  
  36.             public void handleDelivery(String consumerTag,Envelope envelope,AMQP.BasicProperties properties,byte[] body) throws IOException{  
  37. //              String mes=SerializationUtils.deserialize(body);  
  38.                 UserBean userBean=SerializationUtils.deserialize(body);  
  39.                 System.out.println("B Received :'"+userBean.getId()+","+userBean.getName()+"'");  
  40.             }  
  41.         };  
  42.         channel.basicConsume(queue, true, consumer);  
  43.     }  
  44.       
  45.     public static void main(String[] args) throws Exception {  
  46.         recvAToB("hello");  
  47.     }  
  48.   
  49. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值