|
| 1 | +# More examples are at https://github.com/ibm-messaging/mq-dev-patterns |
| 2 | +# and in code/examples in the source distribution. |
| 3 | + |
| 4 | +""" |
| 5 | +This example browses a queue, formatting any known MQ header structures that it finds. It is |
| 6 | +an extension to the DLH example, in that it deals with a number of other headers |
| 7 | +including the XQH found on messages on transmission queues. |
| 8 | +""" |
| 9 | + |
| 10 | +import ibmmq as mq |
| 11 | +from ibmmq import CMQC |
| 12 | + |
| 13 | +queue_manager = 'QM1' |
| 14 | +channel = 'DEV.ADMIN.SVRCONN' |
| 15 | +host = '127.0.0.1' |
| 16 | +port = '1414' |
| 17 | +conn_info = '%s(%s)' % (host, port) |
| 18 | +user = 'admin' |
| 19 | +password = 'password' |
| 20 | + |
| 21 | +qmgr = mq.connect(queue_manager, channel, conn_info, user, password) |
| 22 | + |
| 23 | +ok = True |
| 24 | + |
| 25 | +od = mq.OD() |
| 26 | +# This is an XMITQ associated with a STOPPED channel, because we want to look at XQH processing |
| 27 | +od.ObjectName = 'QM2.STOPPED' |
| 28 | + |
| 29 | +q = mq.Queue(qmgr, od, CMQC.MQOO_BROWSE) |
| 30 | + |
| 31 | +gmo = mq.GMO() |
| 32 | +gmo.Options = CMQC.MQGMO_BROWSE_FIRST |
| 33 | +cnt = 1 |
| 34 | + |
| 35 | +while ok: |
| 36 | + try: |
| 37 | + md = mq.MD() |
| 38 | + msg = q.get(None, md, gmo) |
| 39 | + |
| 40 | + print('------------------------------------') |
| 41 | + print(f"Message: {cnt}") |
| 42 | + cnt += 1 |
| 43 | + |
| 44 | + gmo.Options = CMQC.MQGMO_BROWSE_NEXT |
| 45 | + fmt = md['Format'] |
| 46 | + |
| 47 | + headers = True |
| 48 | + offset = 0 |
| 49 | + ccsid = md['CodedCharSetId'] |
| 50 | + |
| 51 | + # Iterate through headers that we might expect to see until there are no more. |
| 52 | + while headers: |
| 53 | + print() |
| 54 | + print('------------') |
| 55 | + print(f'Header: {bytes.decode(fmt, "utf8")}') |
| 56 | + print('------------') |
| 57 | + |
| 58 | + if fmt == CMQC.MQFMT_XMIT_Q_HEADER: |
| 59 | + # The XQH definition in Python has only the first part of the XQH in C, with the |
| 60 | + # embedded MQMD excluded. But We can extract both parts explicitly with XQH methods. |
| 61 | + xqh = mq.XQH().get_header(msg) |
| 62 | + print(xqh.to_string()) |
| 63 | + |
| 64 | + # The to_string method changes structure contents, so we first stash the |
| 65 | + # Format in its bytes version. That matches the CMQC definition as a bytes-string. |
| 66 | + emd = mq.XQH().get_embedded_md(msg) |
| 67 | + fmt = emd['Format'] |
| 68 | + ccsid = emd['CodedCharSetId'] |
| 69 | + # print(f'G = {emd['GroupId']}') |
| 70 | + |
| 71 | + print() |
| 72 | + offset += CMQC.MQXQH_CURRENT_LENGTH |
| 73 | + |
| 74 | + print(emd.to_string()) |
| 75 | + |
| 76 | + elif fmt == CMQC.MQFMT_MD_EXTENSION: |
| 77 | + # This will only be seen when looking at transmission queues with the XQH block |
| 78 | + mde = mq.MDE().unpack(msg[offset:offset + CMQC.MQMDE_CURRENT_LENGTH]) |
| 79 | + fmt = mde['Format'] |
| 80 | + ccsid = mde['CodedCharSetId'] |
| 81 | + offset += CMQC.MQMDE_CURRENT_LENGTH |
| 82 | + |
| 83 | + print(mde.to_string()) |
| 84 | + |
| 85 | + elif fmt == CMQC.MQFMT_RF_HEADER_2: |
| 86 | + rfh2 = mq.RFH2() |
| 87 | + rfh2.unpack(msg[offset:], 'utf8') |
| 88 | + fmt = rfh2['Format'] |
| 89 | + offset += rfh2['StrucLength'] |
| 90 | + |
| 91 | + print(rfh2.to_string()) |
| 92 | + |
| 93 | + elif fmt == CMQC.MQFMT_DEAD_LETTER_HEADER: |
| 94 | + dlh = mq.DLH() |
| 95 | + dlh.unpack(msg[offset:offset + CMQC.MQDLH_CURRENT_LENGTH]) |
| 96 | + fmt = dlh['Format'] |
| 97 | + ccsid = dlh['CodedCharSetId'] |
| 98 | + offset += CMQC.MQDLH_CURRENT_LENGTH |
| 99 | + |
| 100 | + print(dlh.to_string()) |
| 101 | + |
| 102 | + else: |
| 103 | + headers = False |
| 104 | + |
| 105 | + # And now print the message body. Strings get converted, otherwise just print bytes |
| 106 | + print() |
| 107 | + print('Message Body:') |
| 108 | + |
| 109 | + if fmt == CMQC.MQFMT_STRING: |
| 110 | + cp = 'utf8' # Assume a codepage, though we might want to use the ccsid to be more discriminating |
| 111 | + print(bytes.decode(msg[offset:], cp)) |
| 112 | + else: |
| 113 | + print(msg[offset:]) |
| 114 | + |
| 115 | + print() |
| 116 | + |
| 117 | + except mq.MQMIError as e: |
| 118 | + if e.reason == CMQC.MQRC_NO_MSG_AVAILABLE: |
| 119 | + print('No more messages.') |
| 120 | + ok = False |
| 121 | + else: |
| 122 | + raise |
| 123 | +q.close() |
| 124 | +qmgr.disconnect() |
0 commit comments