.net 中的 MSMQ 即服务

发布于 2024-08-01 19:06:02 字数 194 浏览 3 评论 0原文

我们有一个 Java WebService,它使用 MSMQ 发送消息(带有一组记录的 XML 文件)。

我需要使用 VB.net 在 .net 中构建一个小型应用程序,它应该选择这些消息并读取它们并将其插入 SQL 数据库。

你们有什么建议吗? 我们如何实时读取MSMQ消息。

任何资源或链接都会有很大帮助。

We have a Java WebService which is sending messages(XML file with a set of records) using MSMQ.

I need to build a small application in .net using VB.net which should pick those messages and read them and insert into SQL database.

Do you guys have any suggestions? How can we read MSMQ messages on real time.

Any resources or links will be of great help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浸婚纱 2024-08-08 19:06:02

系统中的 .NET 提供了 MSMQ 的完全托管实现.Messaging 命名空间。 您可以在消息队列上调用 BeginReceive,然后该队列将异步等待消息到达。 一旦完成,您就可以调用 EndReceive, 处理消息并再次调用 BeginReceive 以等待下一条消息(或处理队列中的下一条消息)。

There's a full managed implementation of MSMQ available in .NET in the System.Messaging namespace. You can call BeginReceive on a message queue, which will then asynchronously wait for a message to arrive. Once it does you can then call EndReceive, process the message and call BeginReceive again to wait for the next (or process the next in the queue).

淡淡離愁欲言轉身 2024-08-08 19:06:02

在 .NET 中处理 MSMQ 消息的最佳方法是使用 WCF。 Justin Wilcox 在此处提供了很棒的教程。

但我强烈建议你尝试MSMQ + WCF。 它非常好,您将了解有关 WCF 的更多信息,这是很棒的东西。

更简单的方法是像 JustinD 建议的那样做。 System.Messaging 命名空间非常易于使用。 我要做的唯一不同是调用 Receive 方法而不指定超时。 这会导致线程等待,直到消息出现在队列中,此时将收到消息。

The best way to process MSMQ messages in .NET is using WCF. Justin Wilcox has a great tutorial here.

But I strongly suggest you try MSMQ + WCF. It is very good and you'll learn more about WCF, which is great stuff.

The simpler way is to do something like JustinD suggests. The System.Messaging namespace is very easy to use. The only different I would make is to call the Receive method without specifying a timeout. This causes the thread to wait until a message appears in the queue, at which time is will be received.

眼泪都笑了 2024-08-08 19:06:02

这里有一些示例 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...
        }

here's a bit of sample C# .NET code that may help get you started with reading from a queue...

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...
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文