The Splendid State of Python and WebSphere MQ Programming
Join the DZone community and get the full member experience.
Join For FreeIntro
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()
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()
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')
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.


Summary
- Use PyMQI for quick tasks or low-level functionality.
- Use Spring Python for a higher level API or if you're familiar with Java.
- Use Zato if you want a full-fledged ESB and application server with load-balancing, hot-deployment and other features.
Published at DZone with permission of Dariusz Suchojad. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Top Six React Development Tools
-
Decoding eBPF Observability: How eBPF Transforms Observability as We Know It
-
Building the World's Most Resilient To-Do List Application With Node.js, K8s, and Distributed SQL
-
The Role of AI and Programming in the Gaming Industry: A Look Beyond the Tables
Comments