[英]MSMQ in .net as a Service
我們有一個Java WebService,它使用MSMQ發送消息(帶有一組記錄的XML文件)。
我需要使用VB.net在.net中構建一個小應用程序,它應該選擇這些消息並讀取它們並插入到SQL數據庫中。
你們有什么建議嗎? 我們如何實時讀取MSMQ消息。
任何資源或鏈接都會有很大幫助。
在System.Messaging
命名空間中,.NET中提供了完整的MSMQ托管實現。 您可以在消息隊列上調用BeginReceive
,然后異步等待消息到達。 完成后,您可以調用EndReceive,
處理消息並再次調用BeginReceive
以等待下一個(或處理隊列中的下一個)。
在.NET中處理MSMQ消息的最佳方法是使用WCF。 Justin Wilcox 在這里有一個很棒的教程。
但我強烈建議你嘗試MSMQ + WCF。 這是非常好的,你會了解更多關於WCF,這是偉大的東西。
更簡單的方法是做一些像JustinD建議的事情。 System.Messaging命名空間非常易於使用。 我唯一不同的是調用Receive方法而不指定超時。 這會導致線程等待,直到隊列中出現一條消息,此時將收到該消息。
這里有一些示例C#.NET代碼,可以幫助您開始從隊列中讀取...
using System.Messaging;
using System.IO;
MessageQueue l_queue = new MessageQueue(this.MessageQueuePath);
l_queue.Formatter = new XmlMessageFormatter(new Type[] { typeof(System.String) });
if (!l_queue.CanRead)
{
e.Result = MessageQueueError.InsufficientPermissions;
return;
}
while (true)
{
// sleep 2 seconds between checks to keep this from overloading CPU like a madman
System.Threading.Thread.Sleep(2000);
Message l_msg = null;
string l_msgID = String.Empty;
// try and receive the message - a IOTimeout exception just means that there aren't any messages - move on
try { l_msg = l_queue.Receive(TimeSpan.FromSeconds(5)); }
catch (MessageQueueException ex)
{
if (ex.MessageQueueErrorCode != MessageQueueErrorCode.IOTimeout)
// log error
else
continue;
}
catch (Exception ex) { // log error
}
if (l_msg == null)
{
//log error
continue;
}
// retrieve and log the message ID
try { l_msgID = l_msg.Id; }
catch (Exception ex) { // log error
}
// do whatever with the message...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.