Browse DevX
Sign up for e-mail newsletters from DevX


MSMQ for .NET Developers, Part 2 : Page 2

Learn how to use MSMQ to send messages with complex messages types (not just strings), send MSMQ messages over HTTP, and explore several more useful queue and message properties as well as a few invaluable MSMQ tips and pitfalls.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Sending Typed Messages
In Part 1 of this article, I demonstrated how to send and receive simple text messages. But MSMQ actually provides an easy to use mechanism to send objects from one application to another using queuing. Conceptually, it seems nearly effortless for MSMQ to send and receive strings. The truth is, MSMQ treats strings and custom types very similarly; there's little distinction between handling a string and handling a more complex type. MSMQ simply serializes the object into the body and sends the message. When the message is received, MSMQ de-serializes the object back into its native type (using a little coercion, of course). The only differences between sending a string and a custom type are that you need to specify a formatter to use and coerce the object back into the intended type after it's received.

To illustrate using custom types in MSMQ, the code below shows a very simple object used to store article publication information: article name, URL, and publication date. The property implementations have been removed for brevity, because they simply read and write the private member variables.

Public Class DevXTestClass Private m_strArticleName As String Private m_strArticleURL As String Private m_dtePublishDate As DateTime Public Property ArticleName() As String ... End Property Public Property ArticleURL() As String ... End Property Public Property AriclePublishDate() As DateTime ... End Property Public Overrides Function ToString() As String ... End Function End Class

To use the class, you create and populate a DevXTestClass instance and send it to a queue.

Dim myQueue As MessageQueue Dim objDevX As DevXTestClass ' create and populate a DevXTestClass object objDevX = New DevXTestClass objDevX.ArticleName = "MSMQ for .NET Developers" objDevX.ArticleURL = _ "http://www.devx.com/dotnet/Article/27560/0" objDevX.AriclePublishDate = DateTime.Parse("3/16/2005") ' connect to the queue and send the message myQueue = New MessageQueue(comboQueueName.Text) myQueue.Formatter = New XmlMessageFormatter( _ New Type() {GetType(DevXTestClass)}) myQueue.Send(objDevX)

Finally, the code below retrieves a message containing a DevXTestClass object from the queue and exposes the object's properties (via the DevXTestClass's ToString method) to show they have not changed in transit through the queuing system.

Dim myQueue As MessageQueue Dim myMsg As System.Messaging.Message Dim objDevX As DevXTestClass ' connect to the queue and set the message formatter myQueue = New MessageQueue(comboQueueName.Text) myQueue.Formatter = New XmlMessageFormatter( _ New Type() {GetType(DevXTestClass)}) ' receive the message, waiting no longer than 5 seconds myMsg = myQueue.Receive(New TimeSpan(0, 0, 5)) ' convert the message to a DevXTestClass type objDevX = CType(myMsg.Body, DevXTestClass) ' display the received message MessageBox.Show(objDevX.ToString, "Typed Message Received")

As you can see, sending objects is only slightly more complex than sending string messages.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date