MSMQ in .net as a service

We have a Java WebService that sends messages (an XML file with a set of records) using MSMQ.

I need to create a small application in .net using VB.net, which should select these messages and read them and paste them into the SQL database.

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

Any resources or links will be very helpful.

+4
source share
3 answers

here is some sample C # .NET code that can help you start reading from the 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... } 
+5
source

In the full-featured MSMQ implementation available in .NET, in the System.Messaging namespace. You can call BeginReceive in a message queue, which will then wait for messages asynchronously. After that, you can call EndReceive, process the message and call BeginReceive again to wait for the next one (or process the next one in the queue).

+9
source

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

But I highly recommend you try MSMQ + WCF. This is very good, and you will learn more about WCF, this is great stuff.

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

+6
source

All Articles