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!

xmsconsumer xmsproducer

A non Python programmer’s attempt at some Python…

Three simple steps

1. Start the subscriber first 🙂

xmsconsumer0

2. Publish your message.

xmsproducer

3. Does it work? only one way to find out…

xmsconsumer

So, what does the application look like?

xmsproducercode

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!

Advertisements