Over a million developers have joined DZone.

The Splendid State of Python and WebSphere MQ Programming

· Web Dev Zone

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.


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 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 = ''
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)

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 = ''
port = '1434'
queue1 = 'TEST.1'
factory = WebSphereMQConnectionFactory(qm, channel, host, port)
jms_template = JmsTemplate(factory)
jms_template.send('Hello world', queue1)

Spring Python usage examples


[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


Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.


Published at DZone with permission of Dariusz Suchojad. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}