Support Center
The purpose of this chapter is to gather code showing PyMQI in action or code that is related to common IBM MQ-related tasks in general.
All the samples are self-contained and ready to use in your own PyMQI applications.
import pymqi
queue_manager = 'QM1'
channel = 'DEV.APP.SVRCONN'
host = '127.0.0.1'
port = '1414'
conn_info = '%s(%s)' % (host, port)
qmgr = pymqi.connect(queue_manager, channel, conn_info)
qmgr.disconnect()
import pymqi
queue_manager = 'QM1'
channel = 'DEV.APP.SVRCONN'
host = '127.0.0.1'
port = '1414'
conn_info = '%s(%s)' % (host, port)
user = 'app'
password = 'mypassword'
qmgr = pymqi.connect(queue_manager, channel, conn_info, user, password)
qmgr.disconnect()
Notes:
import pymqi
queue_manager = 'QM1'
channel = 'DEV.APP.SVRCONN'
host = '127.0.0.1'
port = '1414'
queue_name = 'TEST.1'
message = 'Hello from Python!'
conn_info = '%s(%s)' % (host, port)
qmgr = pymqi.connect(queue_manager, channel, conn_info)
queue = pymqi.Queue(qmgr, queue_name)
queue.put(message)
queue.close()
qmgr.disconnect()
import pymqi
queue_manager = 'QM1'
channel = 'DEV.APP.SVRCONN'
host = '127.0.0.1'
port = '1414'
queue_name = 'TEST.1'
conn_info = '%s(%s)' % (host, port)
qmgr = pymqi.connect(queue_manager, channel, conn_info)
queue = pymqi.Queue(qmgr, queue_name)
message = queue.get()
queue.close()
qmgr.disconnect()
Notes:
import pymqi
queue_manager = 'QM1'
channel = 'DEV.APP.SVRCONN'
host = '127.0.0.1'
port = '1414'
queue_name = 'TEST.1'
message = u'My Unicode data'
conn_info = '%s(%s)' % (host, port)
bytes_encoding = 'iso-8859-1'
default_ccsid = 819
qmgr = pymqi.connect(
queue_manager, channel, conn_info,
bytes_encoding=bytes_encoding,
default_ccsid=default_ccsid
)
queue = pymqi.Queue(qmgr, queue_name)
queue.put(message)
queue.close()
qmgr.disconnect()
Notes:
import pymqi
queue_manager = 'QM1'
channel = 'DEV.APP.SVRCONN'
host = '127.0.0.1'
port = '1414'
queue_name = 'TEST.1'
conn_info = '%s(%s)' % (host, port)
qmgr = pymqi.connect(queue_manager, channel, conn_info)
queue = pymqi.Queue(qmgr, queue_name)
# Get the message but discard any JMS headers
message = queue.get_no_jms()
# Works exactly as above: get_no_rfh2 is an alias to get_no_jms
message = queue.get_no_rfh2()
# Close queue and disconnect from queue manager
queue.close()
qmgr.disconnect()
Notes:
import pymqi
queue_manager = 'QM1'
channel = 'DEV.APP.SVRCONN'
host = '127.0.0.1'
port = '1414'
queue_name = 'TEST.1'
conn_info = '%s(%s)' % (host, port)
# Message Descriptor
md = pymqi.MD()
# Get Message Options
gmo = pymqi.GMO()
gmo.Options = pymqi.CMQC.MQGMO_WAIT | pymqi.CMQC.MQGMO_FAIL_IF_QUIESCING
gmo.WaitInterval = 5000 # 5 seconds
qmgr = pymqi.connect(queue_manager, channel, conn_info)
queue = pymqi.Queue(qmgr, queue_name)
message = queue.get(None, md, gmo)
queue.close()
qmgr.disconnect()
Notes:
import pymqi
queue_manager = 'QM1'
channel = 'DEV.APP.SVRCONN'
host = '127.0.0.1'
port = '1414'
queue_name = 'TEST.1'
conn_info = '%s(%s)' % (host, port)
# Message Descriptor
md = pymqi.MD()
# Get Message Options
gmo = pymqi.GMO()
gmo.Options = pymqi.CMQC.MQGMO_WAIT | pymqi.CMQC.MQGMO_FAIL_IF_QUIESCING
gmo.WaitInterval = 5000 # 5 seconds
qmgr = pymqi.connect(queue_manager, channel, conn_info)
queue = pymqi.Queue(qmgr, queue_name)
keep_running = True
while keep_running:
try:
# Wait up to to gmo.WaitInterval for a new message.
message = queue.get(None, md, gmo)
# Process the message here..
# Reset the MsgId, CorrelId & GroupId so that we can reuse
# the same 'md' object again.
md.MsgId = pymqi.CMQC.MQMI_NONE
md.CorrelId = pymqi.CMQC.MQCI_NONE
md.GroupId = pymqi.CMQC.MQGI_NONE
except pymqi.MQMIError as e:
if e.comp == pymqi.CMQC.MQCC_FAILED and e.reason == pymqi.CMQC.MQRC_NO_MSG_AVAILABLE:
# No messages, that is OK, we can ignore it.
pass
else:
# Some other error condition.
raise
queue.close()
qmgr.disconnect()
Notes:
import pymqi
queue_manager = 'QM1'
channel = 'DEV.APP.SVRCONN'
host = '127.0.0.1'
port = '1414'
conn_info = '%s(%s)' % (host, port)
message = 'Please reply to a dynamic queue, thanks.'
dynamic_queue_prefix = 'MY.REPLIES.*'
request_queue = 'TEST.1'
qmgr = pymqi.connect(queue_manager, channel, conn_info)
# Dynamic queue's object descriptor.
dyn_od = pymqi.OD()
dyn_od.ObjectName = 'SYSTEM.DEFAULT.MODEL.QUEUE'
dyn_od.DynamicQName = dynamic_queue_prefix
# Open the dynamic queue.
dyn_input_open_options = pymqi.CMQC.MQOO_INPUT_EXCLUSIVE
dyn_queue = pymqi.Queue(qmgr, dyn_od, dyn_input_open_options)
dyn_queue_name = dyn_od.ObjectName.strip()
# Prepare a Message Descriptor for the request message.
md = pymqi.MD()
md.ReplyToQ = dyn_queue_name
# Send the message.
queue = pymqi.Queue(qmgr, request_queue)
queue.put(message, md)
# Get and process the response here..
dyn_queue.close()
queue.close()
qmgr.disconnect()
Notes:
import pymqi
queue_manager = 'QM1'
channel = 'DEV.APP.SVRCONN'
host = '127.0.0.1'
port = '1414'
queue_name = 'TEST.1'
message = 'Here's a reply'
conn_info = '%s(%s)' % (host, port)
qmgr = pymqi.connect(queue_manager, channel, conn_info)
md = pymqi.MD()
queue = pymqi.Queue(qmgr, queue_name)
message = queue.get(None, md)
reply_to_queue_name = md.ReplyToQ.strip()
reply_to_queue = pymqi.Queue(qmgr, reply_to_queue_name)
reply_to_queue.put(message)
queue.close()
qmgr.disconnect()
Notes:
import pymqi
queue_manager = 'QM1'
channel = 'DEV.APP.SVRCONN'
host = '127.0.0.1'
port = '1414'
topic_string = '/currency/rate/EUR/USD'
msg = '1.3961'
conn_info = '%s(%s)' % (host, port)
qmgr = pymqi.QueueManager(None)
qmgr.connect_tcp_client(queue_manager, pymqi.CD(), channel, conn_info)
topic = pymqi.Topic(qmgr, topic_string=topic_string)
topic.open(open_opts=pymqi.CMQC.MQOO_OUTPUT)
topic.pub(msg)
topic.close()
qmgr.disconnect()
Notes:
# stdlib
import logging
# PyMQI
import pymqi
logging.basicConfig(level=logging.INFO)
queue_manager = 'QM1'
channel = 'DEV.APP.SVRCONN'
host = '127.0.0.1'
port = '1414'
topic_string = '/currency/rate/EUR/USD'
msg = '1.3961'
conn_info = '%s(%s)' % (host, port)
qmgr = pymqi.QueueManager(None)
qmgr.connect_tcp_client(queue_manager, pymqi.CD(), channel, conn_info)
sub_desc = pymqi.SD()
sub_desc['Options'] = pymqi.CMQC.MQSO_CREATE + pymqi.CMQC.MQSO_RESUME + \
pymqi.CMQC.MQSO_DURABLE + pymqi.CMQC.MQSO_MANAGED
sub_desc.set_vs('SubName', 'MySub')
sub_desc.set_vs('ObjectString', topic_string)
sub = pymqi.Subscription(qmgr)
sub.sub(sub_desc=sub_desc)
get_opts = pymqi.GMO(
Options=pymqi.CMQC.MQGMO_NO_SYNCPOINT + pymqi.CMQC.MQGMO_FAIL_IF_QUIESCING + pymqi.CMQC.MQGMO_WAIT)
get_opts['WaitInterval'] = 15000
data = sub.get(None, pymqi.md(), get_opts)
logging.info('Here is the received data: [%s]' % data)
sub.close(sub_close_options=pymqi.CMQC.MQCO_KEEP_SUB, close_sub_queue=True)
qmgr.disconnect()
Notes:
ctypes <http://docs.python.org/library/ctypes.html>
_ and cannot be set directly through the regular dictionary assignment like the 'Options' have been set.# stdlib
import logging
# PyMQI
import pymqi
logging.basicConfig(level=logging.INFO)
queue_manager = 'QM1'
channel = 'SSL.SVRCONN.1'
host = '127.0.0.1'
port = '1414'
queue_name = 'TEST.1'
conn_info = '%s(%s)' % (host, port)
ssl_cipher_spec = 'TLS_RSA_WITH_AES_256_CBC_SHA'
key_repo_location = '/var/mqm/ssl-db/client/KeyringClient'
message = 'Hello from Python!'
cd = pymqi.CD()
cd.ChannelName = channel
cd.ConnectionName = conn_info
cd.ChannelType = pymqi.CMQC.MQCHT_CLNTCONN
cd.TransportType = pymqi.CMQC.MQXPT_TCP
cd.SSLCipherSpec = ssl_cipher_spec
sco = pymqi.SCO()
sco.KeyRepository = key_repo_location
qmgr = pymqi.QueueManager(None)
qmgr.connect_with_options(queue_manager, cd, sco)
put_queue = pymqi.Queue(qmgr, queue_name)
put_queue.put(message)
get_queue = pymqi.Queue(qmgr, queue_name)
logging.info('Here is the message again: [%s]' % get_queue.get())
put_queue.close()
get_queue.close()
qmgr.disconnect()
# stdlib
import logging
# PyMQI
import pymqi
logging.basicConfig(level=logging.INFO)
queue_manager = 'QM1'
channel = 'DEV.APP.SVRCONN'
host = '127.0.0.1'
port = '1414'
queue_name = 'TEST.1'
message = 'Hello from Python!'
conn_info = '%s(%s)' % (host, port)
priority = 2
put_md = pymqi.MD()
put_md.Priority = priority
qmgr = pymqi.connect(queue_manager, channel, conn_info)
put_queue = pymqi.Queue(qmgr, queue_name)
put_queue.put(message, put_md)
get_md = pymqi.MD()
get_queue = pymqi.Queue(qmgr, queue_name)
message_body = get_queue.get(None, get_md)
logging.info('Received a message, priority `%s`.' % get_md.Priority)
put_queue.close()
get_queue.close()
qmgr.disconnect()
Notes:
import pymqi
import CMQXC
queue_manager = 'QM1'
channel = 'DEV.APP.SVRCONN'
host = '127.0.0.1'
port = '1414'
queue_name = 'TEST.1'
message = 'Hello from Python!' * 10000
conn_info = '%s(%s)' % (host, port)
cd = pymqi.CD()
cd.MsgCompList[1] = CMQXC.MQCOMPRESS_ZLIBHIGH
qmgr = pymqi.connect(queue_manager, channel, conn_info)
queue = pymqi.Queue(qmgr, queue_name)
queue.put(message)
queue.close()
qmgr.disconnect()
Notes:
# stdlib
import logging
# PyMQI
import pymqi
queue_manager = 'QM1'
channel = 'DEV.APP.SVRCONN'
host = 'localhost.invalid' # Note the invalid hostname here
port = '1414'
conn_info = '%s(%s)' % (host, port)
try:
qmgr = pymqi.connect(queue_manager, channel, conn_info)
except pymqi.MQMIError as e:
if e.comp == pymqi.CMQC.MQCC_FAILED and e.reason == pymqi.CMQC.MQRC_HOST_NOT_AVAILABLE:
logging.error('Such a host `%s` does not exist.' % host)
Notes:
import logging
import rpm
logging.basicConfig(level=logging.INFO)
package_name = 'MQSeriesClient'
ts = rpm.TransactionSet()
mi = ts.dbMatch('name', package_name)
if not mi.count():
logging.info('Did not find package [%s] in RPM database.' % package_name)
else:
for header in mi:
version = header['version']
msg = 'Found package `%s`, version `%s`.' % (package_name, version)
logging.info(msg)
Notes:
import logging
import _winreg
logging.basicConfig(level=logging.INFO)
key_name = 'Software\\IBM\\MQSeries\\CurrentVersion'
try:
key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, key_name)
except WindowsError:
logging.info('Could not find IBM MQ-related information in Windows registry.')
else:
version = _winreg.QueryValueEx(key, 'VRMF')[0]
logging.info('IBM MQ version is `%s`.' % version)
Notes:
import pymqi
queue_manager = 'QM1'
channel = 'DEV.APP.SVRCONN'
host = '127.0.0.1'
port = '1414'
queue_name = 'TEST.1'
message = 'Hello from Python!'
alternative_user_id = 'myuser'
conn_info = '%s(%s)' % (host, port)
qmgr = pymqi.connect(queue_manager, channel, conn_info)
od = pymqi.OD()
od.ObjectName = queue_name
od.AlternateUserId = alternative_user_id
queue = pymqi.Queue(qmgr)
queue.open(od, pymqi.CMQC.MQOO_OUTPUT | pymqi.CMQC.MQOO_ALTERNATE_USER_AUTHORITY)
queue.put(message)
queue.close()
qmgr.disconnect()
Notes:
import pymqi
queue_manager = 'QM1'
channel = 'DEV.APP.SVRCONN'
host = '127.0.0.1'
port = '1414'
queue_name = 'TEST.1'
message = 'Hello from Python!'
conn_info = '%s(%s)' % (host, port)
cd = pymqi.CD()
cd.ChannelName = channel
cd.ConnectionName = conn_info
cd.ChannelType = pymqi.CMQC.MQCHT_CLNTCONN
cd.TransportType = pymqi.CMQC.MQXPT_TCP
connect_options = pymqi.CMQC.MQCNO_HANDLE_SHARE_BLOCK
qmgr = pymqi.QueueManager(None)
for x in range(10):
qmgr.connect_with_options(queue_manager, cd=cd, opts=connect_options)
qmgr.connect_with_options(queue_manager, cd=cd, opts=connect_options)
queue = pymqi.Queue(qmgr, queue_name)
queue.put(message)
queue.close()
qmgr.disconnect()
import pymqi
queue_manager = 'QM1'
channel = 'DEV.APP.SVRCONN'
host = '127.0.0.1'
port = '1414'
queue_name = 'TEST.1'
message = 'Hello from Python!'
conn_info = '%s(%s)' % (host, port)
qmgr = pymqi.QueueManager(None)
qmgr.connect_tcp_client(queue_manager, pymqi.CD(), channel, conn_info)
try:
qmgr.connect_tcp_client(queue_manager, pymqi.CD(), channel, conn_info)
except pymqi.MQMIError as e:
if e.comp == pymqi.CMQC.MQCC_WARNING and e.reason == pymqi.CMQC.MQRC_ALREADY_CONNECTED:
# Move along, nothing to see here..
pass
queue = pymqi.Queue(qmgr, queue_name)
queue.put(message)
queue.close()
qmgr.disconnect()
Notes:
import pymqi
queue_manager = 'QM1'
channel = 'DEV.APP.SVRCONN'
host = '127.0.0.1'
port = '1414'
conn_info = '%s(%s)' % (host, port)
channel_name = 'MYCHANNEL.1'
channel_type = pymqi.CMQXC.MQCHT_SVRCONN
args = {pymqi.CMQCFC.MQCACH_CHANNEL_NAME: channel_name,
pymqi.CMQCFC.MQIACH_CHANNEL_TYPE: channel_type}
qmgr = pymqi.connect(queue_manager, channel, conn_info)
pcf = pymqi.PCFExecute(qmgr)
pcf.MQCMD_CREATE_CHANNEL(args)
qmgr.disconnect()
Notes:
import pymqi
queue_manager = 'QM1'
channel = 'DEV.APP.SVRCONN'
host = '127.0.0.1'
port = '1414'
conn_info = '%s(%s)' % (host, port)
queue_name = 'MYQUEUE.1'
queue_type = pymqi.CMQC.MQQT_LOCAL
max_depth = 123456
args = {pymqi.CMQC.MQCA_Q_NAME: queue_name,
pymqi.CMQC.MQIA_Q_TYPE: queue_type,
pymqi.CMQC.MQIA_MAX_Q_DEPTH: max_depth}
qmgr = pymqi.connect(queue_manager, channel, conn_info)
pcf = pymqi.PCFExecute(qmgr)
pcf.MQCMD_CREATE_Q(args)
qmgr.disconnect()
Notes:
import logging
import pymqi
logging.basicConfig(level=logging.INFO)
queue_manager = 'QM1'
channel = 'DEV.APP.SVRCONN'
host = '127.0.0.1'
port = '1414'
conn_info = '%s(%s)' % (host, port)
prefix = 'SYSTEM.*'
args = {pymqi.CMQCFC.MQCACH_CHANNEL_NAME: prefix}
qmgr = pymqi.connect(queue_manager, channel, conn_info)
pcf = pymqi.PCFExecute(qmgr)
try:
response = pcf.MQCMD_INQUIRE_CHANNEL(args)
except pymqi.MQMIError as e:
if e.comp == pymqi.CMQC.MQCC_FAILED and e.reason == pymqi.CMQC.MQRC_UNKNOWN_OBJECT_NAME:
logging.info('No channels matched prefix `%s`' % prefix)
else:
raise
else:
for channel_info in response:
channel_name = channel_info[CMQCFC.MQCACH_CHANNEL_NAME]
logging.info('Found channel `%s`' % channel_name)
qmgr.disconnect()
Notes:
import logging
import pymqi
logging.basicConfig(level=logging.INFO)
queue_manager = 'QM1'
channel = 'DEV.APP.SVRCONN'
host = '127.0.0.1'
port = '1414'
conn_info = '%s(%s)' % (host, port)
prefix = 'SYSTEM.*'
queue_type = pymqi.CMQC.MQQT_MODEL
args = {pymqi.CMQC.MQCA_Q_NAME: prefix,
pymqi.CMQC.MQIA_Q_TYPE: queue_type}
qmgr = pymqi.connect(queue_manager, channel, conn_info)
pcf = pymqi.PCFExecute(qmgr)
try:
response = pcf.MQCMD_INQUIRE_Q(args)
except pymqi.MQMIError as e:
if e.comp == pymqi.CMQC.MQCC_FAILED and e.reason == pymqi.CMQC.MQRC_UNKNOWN_OBJECT_NAME:
logging.info('No queues matched given arguments.')
else:
raise
else:
for queue_info in response:
queue_name = queue_info[pymqi.CMQC.MQCA_Q_NAME]
logging.info('Found queue `%s`' % queue_name)
qmgr.disconnect()
Notes:
import logging
import pymqi
logging.basicConfig(level=logging.INFO)
qmgr = pymqi.connect(queue_manager, channel, conn_info)
pcf = pymqi.PCFExecute(qmgr)
attrs = {
CMQC.MQCA_Q_NAME :'*',
CMQC.MQIA_Q_TYPE : CMQC.MQQT_LOCAL,
CMQCFC.MQIACF_Q_ATTRS : CMQC.MQCA_Q_NAME
}
filter1 = pymqi.Filter(CMQC.MQCA_Q_DESC).like('IBM MQ *')
filter2 = pymqi.Filter(CMQC.MQIA_CURRENT_Q_DEPTH).greater(2)
result = pcf.MQCMD_INQUIRE_Q(attrs, [filter1, filter2])
logging.info('Result is %s', result)
Notes:
import pymqi
queue_manager = 'QM1'
channel = 'DEV.APP.SVRCONN'
host = '127.0.0.1'
port = '1414'
conn_info = '%s(%s)' % (host, port)
qmgr = pymqi.connect(queue_manager, channel, conn_info)
pcf = pymqi.PCFExecute(qmgr)
pcf.MQCMD_PING_Q_MGR()
qmgr.disconnect()
Notes: