tags:

views:

1239

answers:

3

Hello, 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.

Thanks in advance

+1  A: 

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).

Adam Robinson
Remember to call EndRecieve in the RecieveCompleted event before you call BeginRecieve again if you use this approach. Had some nasty behaviour before I figured this out.
Torbjørn
@Torb: Absolutely; thanks! I've corrected the answer.
Adam Robinson
+1  A: 

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...
        }
JustinD
Why not remove the timeout and the sleep? Just call 'receive' and wait. When a message arrives then you will receive it from the queue and continue.
Kirk Broadhurst
+2  A: 

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.

Kirk Broadhurst