如何获取 MQ 队列列表 pymqi?

How Get a list of MQ queues pymqi?

您想获取特定队列管理器的队列列表。我似乎明白如何做到这一点,但当我尝试时,我得到一个错误。

Traceback (most recent call last): File "D:/project/Work-Project/queue list.py", line 23, in response = pcf.MQCMD_INQUIRE_Q(args) File "C:\Users\ShevcovAA\AppData\Local\Programs\Python\Python37\lib\site-packages\pymqi_init_.py", line 2769, in call message = self._pcf.reply_queue.get(None, get_md, get_opts) File "C:\Users\ShevcovAA\AppData\Local\Programs\Python\Python37\lib\site-packages\pymqi_init.py", line 2021, in get raise MQMIError(rv[-2], rv[-1], message=rv[0], original_length=rv[-3]) pymqi.MQMIError: MQI Error. Comp: 2, Reason 2033: FAILED: MQRC_NO_MSG_AVAILABLE

我的代码:

import logging
import re
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 = "*"
queue_type = pymqi.CMQC.MQQT_LOCAL

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)

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()

只需安装以下版本即可解决此错误。也就是Meln原来是PyMQi 1.12.0,现在是PyMQI 1.11.0

我的代码:

import pymqi
import date_conn

qmgr = pymqi.connect(date_conn.queue_manager, date_conn.channel, date_conn.conn_info)
pcf = pymqi.PCFExecute(qmgr)

c = 0
attrs = {
        pymqi.CMQC.MQCA_Q_NAME :'*'
    }
result = pcf.MQCMD_INQUIRE_Q(attrs)

for queue_info in result:
        queue_name = queue_info[pymqi.CMQC.MQCA_Q_NAME]
        print(queue_name)
        c+=1

print(c)
qmgr.disconnect()

v1.12.0 pymqi 使用不同的逻辑从响应队列中获取 PCF 响应消息。

默认情况下,使用 5 秒的超时时间来等待响应。 因此,如果您有很多队列或您的 QM 负载很重,这可能还不够。

要解决此问题,您可以使用 PCFExecute 构造函数的 response_wait_interval 参数增加此间隔。

pcf = pymqi.PCFExecute(qmgr, response_wait_interval=30000) # 30 seconds

v1.11.0 没有此参数,使用默认间隔 30 秒。

并避免查询每个队列的深度,只需查询 MQIA_CURRENT_Q_DEPTH 属性。

在 v1.12+ 支持的新表示法中,它将类似于:

    attrs = []  # type: List[pymqi.MQOpts]
    attrs.append(pymqi.CFST(Parameter=pymqi.CMQC.MQCA_Q_NAME,
                            String=pymqi.ensure_bytes(prefix)))
    attrs.append(pymqi.CFIN(Parameter=pymqi.CMQC.MQIA_Q_TYPE,
                            Value=queue_type))
    attrs.append(pymqi.CFIL(Parameter=pymqi.CMQCFC.MQIACF_Q_ATTRS,
                            Values=[pymqi.CMQC.MQIA_CURRENT_Q_DEPTH]))

    object_filters = []
#        object_filters.append(
#            pymqi.CFIF(Parameter=pymqi.CMQC.MQIA_CURRENT_Q_DEPTH,
#                       Operator=pymqi.CMQCFC.MQCFOP_GREATER,
#                       FilterValue=0))

    response = pcf.MQCMD_INQUIRE_Q(attrs, object_filters)

    for queue_info in response:
        queue_name = queue_info[pymqi.CMQC.MQCA_Q_NAME]
        queue_depth = queue_info[pymqi.CMQC.MQIA_CURRENT_Q_DEPTH]
        print('{}: {} message(s)'.format(queue_name.strip().decode(), queue_depth))