您可能会感兴趣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<object> _commonQueue;
// process an individual queue
void ProcessQueue(int id, BlockingCollection<object> 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<BlockingCollection<object>>();
_commonQueue = new BlockingCollection<object>();
// start individual queue processors
var tasks = Enumerable.Range(0, 4).Select((i) =>
{
var queue = new BlockingCollection<object>();
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<BlockingCollection<object>> 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可以帮助将它们聚合为单个流。
可能是一个副本:[如何实现rabbitMQ的单消费者多队列模型](http://stackoverflow.com/q/11357512/1768303) – Noseratio
@Noseratio - no我不是问单消费者多队列。将有多个队列与多个队列,但实施应完成单个窗口服务。所以,而不是为每个队列消费者编写多个Windows服务,我想编写单个的Windows服务,它将监听多个队列并处理消息。 – Mahesh
我同意,这似乎并不重复。 – theMayer