Using IronPython and XMS .NET under-the-covers. Works like a charm for Point-to-point messaging and Publish/Subscribe messaging using WebSphere MQ.
And, you can change five lines and connect to WebSphere Platform Messaging (WebSphere Application Server, WebSphere ESB and WebSphere Process Server) or connect to WebSphere Brokers using the Real-time transport!
A non Python programmer’s attempt at some Python…
Three simple steps
1. Start the subscriber first
2. Publish your message.
3. Does it work? only one way to find out…
So, what does the application look like?
Q&A
Q: What is XMS?
A: XMS is like JMS, but non-Java, a nice summary is here.
Q: Hmm.. sounds interesting, I really want to know more!
A: The XMS book is for you!
Q: That’s all nice and well, but what is this funky command line interface you’re using?
A: Console! More info here.
Q: Do you really use Notepad to write programs?
A: Yeah.. why not. What’s wrong with Notepad?
Q: Really?
A: Uh! no way, this is just a silly example!
Producer/Publisher application source
import System
from System import *import clr
clr.AddReferenceToFile(”IBM.XMS.dll”)
from IBM.XMS import *#
# config
#
host, port, mode, qm = “localhost”, 1414, 1, “QM1″#
# logic
#
try:# Create the connection factory factory
ff = XMSFactoryFactory.GetInstance(XMSC.CT_WMQ)# Use the connection factory factory to create a connection factory
cf = ff.CreateConnectionFactory()# Set the properties
cf.SetStringProperty(XMSC.WMQ_HOST_NAME, host)
cf.SetIntProperty(XMSC.WMQ_PORT, port)
cf.SetIntProperty(XMSC.WMQ_CONNECTION_MODE, mode)
cf.SetStringProperty(XMSC.WMQ_QUEUE_MANAGER, qm)# Create a connection
connection = cf.CreateConnection()
print “Connection created”# Create a session
# todo: 0 used instead of false
session = connection.CreateSession(0, AcknowledgeMode.AutoAcknowledge)
print “Session created”# Create a destination
destination = session.CreateTopic(”topic://xms/test”)
#destination = session.CreateQueue(”queue://Q1″)
print “Destination created”# Create a publisher
publisher = session.CreateProducer(destination)
print “Publisher created”# Start the connection
connection.Start()# Create a text message
sendMsg = session.CreateTextMessage(”Wow! this actually works! ;-)”)# Send the message using the sender
publisher.Send(sendMsg)
print sendMsg;
print “Message sent!”# Tidy up all the resources.
publisher.Close()
print “Publisher closed”session.Close()
print “Session closed”connection.Close()
print “Connection closed”# We’re finished.
print “Sample execution SUCCESSFUL”except XMSException, (ex):
print “XMSException caught”, ex
print “Sample execution FAILED!”except Exception, (ex):
print “Exception caught”, ex
print “Sample execution FAILED!”
Consumer/Subscriber application source
Very similar…
import System
from System import *import clr
clr.AddReferenceToFile(”IBM.XMS.dll”)
from IBM.XMS import *#
# config
#host, port, mode, qm = “localhost”, 1414, 1, “QM1″
timeout = 10000 # in ms#
# logic
#try:
# Create the connection factory factory
ff = XMSFactoryFactory.GetInstance(XMSC.CT_WMQ)# Use the connection factory factory to create a connection factory
cf = ff.CreateConnectionFactory()# Set the properties
cf.SetStringProperty(XMSC.WMQ_HOST_NAME, host)
cf.SetIntProperty(XMSC.WMQ_PORT, port)
cf.SetIntProperty(XMSC.WMQ_CONNECTION_MODE, mode)
cf.SetStringProperty(XMSC.WMQ_QUEUE_MANAGER, qm)# Create a connection
connection = cf.CreateConnection()
print “Connection created”# Create a session
# todo: 0 used instead of false
session = connection.CreateSession(0, AcknowledgeMode.AutoAcknowledge)
print “Session created”# Create a destination
destination = session.CreateTopic(”topic://xms/test”)
#destination = session.CreateQueue(”queue://Q1″)
print “Destination created”# Create a subscriber
subscriber = session.CreateConsumer(destination)
print “Subscriber created”# Start the connection
connection.Start()# Recieve the message
print “Waiting for message…”
recvMsg = subscriber.Receive(timeout)
#recvMsg = subscriber.ReceiveNoWait()
print recvMsg;
print “Message recieved!”# Tidy up all the resources.
subscriber.Close()
print “Subscriber closed”session.Close()
print “Session closed”connection.Close()
print “Connection closed”# We’re finished.
print “Sample execution SUCCESSFUL”except XMSException, (ex):
print “XMSException caught”, ex
print “Sample execution FAILED!”except Exception, (ex):
print “Exception caught”, ex
print “Sample execution FAILED!”
Note: I copied IBM.XMS.dll to the current working directory. There may be a better way to do it, i.e. somehow pick it from the .NET Global Assembly Cache (GAC) directly!

6 comments
Comments feed for this article
February 14, 2007 at 8:04 am
mbwhite
That is really very neat…. in a similar way you can use Jython and the JMS client. So whatever you’re preferred client and scripting lanaguage access to messaging is very easy.
JRuby is also another option. A demo of JRuby I saw last year looked very promising.
February 14, 2007 at 8:44 am
Brian Cope
OK, so let’s see what environments you can now program to MQ in (I’ll miss out lots, I’m sure, but I’m equally sure someone will fill in the gaps)…
-There’s the good old MQI - traditional, procedural programming in C, COBOL, etc…still the fastest and richest MQ API if you want to get all the bells and whistles, and pretty much on any platform you can think of
-If you like the richness of the MQI but you prefer OO, you could try MQ Classes for Java/C++/.NET
-There’s JMS - if you like Java and you like standards for porting between messaging providers, or if you want to do messaging within J2EE. So MQ’s JMS can be used in standalone Java apps or in J2EE apps within an app server, including Message Driven Beans. And, for free with MQ JMS, you get the Direct IP transport for fast non-persistent pub/sub over WebSphere Event Broker or Message Broker. And of course, there’s also an implementation of JMS for the default messaging provider in WAS
-There’s XMS C/C++ - the equivalent of JMS in (well, you can guess what languages). It can talk to MQ, Direct IP or default provider in WAS. It comes in the box for WebSphere ESB or Process Server, or you can get it as SupportPac IA94. One of the nice things about XMS is that not only does the API look familiar to JMS programmers, but it is fully interoperable with JMS - the messages it produces look identical to those from JMS
-If you prefer .NET, there’s an XMS implementation for that too, with all the same benefits. So if you want to write your JMS-style apps on Windows in C#, VB.NET, Managed C++ etc., get SupportPac IA9H (or WESB or Process Server)
-SAM is Simple Asynchronous Messaging in PHP, now working for at least 4 messaging providers, some of which make use of XMS C/C++ under the covers - go see http://project-sam.awardspace.com/ for more details - this is quite new but looking pretty robust and very cool
-Saket’s posting tells us about using IronPython sitting on top of XMS .NET
-mbwhite’s comment points us at Jython and JRuby to do a similar thing with JMS
So what have I missed?
February 14, 2007 at 2:23 pm
peterbroadhurst
As well as from PHP with SAM, there’s a SupportPac to access WMQ from perl here:
http://www.ibm.com/support/docview.wss?rs=171&uid=swg24000208
February 18, 2008 at 12:09 pm
Kivanc
Where can I find “IBM.XMS.dll”?
February 18, 2008 at 1:38 pm
Saket Rungta
@Kivanc
See XMS .NET link above.
March 20, 2008 at 4:06 pm
Kivanc
@Saket Rungta
I have MQ 5.3 and I have installed IA9H: IBM Message Service Client for .NET. But I am receiving an error message while connecting:
The type initializer for ‘IBM.XMS.WMQ.WmqConnectionFactory’ threw an exception.
at System.Runtime.CompilerServices.RuntimeHelpers._RunClassConstructor(IntPtr type)
at System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(RuntimeTypeHandle type)
at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.ConstructorInfo.Invoke(Object[] parameters)
at IBM.XMS.Impl.XMSFactoryFactoryImpl.CreateConnectionFactory()
at ConsoleApplication1.Program.Main(String[] args) in C:\Documents and Settings\Administrator\Desktop\ConsoleApplication1\ConsoleApplication1\Program.cs:line 22
Do you have any idea why it could be?