记录一下,环境基本信息是
# pymqi-1.7.2.post1 windows 7 64 ,Python2.7.15 ibmmq 7.0 client
# pymqi-1.7.2.post1 windows 7 64 ,Python2.7.15 ibmmq 7.0 client
import pymqi
queue_manager = 'DZD_ZSSS_01_TST84'
channel = 'SVRCONN'
host = '100.100.100.84'
port = '2403'
queue_name = 'S_TMP'
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()
另一个是 连接 MQ获取队列深度。是参看别人的例子:https://blog.csdn.net/zhaoyangjian724/article/details/77094892
import sys
import os
import pymqi
import time
import re
if len(sys.argv) != 5:
print sys.argv[0] + ' ' + 'queue_manager' + ' ' + 'channel' + ' ' + 'host' + ' ' + 'port'
else:
queue_manager = sys.argv[1]
channel = sys.argv[2]
host = sys.argv[3]
port = sys.argv[4]
conn_info = "%s(%s)" % (host, port)
prefix = "*"
queue_type = pymqi.CMQC.MQQT_LOCAL
args = {pymqi.CMQC.MQCA_Q_NAME: prefix,
pymqi.CMQC.MQIA_Q_TYPE: queue_type}
print '-----------------------------------------'
print args
print '-----------------------------------------'
qmgr = pymqi.connect(queue_manager, channel, conn_info)
pcf = pymqi.PCFExecute(qmgr)
response = pcf.MQCMD_INQUIRE_Q(args)
for queue_info in response:
queue_name = queue_info[pymqi.CMQC.MQCA_Q_NAME]
if (re.match('^SYSTEM', queue_name) or re.match('^AMQ', queue_name) or re.match('^MQ', queue_name)):
pass
else:
q = pymqi.Queue(qmgr, queue_name)
print(queue_name.strip() + ':' + 'Queue depth:', q.inquire(pymqi.CMQC.MQIA_CURRENT_Q_DEPTH))
q.close()
qmgr.disconnect()
执行代码时遇到了不懂怎么传入参数。。。。参看了:https://blog.csdn.net/jacke121/article/details/77867939
执行效果:
2.7安装的时候 装了 VCForPython27.msi
pip 升级到了10.x
随便练习一下,补了一个循环读取队列信息,并打印
# -*- coding: UTF-8 -*-
import pymqi
import time
queue_manager = 'TIS_ZSPD_01_TST84'
channel = 'SVRCONN'
host = '100.100.100.84'
port = '4310'
queue_name = 'S_DCL_SERVER'
message = 'Hello from Python!'
conn_info = '%s(%s)' % (host, port)
qmgr = pymqi.connect(queue_manager, channel, conn_info)
getq = pymqi.Queue(qmgr, queue_name)
while 1 == 1:
try:
print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
count = getq.inquire(pymqi.CMQC.MQIA_CURRENT_Q_DEPTH)
print '当前队列深度:' + str(count)
# count = 0
if count > 0:
getMsg = pymqi.Queue(qmgr, queue_name)
s = getMsg.get()
# 查找关键位置信息
start = s.index('<MSG>')
end = s.index('</MSG>')
print ('start end:', start, end)
# 截取字符串
tmp = s[start:end+6]
print('RCV:', s)
print '截取后的字符串:'+ tmp
else:
# 休眠1秒
print '当前队列:'+ queue_name +'>>>队列内容为空>>>'
time.sleep(1)
except BaseException,e:
print e
qmgr.disconnect()
qmgr = pymqi.connect(queue_manager, channel, conn_info)
getq = pymqi.Queue(qmgr, queue_name)
# break
else:
print '-----------------------'
给出MQ 相关有学习价值的相关知识网页
如何创建MQ 及其基础知识
https://www.ibm.com/developerworks/cn/websphere/library/techarticles/0712_dunn/0712_dunn.html
pymqi 自己的主页 sample
https://dsuch.github.io/pymqi/examples.html#how-to-wait-for-multiple-messages
一些MQ 的关键参数的含义