消息队列的Windows服务 [英] Message Queue Windows Service
问题描述
我希望写一个Windows服务在.net 2.0的等待并处理消息队列(MSMQ)。
I wish to write a windows service in .Net 2.0 that listens to and processes a Message Queue (MSMQ).
而不是推倒重来,可以有人张贴做到这一点的最好方法的例子吗?它仅具有处理事情之一的时间,从不平行(例如螺纹)。
Rather than reinvent the wheel, can someone post an example of the best way to do it? It only has to process things one at a time, never in parallel (eg. Threads).
基本上我想这对队列的查询,如果有什么事,在那里,处理它,把它关闭队列,并重复。我想这样做,在一个系统中高效的方式为好。
Essentially I want it to poll the queue, if there's anything there, process it, take it off the queue and repeat. I want to do this in a system-efficient way as well.
感谢您的任何建议!
推荐答案
有一些不同的方法可以做到以上。我建议建立在消息队列中的事件,以便通知您,当消息可用,而不是投票吧。
There are a few different ways you can do the above. I would recommend setting up an event on the message queue so that you are notified when a message is available rather than polling for it.
简单的使用消息队列的例子是 HTTP://www.$c $ cproject.com / KB / CS / mgpmyqueue.aspx 和MSDN文档附加事件等可以在这里找到 的http://msdn.microsoft.com/en-us/library/system.messaging.messagequeue_events.aspx
Simple example of using message queues is http://www.codeproject.com/KB/cs/mgpmyqueue.aspx and the MSDN documentation for attaching events etc can be found at http://msdn.microsoft.com/en-us/library/system.messaging.messagequeue_events.aspx
从这里 微软例如:
Microsoft example from here:
....
// Create an instance of MessageQueue. Set its formatter.
MessageQueue myQueue = new MessageQueue(".\\myQueue");
myQueue.Formatter = new XmlMessageFormatter(new Type[]
{typeof(String)});
// Add an event handler for the ReceiveCompleted event.
myQueue.ReceiveCompleted += new
ReceiveCompletedEventHandler(MyReceiveCompleted);
// Begin the asynchronous receive operation.
myQueue.BeginReceive();
....
private static void MyReceiveCompleted(Object source,
ReceiveCompletedEventArgs asyncResult)
{
// Connect to the queue.
MessageQueue mq = (MessageQueue)source;
// End the asynchronous Receive operation.
Message m = mq.EndReceive(asyncResult.AsyncResult);
// Display message information on the screen.
Console.WriteLine("Message: " + (string)m.Body);
// Restart the asynchronous Receive operation.
mq.BeginReceive();
return;
}
这篇关于消息队列的Windows服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!