0

Hi has anyone created a message listener using IBM MQ/PYMQI using Python? I am able to queue and put messages on a channel using PyMQI, but I am unsure if there is a way to create a listener from it that can get messages in realtime. Has anyone worked on something like this or has any resources?

soniccool
  • 5,790
  • 22
  • 60
  • 98
  • You can poll (loop and do a get with wait) queue. I do not think that MQCB is implemented in pymqi, if it was it would allow for an async listener. – JoshMc Oct 25 '22 at 01:08

1 Answers1

0

You can use a while loop to be connected and poll for new messages.

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()
Vishnu
  • 16
  • 4