Search code examples
ibm-mqpymqi

IBM MQ Listener using PyMQI/Python


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?


Solution

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