Message Queuing

From IronPython Cookbook

(Difference between revisions)
 
(4 intermediate revisions not shown)
Line 1: Line 1:
From [http://weblogs.asp.net/nleghari/archive/2007/03/06/using-msmq-from-ironpython.aspx Nauman Leghari].
From [http://weblogs.asp.net/nleghari/archive/2007/03/06/using-msmq-from-ironpython.aspx Nauman Leghari].
-
Message Queuing is a form of inter-process communication. It allows ''allows applications running at different times to communicate across heterogeneous networks and systems that might be temporarily offline. Applications send, receive, or peek (read without removing) messages from queues''.
+
Message Queuing (MSMQ) is a form of inter-process communication. It allows ''allows applications running at different times to communicate across heterogeneous networks and systems that might be temporarily offline. Applications send, receive, or peek (read without removing) messages from queues''.
Message Queuing is an optional component of Windows XP and Windows 2000/NT. In order to use it, it must be installed. To do this you go to the control panel, select add or remove programs, and then Add/Remove Windows Components.
Message Queuing is an optional component of Windows XP and Windows 2000/NT. In order to use it, it must be installed. To do this you go to the control panel, select add or remove programs, and then Add/Remove Windows Components.
Line 10: Line 10:
This example uses the [http://msdn2.microsoft.com/en-us/library/system.messaging.messagequeue(VS.80).aspx MessageQueue] class from the [http://msdn2.microsoft.com/en-us/library/system.messaging(vs.80).aspx System.Messaging] namespace.
This example uses the [http://msdn2.microsoft.com/en-us/library/system.messaging.messagequeue(VS.80).aspx MessageQueue] class from the [http://msdn2.microsoft.com/en-us/library/system.messaging(vs.80).aspx System.Messaging] namespace.
 +
 +
Messages are sent (and received) as a [http://msdn2.microsoft.com/en-us/library/system.messaging.message(VS.80).aspx Message].
Launch an IronPython console. In this interpreter we will setup the queue and get ready to receive the message:
Launch an IronPython console. In this interpreter we will setup the queue and get ready to receive the message:
Line 35: Line 37:
</pre>
</pre>
-
We're now ready to receive this message from the original queue:
+
We're now ready to receive this message back in the original console (we will receive it as bytes and decode it back to text):
<pre>
<pre>
 +
from System import Array, Byte
 +
from System.Text import Encoding
 +
# This blocks until it receives
 +
message = queue.Receive()
 +
 +
messageStream = message.BodyStream
 +
byteArray = Array.CreateInstance(Byte, messageStream.Length)
 +
messageStream.Read(byteArray, 0, messageStream.Length)
 +
 +
messageText = Encoding.UTF8.GetString(byteArray)
 +
print messageText
</pre>
</pre>
 +
This prints:
 +
<pre>
 +
<?xml version="1.0"?>
 +
<string>Hello from IronPython</string>
 +
</pre>
 +
 +
 +
Using [http://msdn2.microsoft.com/en-us/library/system.messaging.xmlmessageformatter(VS.80).aspx XmlMessageFormatter] and [http://msdn2.microsoft.com/en-us/library/system.messaging.binarymessageformatter(VS.80).aspx BinaryMessageFormatter], you can send and receive messages (including files/images etc) without having to deal with the XML de-serialization yourself.
Back to [[Contents]].
Back to [[Contents]].

Current revision as of 14:12, 17 June 2007

From Nauman Leghari.

Message Queuing (MSMQ) is a form of inter-process communication. It allows allows applications running at different times to communicate across heterogeneous networks and systems that might be temporarily offline. Applications send, receive, or peek (read without removing) messages from queues.

Message Queuing is an optional component of Windows XP and Windows 2000/NT. In order to use it, it must be installed. To do this you go to the control panel, select add or remove programs, and then Add/Remove Windows Components.

Installing Message Queuing

(On my system this didn't require the original install disks to do.)

This example uses the MessageQueue class from the System.Messaging namespace.

Messages are sent (and received) as a Message.

Launch an IronPython console. In this interpreter we will setup the queue and get ready to receive the message:

import clr
clr.AddReference('System.Messaging')
from System.Messaging import MessageQueue

ourQueue = '.\\private$\\myqueue'
if not MessageQueue.Exists(ourQueue):
    queue = MessageQueue.Create(ourQueue)
else:
    queue = MessageQueue(ourQueue)

In a separate console, we will access the queue and send a message:

import clr
clr.AddReference('System.Messaging')
from System.Messaging import MessageQueue

ourQueue = '.\\private$\\myqueue'
queue = MessageQueue(ourQueue)
queue.Send('Hello from IronPython')

We're now ready to receive this message back in the original console (we will receive it as bytes and decode it back to text):

from System import Array, Byte
from System.Text import Encoding

# This blocks until it receives
message = queue.Receive()

messageStream = message.BodyStream
byteArray = Array.CreateInstance(Byte, messageStream.Length)
messageStream.Read(byteArray, 0, messageStream.Length)

messageText = Encoding.UTF8.GetString(byteArray)
print messageText

This prints:

<?xml version="1.0"?>
<string>Hello from IronPython</string>


Using XmlMessageFormatter and BinaryMessageFormatter, you can send and receive messages (including files/images etc) without having to deal with the XML de-serialization yourself.


Back to Contents.

TOOLBOX
LANGUAGES