我们有一个Java WebService,它使用MSMQ发送消息(带有一组记录的XML文件).
我需要使用VB.net在.net中构建一个小应用程序,它应该选择这些消息并读取它们并插入到SQL数据库中.
你们有什么建议吗?我们如何实时读取MSMQ消息.
任何资源或链接都会有很大帮助.
在System.Messaging命名空间中,.NET中提供了完整的MSMQ托管实现.您可以调用BeginReceive消息队列,然后异步等待消息到达.完成后,您可以调用EndReceive,处理消息并BeginReceive再次调用以等待下一个消息(或处理队列中的下一个消息).
小智 5
这里有一些示例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...
}
Run Code Online (Sandbox Code Playgroud)