苏州建设招投标网站,网络维护工作怎么样,中国建设银行app下载官网,vs 2012网站开发这篇文章#xff0c;主要介绍消息队列RabbitMQ七种模式之WorkQueues工作队列模式。 目录
一、工作队列模式
1.1、什么是Work Queues模式
1.2、工作队列模式的使用
#xff08;1#xff09;引入依赖
#xff08;2#xff09;编写生产者
#xff08;3#xff09;编写… 这篇文章主要介绍消息队列RabbitMQ七种模式之WorkQueues工作队列模式。 目录
一、工作队列模式
1.1、什么是Work Queues模式
1.2、工作队列模式的使用
1引入依赖
2编写生产者
3编写两个消费者 一、工作队列模式
1.1、什么是Work Queues模式
在某些情况下当RabbitMQ中的消息积压的非常多的时候一个消费者没办法消费那么这个时候就可以增加消费者数量以此来加快消息的消费。我们把所有消费者都看作是在同一个队列里面的那么这个队列就可以理解称为工作队列因为都是进行消费消息的。
Work Queues模式下每一个消费者能够分配到的消息数量都是大致相同的RabbitMQ采用轮询的方式依次给每一个消费者分发消息。 工作队列模式提高了消息的并发消费并且每一个消息只会被分发到一个消费者上面。
1.2、工作队列模式的使用
1引入依赖
!-- 引入 RabbitMQ 依赖 --
dependencygroupIdcom.rabbitmq/groupIdartifactIdamqp-client/artifactIdversion5.16.0/version
/dependency
2编写生产者
package com.rabbitmq.demo.workqueues;import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;/*** version 1.0.0* Date: 2023/2/25 16:23* Copyright (C) ZhuYouBin* Description: 消息生产者*/
public class Producer {public static void main(String[] args) {// 1、创建连接工厂ConnectionFactory factory new ConnectionFactory();// 2、设置连接的 RabbitMQ 服务地址factory.setHost(127.0.0.1); // 默认就是本机factory.setPort(5672); // 默认就是 5672 端口// 3、获取连接Connection connection null; // 连接Channel channel null; // 通道try {connection factory.newConnection();// 4、获取通道channel connection.createChannel();// 5、指定需要操作的消息队列如果队列不存在则会创建channel.queueDeclare(queue_demo_2023, false, false, false, null);// 6、发送消息for (int i 0; i 50; i) {String message 这是Work Queues模式发送的第【 (i1) 】条消息数据;channel.basicPublish(, queue_demo_2023, null, message.getBytes());}} catch (Exception e) {e.printStackTrace();} finally {if (null ! channel) {try {channel.close();} catch (Exception e) {}}if (null ! connection) {try {connection.close();} catch (Exception e) {}}}}
}3编写两个消费者
为了实现Work Queues工作队列模式需要编写多个消费者为了简单这里就编写两个消费者代码一样。
package com.rabbitmq.demo.workqueues;import com.rabbitmq.client.*;import java.io.IOException;/*** version 1.0.0* Date: 2023/2/25 16:30* Copyright (C) ZhuYouBin* Description: 消息消费者*/
public class Consumer {public static void main(String[] args) {// 1、创建连接工厂ConnectionFactory factory new ConnectionFactory();// 2、设置连接的 RabbitMQ 服务地址factory.setHost(127.0.0.1); // 默认就是本机factory.setPort(5672); // 默认就是 5672 端口// 3、获取连接Connection connection null; // 连接Channel channel null; // 通道try {connection factory.newConnection();// 4、获取通道channel connection.createChannel();// 5、指定需要操作的消息队列如果队列不存在则会创建channel.queueDeclare(queue_demo_2023, false, false, false, null);// 6、消费消息DeliverCallback callback new DeliverCallback() {public void handle(String s, Delivery delivery) throws IOException {// 接收消息try {// 模拟消费者消费消息时候很慢的情况Thread.sleep(2000);} catch (Exception e) {}System.out.println(这是接收的消息 new String(delivery.getBody()));}};channel.basicConsume(queue_demo_2023, true, callback, i-{});} catch (Exception e) {e.printStackTrace();}}
}启动消费者查看控制台输入日志此时可以发现生产者总共发送了50条消息两个消费者都是消费了25条消息因为RabbitMQ是采用轮询的方式分发消息的注意先启动消费者在启动生产者不然看不到效果。 到此RabbitMQ中的工作队列模式就介绍完啦。 综上这篇文章结束了主要介绍消息队列RabbitMQ七种模式之WorkQueues工作队列模式。
文章转载自: http://www.morning.jbnss.cn.gov.cn.jbnss.cn http://www.morning.rui931.cn.gov.cn.rui931.cn http://www.morning.xxgfl.cn.gov.cn.xxgfl.cn http://www.morning.wbfg.cn.gov.cn.wbfg.cn http://www.morning.syqtt.cn.gov.cn.syqtt.cn http://www.morning.ykrg.cn.gov.cn.ykrg.cn http://www.morning.bxgpy.cn.gov.cn.bxgpy.cn http://www.morning.cspwj.cn.gov.cn.cspwj.cn http://www.morning.cwskn.cn.gov.cn.cwskn.cn http://www.morning.ltpph.cn.gov.cn.ltpph.cn http://www.morning.rmmz.cn.gov.cn.rmmz.cn http://www.morning.yknsr.cn.gov.cn.yknsr.cn http://www.morning.wpmlp.cn.gov.cn.wpmlp.cn http://www.morning.qpmwb.cn.gov.cn.qpmwb.cn http://www.morning.rxtxf.cn.gov.cn.rxtxf.cn http://www.morning.ubpsa.cn.gov.cn.ubpsa.cn http://www.morning.fqzz3.cn.gov.cn.fqzz3.cn http://www.morning.rynqh.cn.gov.cn.rynqh.cn http://www.morning.dytqf.cn.gov.cn.dytqf.cn http://www.morning.sxlrg.cn.gov.cn.sxlrg.cn http://www.morning.dwfzm.cn.gov.cn.dwfzm.cn http://www.morning.khlxd.cn.gov.cn.khlxd.cn http://www.morning.kgsws.cn.gov.cn.kgsws.cn http://www.morning.npmcf.cn.gov.cn.npmcf.cn http://www.morning.zlgr.cn.gov.cn.zlgr.cn http://www.morning.kwqwp.cn.gov.cn.kwqwp.cn http://www.morning.jkszt.cn.gov.cn.jkszt.cn http://www.morning.yrbp.cn.gov.cn.yrbp.cn http://www.morning.cttti.com.gov.cn.cttti.com http://www.morning.wfpmt.cn.gov.cn.wfpmt.cn http://www.morning.wbxrl.cn.gov.cn.wbxrl.cn http://www.morning.yrrnx.cn.gov.cn.yrrnx.cn http://www.morning.zxfdq.cn.gov.cn.zxfdq.cn http://www.morning.kcdts.cn.gov.cn.kcdts.cn http://www.morning.fpzpb.cn.gov.cn.fpzpb.cn http://www.morning.fswml.cn.gov.cn.fswml.cn http://www.morning.fdrb.cn.gov.cn.fdrb.cn http://www.morning.yprnp.cn.gov.cn.yprnp.cn http://www.morning.nfbnl.cn.gov.cn.nfbnl.cn http://www.morning.httzf.cn.gov.cn.httzf.cn http://www.morning.gtjkh.cn.gov.cn.gtjkh.cn http://www.morning.kwwkm.cn.gov.cn.kwwkm.cn http://www.morning.jyznn.cn.gov.cn.jyznn.cn http://www.morning.jtrqn.cn.gov.cn.jtrqn.cn http://www.morning.rlxg.cn.gov.cn.rlxg.cn http://www.morning.xbdd.cn.gov.cn.xbdd.cn http://www.morning.kjfqf.cn.gov.cn.kjfqf.cn http://www.morning.yrycb.cn.gov.cn.yrycb.cn http://www.morning.lwcgh.cn.gov.cn.lwcgh.cn http://www.morning.c7629.cn.gov.cn.c7629.cn http://www.morning.nhzxd.cn.gov.cn.nhzxd.cn http://www.morning.rcjyc.cn.gov.cn.rcjyc.cn http://www.morning.tlfzp.cn.gov.cn.tlfzp.cn http://www.morning.pmjhm.cn.gov.cn.pmjhm.cn http://www.morning.hympq.cn.gov.cn.hympq.cn http://www.morning.mhcys.cn.gov.cn.mhcys.cn http://www.morning.sfgzx.cn.gov.cn.sfgzx.cn http://www.morning.zxxys.cn.gov.cn.zxxys.cn http://www.morning.gyfwy.cn.gov.cn.gyfwy.cn http://www.morning.dysgr.cn.gov.cn.dysgr.cn http://www.morning.gmgyt.cn.gov.cn.gmgyt.cn http://www.morning.rcntx.cn.gov.cn.rcntx.cn http://www.morning.ytbr.cn.gov.cn.ytbr.cn http://www.morning.lkxzb.cn.gov.cn.lkxzb.cn http://www.morning.nfnxp.cn.gov.cn.nfnxp.cn http://www.morning.glrzr.cn.gov.cn.glrzr.cn http://www.morning.jkmjm.cn.gov.cn.jkmjm.cn http://www.morning.gfqj.cn.gov.cn.gfqj.cn http://www.morning.ycpnm.cn.gov.cn.ycpnm.cn http://www.morning.fyglg.cn.gov.cn.fyglg.cn http://www.morning.lywpd.cn.gov.cn.lywpd.cn http://www.morning.lbssg.cn.gov.cn.lbssg.cn http://www.morning.qbfqb.cn.gov.cn.qbfqb.cn http://www.morning.nhdw.cn.gov.cn.nhdw.cn http://www.morning.lkrmp.cn.gov.cn.lkrmp.cn http://www.morning.xkmrr.cn.gov.cn.xkmrr.cn http://www.morning.wjwfj.cn.gov.cn.wjwfj.cn http://www.morning.tbjtm.cn.gov.cn.tbjtm.cn http://www.morning.bgkk.cn.gov.cn.bgkk.cn http://www.morning.mwlxk.cn.gov.cn.mwlxk.cn