Work模式

一个生产者、多个消费者
多个消费者,共同监听一个队列

一个消息,只能被一个消费者获取

Send

发送者

package cn.itcast.rabbitmq.work;import cn.itcast.rabbitmq.util.ConnectionUtil;import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;public class Send {private final static String QUEUE_NAME = "test_queue_work";public static void main(String[] argv) throws Exception {// 获取到连接以及mq通道Connection connection = ConnectionUtil.getConnection();Channel channel = connection.createChannel();// 声明队列channel.queueDeclare(QUEUE_NAME, false, false, false, null);for (int i = 0; i < 50; i++) {// 消息内容String message = "" + i;channel.basicPublish("", QUEUE_NAME, null, message.getBytes());System.out.println(" [x] Sent '" + message + "'");Thread.sleep(i * 10);}channel.close();connection.close();}
}

Recv

消费者1

package cn.itcast.rabbitmq.work;import cn.itcast.rabbitmq.util.ConnectionUtil;import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.QueueingConsumer;public class Recv {private final static String QUEUE_NAME = "test_queue_work";public static void main(String[] argv) throws Exception {// 获取到连接以及mq通道Connection connection = ConnectionUtil.getConnection();Channel channel = connection.createChannel();// 声明队列channel.queueDeclare(QUEUE_NAME, false, false, false, null);// 同一时刻服务器只会发一条消息给消费者//channel.basicQos(1);// 定义队列的消费者QueueingConsumer consumer = new QueueingConsumer(channel);// 监听队列,手动返回完成channel.basicConsume(QUEUE_NAME, false, consumer);// 获取消息while (true) {QueueingConsumer.Delivery delivery = consumer.nextDelivery();String message = new String(delivery.getBody());System.out.println(" [x] Received '" + message + "'");//休眠Thread.sleep(10);// 返回确认状态channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);}}
}

Recv2

消费者2

package cn.itcast.rabbitmq.work;import cn.itcast.rabbitmq.util.ConnectionUtil;import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.QueueingConsumer;public class Recv2 {private final static String QUEUE_NAME = "test_queue_work";public static void main(String[] argv) throws Exception {// 获取到连接以及mq通道Connection connection = ConnectionUtil.getConnection();Channel channel = connection.createChannel();// 声明队列channel.queueDeclare(QUEUE_NAME, false, false, false, null);// 同一时刻服务器只会发一条消息给消费者//channel.basicQos(1);// 定义队列的消费者QueueingConsumer consumer = new QueueingConsumer(channel);// 监听队列,手动返回完成状态channel.basicConsume(QUEUE_NAME, false, consumer);// 获取消息while (true) {QueueingConsumer.Delivery delivery = consumer.nextDelivery();String message = new String(delivery.getBody());System.out.println(" [x] Received '" + message + "'");// 休眠1秒Thread.sleep(1000);channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);}}
}

测试

查看队列

查看消费者

消费者1
接收消息,休眠10毫秒
消费者2
接收消息,休眠1000毫秒

测试结果

消费者1
获取了25条

消费者2
获取了25条

消费者1,消费者2
获得了相同数量的消息

RabbitMQ的Work模式相关推荐

  1. RabbitMQ六种队列模式-简单队列模式

    前言 RabbitMQ六种队列模式-简单队列 [本文] RabbitMQ六种队列模式-工作队列 RabbitMQ六种队列模式-发布订阅 RabbitMQ六种队列模式-路由模式 RabbitMQ六种队列 ...

  2. RabbitMQ六种队列模式-工作队列模式

    前言 RabbitMQ六种队列模式-简单队列 RabbitMQ六种队列模式-工作队列 [本文] RabbitMQ六种队列模式-发布订阅 RabbitMQ六种队列模式-路由模式 RabbitMQ六种队列 ...

  3. RabbitMQ六种队列模式-发布订阅模式

    前言 RabbitMQ六种队列模式-简单队列 RabbitMQ六种队列模式-工作队列 RabbitMQ六种队列模式-发布订阅 [本文] RabbitMQ六种队列模式-路由模式 RabbitMQ六种队列 ...

  4. RabbitMQ六种队列模式-主题模式

    前言 RabbitMQ六种队列模式-简单队列 RabbitMQ六种队列模式-工作队列 RabbitMQ六种队列模式-发布订阅 RabbitMQ六种队列模式-路由模式 RabbitMQ六种队列模式-主题 ...

  5. 【转】RabbitMQ六种队列模式-5.主题模式

    前言 RabbitMQ六种队列模式-简单队列 RabbitMQ六种队列模式-工作队列 RabbitMQ六种队列模式-发布订阅 RabbitMQ六种队列模式-路由模式 RabbitMQ六种队列模式-主题 ...

  6. 【转】RabbitMQ六种队列模式-4.路由模式

    前言 RabbitMQ六种队列模式-简单队列 RabbitMQ六种队列模式-工作队列 RabbitMQ六种队列模式-发布订阅 RabbitMQ六种队列模式-路由模式 [本文] RabbitMQ六种队列 ...

  7. 【转】RabbitMQ六种队列模式-3.发布订阅模式

    前言 RabbitMQ六种队列模式-简单队列 RabbitMQ六种队列模式-工作队列 RabbitMQ六种队列模式-发布订阅 [本文] RabbitMQ六种队列模式-路由模式 RabbitMQ六种队列 ...

  8. 【转】RabbitMQ六种队列模式-2.工作队列模式

    前言 RabbitMQ六种队列模式-简单队列 RabbitMQ六种队列模式-工作队列 [本文] RabbitMQ六种队列模式-发布订阅 RabbitMQ六种队列模式-路由模式 RabbitMQ六种队列 ...

  9. 【转】RabbitMQ六种队列模式-1.简单队列模式

    前言 RabbitMQ六种队列模式-简单队列 [本文] RabbitMQ六种队列模式-工作队列 RabbitMQ六种队列模式-发布订阅 RabbitMQ六种队列模式-路由模式 RabbitMQ六种队列 ...

  10. RabbitMQ的工作模式Topics  通配符,test测试代

    RabbitMQ有以下几种工作模式 : 1.Work queues  工作队列 2.Publish/Subscribe 发布订阅 3.Routing      路由 4.Topics        通 ...

最新文章

  1. (原創) 如何在VC8使用OpenMP? (C/C++) (VC++) (OpenMP)
  2. 李开复:AI行业正在回归商业本质,技术公司要有服务心态落地为王
  3. 调试机械臂一体化控制电路:STM32F103控制器初步调试
  4. nginx配置静态资源html,通过nginx服务器访问静态资源(示例代码)
  5. double,float,BigDecimal类型数值的操作
  6. JavaScript 原型链学习(二)原型的动态性
  7. php dingo和jwt,dingo配合laravel、JWT使用
  8. 半小时让你快速入门linux掌握基础命令
  9. Linux mv命令
  10. Find Any File for Mac(本地文件搜索查找工具)
  11. 计算机应用技术作业答案,计算机应用与技术网上作业题参考答案20121109
  12. 教你几招提高自媒体文章原创度
  13. Spring实战(第4版)pdf
  14. 古体字与简体字对照表_古代汉语必备简化字与繁体字对照表
  15. Vscode——内置浏览器
  16. 易到暂停办理线下提现 称贾跃亭隐瞒巨额债务成影响提现关键因素
  17. A - Round decimals
  18. Wemos D1 Mini / nodeMcu / esp8266 + GUIslice库 驱动ST7789 TFT显示屏
  19. Setup Factory用户只选择硬盘根目录时自动创建MyApp文件夹
  20. x570主板怎么样 x570主板支持的cpu

热门文章

  1. 【2017-07-03】JS连续删除table中的选中的多行数据
  2. openwrt安装编译
  3. 无向图的完美消除序列 判断弦图 ZOJ 1015 Fish net
  4. 你必须知道的28个HTML5特征、窍门和技术
  5. linux系统vsftpd登陆慢卡怎么办
  6. Google App Engine平台下JDOQL查询报异常的问题解决方案
  7. 详解ADO.NET操作数据库合力创享
  8. python字节码大全
  9. buu 萌萌哒的八戒
  10. 数据结构与算法——树与二叉树详细分享