Work Queues(点对多)
Work Queues(点对多)
多个消费者在同一个消息队列中获取消息的情况。在有些应用当中,消费端接收到消息任务需要长时间的处理,如果等上一个消息处理完成以后再取下一个数据进行处理的话,势必会有一些延迟。在消息队列中的数据也会不断增多,延迟将越来越大。当然对于一个消费进程来说,在某些情况下可以起多个线程来处理,而在这里将介绍另一种处理方式,多个消费进程的情况。而RabbitMQ在这方面进行了很好的处理和封装,使客户程序可以很方便的使用。
默认的,RabbitMQ会顺序的把消息发送到下一个Consumer,这种发送消息的方式称为循环发送(round-robin)
package com.rabbitmq.www.work_queues; import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties; public class NewTask { private static final String TASK_QUEUE_NAME = "task_queue";
private final static String HOST_ADDR = "172.18.112.102"; public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory();
factory.setHost(HOST_ADDR);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel(); //durable 设置true,queue持久化,server重启,此queue不丢失
channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);
for(int i=0;i<=10;i++){
String message = "hello world";
message=message+i;
//BasicProperties设置MessageProperties.PERSISTENT_TEXT_PLAIN,信息持久化
channel.basicPublish("", TASK_QUEUE_NAME,MessageProperties.PERSISTENT_TEXT_PLAIN,message.getBytes("UTF-8"));
System.out.println(" [x] Sent '" + message);
} channel.close();
connection.close();
} }
package com.rabbitmq.www.work_queues; import com.rabbitmq.client.*; import java.io.IOException; public class Worker { private static final String TASK_QUEUE_NAME = "task_queue";
private final static String HOST_ADDR = "172.18.112.102"; public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory();
factory.setHost(HOST_ADDR);
final Connection connection = factory.newConnection();
final Channel channel = connection.createChannel();
//durable 设置true,queue持久化,server重启,此queue不丢失
channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
//一次只接受一条信息
channel.basicQos(1); final Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
String message = new String(body, "UTF-8"); System.out.println(" [x] Received '" + message + "'");
try {
doWork(message);
} finally {
System.out.println(" [x] Done");
//向服务器发送应答
channel.basicAck(envelope.getDeliveryTag(), false);
}
}
};
//autoAck 设置false,消费端挂掉,信息不会丢失,server会re-queue
channel.basicConsume(TASK_QUEUE_NAME, false, consumer);
} private static void doWork(String task) {
for (char ch : task.toCharArray()) {
if (ch == '.') {
try {
Thread.sleep(1000);
} catch (InterruptedException _ignored) {
Thread.currentThread().interrupt();
}
}
}
}
}
Work Queues(点对多)的更多相关文章
- [LeetCode] Implement Stack using Queues 用队列来实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- Ring buffers and queues
Ring buffers and queues The data structure is extremely simple: a bounded FIFO. One step up from pla ...
- RabbitMQ官方中文入门教程(PHP版) 第二部分:工作队列(Work queues)
工作队列 在第一篇教程中,我们已经写了一个从已知队列中发送和获取消息的程序.在这篇教程中,我们将创建一个工作队列(Work Queue),它会发送一些耗时的任务给多个工作者(Works ). 工作队列 ...
- Java for LeetCode 225 Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- RabbitMQ(二) -- Work Queues
RabbitMQ(一) -- Work Queues RabbitMQ使用Work Queues的主要目的是为了避免资源使用密集的任务,它不同于定时任务处理的方式,而是把任务封装为消息添加到队列中.而 ...
- Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- the OS maintains a number of queues
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION To do its job, the OS ...
- (leetcode)Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- iOS 并发编程之 Operation Queues
现如今移动设备也早已经进入了多核心 CPU 时代,并且随着时间的推移,CPU 的核心数只会增加不会减少.而作为软件开发者,我们需要做的就是尽可能地提高应用的并发性,来充分利用这些多核心 CPU 的性能 ...
- (easy)LeetCode 225.Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
随机推荐
- 运行mlflow命令报错 The 'nose' distribution was not found and is required by nose-exclude
安装好mlflow之后命令行运行: mlflow 得到报错: 解决: sudo pip3 install nose
- Python开发【笔记】:git&github 快速入门
github入门 简介: 很多人都知道,Linus在1991年创建了开源的Linux,从此,Linux系统不断发展,已经成为最大的服务器系统软件了. Linus虽然创建了Linux,但Linux的壮大 ...
- 【手机自动化测试】monkey测试
1 概述 Monkey测试是Android自动化测试的一种手段.Monkey测试本身非常简单,就是模拟用户的按键输入,触摸屏输入,手势输入等,看设备多长时间会出异常. 当Mon ...
- android 本地通知
NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); Notificat ...
- Openstack(三)Haproxy+Keepalived双机
3.1部署keepalived 3.1.1下载keepalived源码包,并解压 # wget http://www.keepalived.org/software/keepalived-1.4.2. ...
- TeamViewer远程唤醒主机实战教程(多图)
前言:首先感谢大家来到这里.这篇文章其实算是一个教程,文章中涉及到了TeamViewer,Mac OS X,TP-Link家用路由器,以及花生壳DDNS,对于新手而言内容可能稍微有些多,但我相信按照我 ...
- [转]AJAX 跨源 HTTP 请求
转自OSChina, 原文: http://www.oschina.net/translate/ajax-cross-origin-http-request 背景 跨源HTTP请求(也称跨域AJAX请 ...
- C++中定义NULL的头文件
NULL不是C语言基本类型,其定义在stddef.h文件中,作为最基本的语言依赖宏存在.但是随着C/C++的发展,很多文件只要涉及了系统或者标准操作都会将NULL作为标准宏声明或者包含.所以几乎包含任 ...
- forEach方法的实现
var arr = [1, 23, 1, 1, 1, 3, 23, 5, 6, 7, 9, 9, 8, 5]; Array.prototype.forEach = Array.prototype.fo ...
- Vector、List、LinkedList
Vector 遍历 Vector<String> vestor =new Vector<String>(); vestor.add("qq"); vesto ...