rabbitmq java 多线程_基于多线程的RabbitMQ消费者

本文介绍了一个基于多线程的RabbitMQ消费者示例,使用BlockingCollection和Task Parallel Library处理来自多个队列的消息。消费者会将消息聚合到一个公共队列中进行序列化处理。此外,还提到了使用Reactive Extensions (Rx) 的可能性来整合事件流。
摘要由CSDN通过智能技术生成

您可能会发现this answer非常有帮助 . 我对RabbitMQ的工作原理有了一个非常基本的了解,但我可能会在每个线程的每个通道上继续使用一个用户,正如那里所建议的那样 .

为此组织线程模型肯定有多个选项 . 实际实现将取决于您需要如何处理来自多个队列的消息:并行,或者通过聚合它们并序列化处理 . 以下代码是一个控制台应用程序,它实现了后一种情况的模拟 . 它使用Task Parallel Library和BlockingCollection类(这对于这种任务非常方便) .

using System;

using System.Collections.Concurrent;

using System.Collections.Generic;

using System.Linq;

using System.Threading;

using System.Threading.Tasks;

namespace Console_21842880

{

class Program

{

BlockingCollection _commonQueue;

// process an individual queue

void ProcessQueue(int id, BlockingCollection queue, CancellationToken token)

{

while (true)

{

// observe cancellation

token.ThrowIfCancellationRequested();

// get a message, this blocks and waits

var message = queue.Take(token);

// process this message

// just place it to the common queue

var wrapperMessage = "queue " + id + ", message: " + message;

_commonQueue.Add(wrapperMessage);

}

}

// process the common aggregated queue

void ProcessCommonQeueue(CancellationToken token)

{

while (true)

{

// observe cancellation

token.ThrowIfCancellationRequested();

// this blocks and waits

// get a message, this blocks and waits

var message = _commonQueue.Take(token);

// process this message

Console.WriteLine(message.ToString());

}

}

// run the whole process

async Task RunAsync(CancellationToken token)

{

var queues = new List>();

_commonQueue = new BlockingCollection();

// start individual queue processors

var tasks = Enumerable.Range(0, 4).Select((i) =>

{

var queue = new BlockingCollection();

queues.Add(queue);

return Task.Factory.StartNew(

() => ProcessQeueue(i, queue, token),

TaskCreationOptions.LongRunning);

}).ToList();

// start the common queue processor

tasks.Add(Task.Factory.StartNew(

() => ProcessCommonQeueue(token),

TaskCreationOptions.LongRunning));

// start the simulators

tasks.AddRange(Enumerable.Range(0, 4).Select((i) =>

SimulateMessagesAsync(queues, token)));

// wait for all started tasks to complete

await Task.WhenAll(tasks);

}

// simulate a message source

async Task SimulateMessagesAsync(List> queues, CancellationToken token)

{

var random = new Random(Environment.TickCount);

while (true)

{

token.ThrowIfCancellationRequested();

await Task.Delay(random.Next(100, 1000));

var queue = queues[random.Next(0, queues.Count)];

var message = Guid.NewGuid().ToString() + " " + DateTime.Now.ToString();

queue.Add(message);

}

}

// entry point

static void Main(string[] args)

{

Console.WriteLine("Ctrl+C to stop...");

var cts = new CancellationTokenSource();

Console.CancelKeyPress += (s, e) =>

{

// cancel upon Ctrl+C

e.Cancel = true;

cts.Cancel();

};

try

{

new Program().RunAsync(cts.Token).Wait();

}

catch (Exception ex)

{

if (ex is AggregateException)

ex = ex.InnerException;

Console.WriteLine(ex.Message);

}

Console.WriteLine("Press Enter to exit");

Console.ReadLine();

}

}

}

另一个想法可能是使用Reactive Extensions (Rx) . 如果你能想到事件中到达的消息,Rx可以帮助将它们聚合成单个流 .

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值