Enterprise Integration Zone is brought to you in partnership with:

Dariusz Suchojad has 12 years’ experience in enterprise architecture and software engineering with 8 years in EAI/ESB/SOA/BPM/SSO in telecommunications and banking. He feels equally well in dissecting proprietary protocols, developing systems of systems, talking businesses over and anything in between. Having spent far too many nights on putting out fires caused by the poor quality of solutions he had to use daily, he quit his job and spent almost 2 years on creating Zato, which is a Python-based ESB for SOA, cloud integrations and backend servers. Dariusz has posted 5 posts at DZone. You can read more from them at their website. View Full User Profile

The Splendid State of Python and WebSphere MQ Programming

09.14.2013
| 2943 views |
  • submit to reddit

Intro

This post summarizes the options programmers have when it comes to WebSphere MQ programming with Python.

WebSphere MQ is a tool for everyone, and no matter what your background is, C, C++, Java, COBOL, Visual Basic or anything, you will find something interesting in Python too.

If you've never heard of WebSphere MQ before, I'll explain it, risking a slight oversimplification.  In queueing, WebSphere AMQP is to AMQP what Oracle DB is to PostgreSQL in RDBMS land.

The Actors

There are three libraries and projects Python programmers can pick from, and they're all open source.  All of them let you connect and exchange messages with any MQ application, regardless of what language or languages it's written in.

Each of them builds on the previous one to provide more useful features with less manual coding.

PyMQI

PyMQI is the lowest level library that provides direct access to all the primitives that are usually associated with MQ programming in other languages: queues, topics, queue managers, subscriptions, PCF commands and anything else that is needed for manipulating MQ objects directly.

PyMQI is the tool to use for quick one-off jobs, particularly if you're already familiar with MQI, which is a C API that WebSphere MQ provides. PyMQI mostly provides an object-oriented Python layer on top of MQI.

If you're an admin, and you need something convenient to build a useful tool very quickly, Python and PyMQI are the tools to use.

Here's some sample code to put a message in a queue:

import pymqi
 
queue_manager = 'QM01'
channel = 'SVRCONN.1'
host = '192.168.1.135'
port = '1434'
queue_name = 'TEST.1'
message = 'Hello from Python!'
conn_info = '{}({})'.format(host, port)
 
qmgr = pymqi.connect(queue_manager, channel, conn_info)
 
queue = pymqi.Queue(qmgr, queue_name)
queue.put(message)
queue.close()
 
qmgr.disconnect()

PyMQI usage examples.


Spring Python

Spring Python takes the good parts of Spring for Java and implements them in a Pythonic way. Among other things, one can find the support for JMS WebSphere MQ programming in Python.  In other words, completely seamless means to exchange messages with Java apps from Python ones.

For those who are curious, this is achieved by manually constructing and parsing all the MQRFH2 headers required behind the scenes.

On top of that, it has connection factories, background listeners and a JMS Template, which is a higher-level object with which to manipulate MQ messages.

Use Spring Python if:

  • You need a higher level API.
  • You're okay with manual coding.
  • You've already used Spring for Java elsewhere.

Note that Spring Python now requires the use of JMS, but this is something that will soon change and this post will be updated to reflect it.

Here's the sample code to put a message in a queue:

from springpython.jms.core import JmsTemplate
from springpython.jms.factory import WebSphereMQConnectionFactory
 
qm = 'QM.1'
channel = 'SVRCONN1.1'
host = '192.168.1.121'
port = '1434'
queue1 = 'TEST.1'
 
factory = WebSphereMQConnectionFactory(qm, channel, host, port)
 
jms_template = JmsTemplate(factory)
jms_template.send('Hello world', queue1)
 
factory.destroy()

Spring Python usage examples

Zato

[Full disclosure: I am the creator of Zato]

Zato is a full-fledged ESB and backend application server written in Python which supports MQ and a lot of other technologies and protocols and data formats.

Its abstractions are on the highest level, and it requires very little coding. In fact, there's no MQ-specific programming needed at all. One can simply focus on receiving requests (which requires zero coding) and producing the messages to send while Zato establishes connections, reconnects when necessary, adds headers and anything else that should be done.  You just create a message and the rest is handled automatically.

Here's the sample code to put a message in a queue:

from zato.server.service import Service
 
class MyService(Service):
  def handle(self):
    self.outgoing.jms_wmq.send('msg', 'connection-name', 'Q.1')

More Zato MQ usage examples.

One can forget about the particularities of WebSphere MQ because such details are configured using the GUI (as below), API, CLI or en masse in JSON.

Form for creating an MQ connection definitionForm for creating an MQ connection itself

Summary

Published at DZone with permission of its author, Dariusz Suchojad. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)