def test_arbitrary_message_with_mqcfil(self, value): """Test arbitrary message with MQCFIL.""" message = pymqi.CFH(Version=pymqi.CMQCFC.MQCFH_VERSION_1, Type=pymqi.CMQCFC.MQCFT_USER, ParameterCount=1).pack() message = message + pymqi.CFIL(Parameter=1, Values=value).pack() queue = pymqi.Queue( self.qmgr, self.queue_name, pymqi.CMQC.MQOO_INPUT_AS_Q_DEF + pymqi.CMQC.MQOO_OUTPUT) put_md = pymqi.MD(Format=pymqi.CMQC.MQFMT_PCF) queue.put(message, put_md) get_opts = pymqi.GMO(Options=pymqi.CMQC.MQGMO_NO_SYNCPOINT + pymqi.CMQC.MQGMO_FAIL_IF_QUIESCING, Version=pymqi.CMQC.MQGMO_VERSION_2, MatchOptions=pymqi.CMQC.MQMO_MATCH_CORREL_ID) get_md = pymqi.MD(MsgId=put_md.MsgId) # pylint: disable=no-member message = queue.get(None, get_md, get_opts) queue.close() message = pymqi.PCFExecute.unpack(message) self.assertTrue( isinstance(message[0][1], list), 'Returned value is not list: {}'.format(type(message[0][1]))) self.assertTrue( len(message[0][1]) == len(value), 'List length is different!') for item in message[0][1]: self.assertTrue(item in value, '{} value not in values list'.format(item)) value.remove(item)
def test_unpack_header(self): """Test unpack header.""" message = pymqi.CFH(Version=pymqi.CMQCFC.MQCFH_VERSION_1, Type=pymqi.CMQCFC.MQCFT_STATISTICS, Command=pymqi.CMQCFC.MQCMD_STATISTICS_Q, ParameterCount=1).pack() message += pymqi.CFST(Parameter=pymqi.CMQC.MQCA_Q_MGR_NAME, String=b'QM1').pack() queue = pymqi.Queue( self.qmgr, self.queue_name, pymqi.CMQC.MQOO_INPUT_AS_Q_DEF + pymqi.CMQC.MQOO_OUTPUT) put_md = pymqi.MD(Format=pymqi.CMQC.MQFMT_PCF) queue.put(message, put_md) get_opts = pymqi.GMO(Options=pymqi.CMQC.MQGMO_NO_SYNCPOINT + pymqi.CMQC.MQGMO_FAIL_IF_QUIESCING, Version=pymqi.CMQC.MQGMO_VERSION_2, MatchOptions=pymqi.CMQC.MQMO_MATCH_CORREL_ID) get_md = pymqi.MD(MsgId=put_md.MsgId) # pylint: disable=no-member message = queue.get(None, get_md, get_opts) queue.close() message, header = pymqi.PCFExecute.unpack(message) self.assertEqual(header.Command, pymqi.CMQCFC.MQCMD_STATISTICS_Q) # pylint: disable=no-member self.assertEqual(header.Type, pymqi.CMQCFC.MQCFT_STATISTICS) # pylint: disable=no-member self.assertEqual({ pymqi.CMQC.MQCA_Q_MGR_NAME: b'QM1\x00', }, message)
def test_mqcfgr_mqcfin64_mqcfil64(self): """Test arbitrary message with MQCFIL.""" message = pymqi.CFH(Version=pymqi.CMQCFC.MQCFH_VERSION_1, Type=pymqi.CMQCFC.MQCFT_USER, ParameterCount=4).pack() message += pymqi.CFST(Parameter=pymqi.CMQC.MQCA_Q_MGR_NAME, String=b'QM1').pack() # group1 message += pymqi.CFGR(Parameter=pymqi.CMQCFC.MQGACF_Q_STATISTICS_DATA, ParameterCount=3).pack() message += pymqi.CFST(Parameter=pymqi.CMQC.MQCA_Q_NAME, String=b'SYSTEM.ADMIN.COMMAND.QUEUE').pack() message += pymqi.CFIN64(Parameter=pymqi.CMQCFC.MQIAMO_Q_MIN_DEPTH, Value=10).pack() message += pymqi.CFIL64(Parameter=pymqi.CMQCFC.MQIAMO64_AVG_Q_TIME, Values=[1, 2, 3]).pack() # group2 message += pymqi.CFGR(Parameter=pymqi.CMQCFC.MQGACF_Q_STATISTICS_DATA, ParameterCount=3).pack() message += pymqi.CFST(Parameter=pymqi.CMQC.MQCA_Q_NAME, String=b'SYSTEM.ADMIN.COMMAND.QUEUE2').pack() message += pymqi.CFIN64(Parameter=pymqi.CMQCFC.MQIAMO_Q_MIN_DEPTH, Value=20).pack() message += pymqi.CFIL64(Parameter=pymqi.CMQCFC.MQIAMO64_AVG_Q_TIME, Values=[111, 222]).pack() message += pymqi.CFST(Parameter=pymqi.CMQCFC.MQCAMO_START_TIME, String=b'10.41.58').pack() queue = pymqi.Queue(self.qmgr, self.queue_name, pymqi.CMQC.MQOO_INPUT_AS_Q_DEF + pymqi.CMQC.MQOO_OUTPUT) put_md = pymqi.MD(Format=pymqi.CMQC.MQFMT_PCF) queue.put(message, put_md) get_opts = pymqi.GMO( Options=pymqi.CMQC.MQGMO_NO_SYNCPOINT + pymqi.CMQC.MQGMO_FAIL_IF_QUIESCING, Version=pymqi.CMQC.MQGMO_VERSION_2, MatchOptions=pymqi.CMQC.MQMO_MATCH_CORREL_ID) get_md = pymqi.MD(MsgId=put_md.MsgId) # pylint: disable=no-member message = queue.get(None, get_md, get_opts) queue.close() message, _ = pymqi.PCFExecute.unpack(message) self.assertEqual({ pymqi.CMQC.MQCA_Q_MGR_NAME: b'QM1\x00', pymqi.CMQCFC.MQCAMO_START_TIME: b'10.41.58', pymqi.CMQCFC.MQGACF_Q_STATISTICS_DATA: [ { pymqi.CMQC.MQCA_Q_NAME: b'SYSTEM.ADMIN.COMMAND.QUEUE\x00\x00', pymqi.CMQCFC.MQIAMO_Q_MIN_DEPTH: 10, pymqi.CMQCFC.MQIAMO64_AVG_Q_TIME: [1, 2, 3], }, { pymqi.CMQC.MQCA_Q_NAME: b'SYSTEM.ADMIN.COMMAND.QUEUE2\x00', pymqi.CMQCFC.MQIAMO_Q_MIN_DEPTH: 20, pymqi.CMQCFC.MQIAMO64_AVG_Q_TIME: [111, 222], }, ] }, message)
def get_message(self): # Message Descriptor request_md = pymqi.MD() # Get Message Options gmo = pymqi.GMO() gmo.Options = pymqi.CMQC.MQGMO_FAIL_IF_QUIESCING start = time.time() message = {} try: request_message = ET.fromstring( self.request_queue.get(None, request_md, gmo)) message['req_guid'] = request_message.find('Request_GUID').text message['filename'] = request_message.find('FileName').text message['bucket'] = request_message.find('BucketName').text message['data'] = request_message.find('FileContent').text end = time.time() logger.debug( "Retrieving message from queue took: {}".format(end - start)) except pymqi.MQMIError as e: message = None if e.comp == pymqi.CMQC.MQCC_FAILED and e.reason == pymqi.CMQC.MQRC_NO_MSG_AVAILABLE: # No messages, that's OK, we can ignore it. logger.debug('No messages in queue, retrying in 0.5 second...') time.sleep(0.5) else: raise return message, request_md
def browse_queue(): queue = pymqi.Queue(qmgr, args.browse_queue_name, pymqi.CMQC.MQOO_BROWSE) current_options = pymqi.GMO() current_options.Options = pymqi.CMQC.MQGMO_BROWSE_FIRST md = pymqi.MD() message = queue.get(None, md, current_options) print("Browsing Top Message Of " + str(args.browse_queue_name)) print(str(message))
def test_get_truncated_enough(self): """Test truncated with big buffer.""" self._put_message() gmo = pymqi.GMO() gmo.Options = pymqi.CMQC.MQGMO_ACCEPT_TRUNCATED_MSG md_get = pymqi.MD() message = self.queue.get(len(self.message), md_get, gmo) self.assertEqual(self.message, message)
def _get_message_from_ibm_queue(self, queue_name, session, uniquemessageid=None): """ It is Used to get the messages from IBM MQ queue. It takes the session instance to perform action.""" if uniquemessageid: queue = pymqi.Queue( session, str(queue_name), pymqi.CMQC.MQOO_FAIL_IF_QUIESCING | pymqi.CMQC.MQOO_INPUT_SHARED | pymqi.CMQC.MQOO_BROWSE) current_options = pymqi.GMO() current_options.Options = pymqi.CMQC.MQGMO_BROWSE_NEXT while True: try: md = pymqi.MD() message = queue.get(None, md, current_options) find = str(message).find(str(uniquemessageid)) if find != -1: break except pymqi.MQMIError as e: if e.comp == pymqi.CMQC.MQCC_FAILED and e.reason == pymqi.CMQC.MQRC_NO_MSG_AVAILABLE: raise AssertionError( "No Message With '{0}' Unique Message ID Found in '{1}' Queue" .format(uniquemessageid, queue_name)) queue.close() else: queue = pymqi.Queue( session, str(queue_name), pymqi.CMQC.MQOO_FAIL_IF_QUIESCING | pymqi.CMQC.MQOO_INPUT_SHARED | pymqi.CMQC.MQOO_BROWSE) current_options = pymqi.GMO() current_options.Options = pymqi.CMQC.MQGMO_BROWSE_FIRST try: md = pymqi.MD() message = queue.get(None, md, current_options) except pymqi.MQMIError as e: if e.comp == pymqi.CMQC.MQCC_FAILED and e.reason == pymqi.CMQC.MQRC_NO_MSG_AVAILABLE: raise AssertionError("'{}' queue is empty!".format( str(queue_name))) finally: queue.close() print("Message content: {}".format(message)) return message
def run(self): while True: # Put the request message. put_mqmd = pymqi.MD() # Set the MsgType to request. put_mqmd["MsgType"] = pymqi.CMQC.MQMT_REQUEST # Set up the ReplyTo QUeue/Queue Manager (Queue Manager is automatically # set by MQ). put_mqmd["ReplyToQ"] = replyto_queue_name put_mqmd["ReplyToQMgr"] = qm_name # Set up the put options - must do with NO_SYNCPOINT so that the request # message is committed immediately. put_opts = pymqi.PMO(Options=pymqi.CMQC.MQPMO_NO_SYNCPOINT + pymqi.CMQC.MQPMO_FAIL_IF_QUIESCING) # Create a random message. message = message_prefix + uuid.uuid4().hex self.req_queue.put(message, put_mqmd, put_opts) logging.info("Put request message. Message: [%s]" % message) # Set up message descriptor for get. get_mqmd = pymqi.MD() # Set the get CorrelId to the put MsgId (which was set by MQ on the put1). get_mqmd["CorrelId"] = put_mqmd["MsgId"] # Set up the get options. get_opts = pymqi.GMO(Options=pymqi.CMQC.MQGMO_NO_SYNCPOINT + pymqi.CMQC.MQGMO_FAIL_IF_QUIESCING + pymqi.CMQC.MQGMO_WAIT) # Version must be set to 2 to correlate. get_opts["Version"] = pymqi.CMQC.MQGMO_VERSION_2 # Tell MQ that we are matching on CorrelId. get_opts["MatchOptions"] = pymqi.CMQC.MQMO_MATCH_CORREL_ID # Set the wait timeout of half a second. get_opts["WaitInterval"] = 500 # Open the replyto queue and get response message, replyto_queue = pymqi.Queue(self.qm, replyto_queue_name, pymqi.CMQC.MQOO_INPUT_SHARED) response_message = replyto_queue.get(None, get_mqmd, get_opts) logging.info("Got response message [%s]" % response_message) time.sleep(1)
def test_put_unicode(self): self.queue.put(u'\u0442\u0435\u0441\u0442') # Unicode characters md = pymqi.MD() gmo = pymqi.GMO() gmo.Options = gmo.Options & ~pymqi.CMQC.MQGMO_CONVERT message = self.queue.get(None, md, gmo) self.assertEqual(message, b'\xd1\x82\xd0\xb5\xd1\x81\xd1\x82') self.assertEqual(md.Format, pymqi.CMQC.MQFMT_STRING) self.assertEqual(md.CodedCharSetId, 1208)
def test_put1_bytes(self): md = pymqi.MD() self.qmgr.put1(self.queue_name, b'\xd1\x82\xd0\xb5\xd1\x81\xd1\x82', md) # Non-ascii characters gmo = pymqi.GMO() gmo.Options = gmo.Options & ~pymqi.CMQC.MQGMO_CONVERT message = self.queue.get(None, md, gmo) self.assertEqual(message, b'\xd1\x82\xd0\xb5\xd1\x81\xd1\x82') self.assertEqual(md.Format, pymqi.CMQC.MQFMT_NONE)
def test_get_nontruncated_enough(self): """Test nontruncated with big enough buffer.""" md_put = self._put_message() gmo = pymqi.GMO() gmo.MatchOptions = pymqi.CMQC.MQMO_MATCH_MSG_ID md_get = pymqi.MD() md_get.MsgId = md_put.MsgId message = self.queue.get(len(self.message), md_get, gmo) self.assertEqual(self.message, message)
def getMessages(): logger.info('Attempting gets from Queue') # Message Descriptor md = pymqi.MD() # Get Message Options # MQGMO_NO_PROPERTIES indicates that JMS headers are to be stripped # off the message during the get. This can also be done by calling # .get_no_jms on the queue instead of .get gmo = pymqi.GMO() gmo.Options = pymqi.CMQC.MQGMO_WAIT | \ pymqi.CMQC.MQGMO_FAIL_IF_QUIESCING | \ pymqi.CMQC.MQGMO_NO_PROPERTIES gmo.WaitInterval = 5000 # 5 seconds keep_running = True while keep_running: try: # 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 # Wait up to to gmo.WaitInterval for a new message. # message = queue.get_no_jms(None, md, gmo) message = queue.get(None, md, gmo) # Process the message here.. msgObject = json.loads(message.decode()) logger.info('Have message from Queue') logger.info(msgObject) except pymqi.MQMIError as e: if e.comp == pymqi.CMQC.MQCC_FAILED and e.reason == pymqi.CMQC.MQRC_NO_MSG_AVAILABLE: # No messages, we should more on to next connection endpoint if there is one. logger.info('No more messages found on this connection') keep_running = False else: # Some other error condition. raise except (UnicodeDecodeError, ValueError) as e: logger.info('Message is not valid json') logger.info(e) logger.info(message) continue except KeyboardInterrupt: logger.info('Have received a keyboard interrupt') keep_running = False
def test_put1_unicode_with_ccsid_and_format(self): md = pymqi.MD(CodedCharSetId=1208, Format=pymqi.CMQC.MQFMT_STRING) self.qmgr.put1(self.queue_name, u'\u0442\u0435\u0441\u0442', md) # Unicode characters gmo = pymqi.GMO() gmo.Options = gmo.Options & ~pymqi.CMQC.MQGMO_CONVERT message = self.queue.get(None, md, gmo) self.assertEqual(message, b'\xd1\x82\xd0\xb5\xd1\x81\xd1\x82') self.assertEqual(md.Format, pymqi.CMQC.MQFMT_STRING) self.assertEqual(md.CodedCharSetId, 1208)
def pcf_run(qmgr, reply_queue_name, message): pcf = mqpcf.mqpcf() # Set up the queue for putting to the admin queue hAdmin = pcf.get_h_admin_queue(qmgr) # get the reply to queue - uses model queue if non specified hReply = pcf.get_h_reply_queue(qmgr, queue=reply_queue_name) #hReply = pcf.get_h_reply_queue(qmgr,queue=b'SYSTEM.MQSC.REPLY.QUEUE') #hReply = pcf.get_h_reply_queue(qmgr,queue=b'SYSTEM.MQEXPLORER.REPLY.MODEL') md = pcf.create_admin_MD(hReplyToQ=hReply) # issue the request hAdmin.put(message, md) # we now need to get the replies md = pymqi.MD() gmo = pymqi.GMO() gmo.Options = pymqi.CMQC.MQGMO_WAIT | pymqi.CMQC.MQGMO_FAIL_IF_QUIESCING | pymqi.CMQC.MQGMO_CONVERT gmo.WaitInterval = 1000 # 1 seconds output = [] try: # loop around looking for messages # Once a message has been got, get the rest of messages with the same msgid # and correlid # if no more messages, then need to clear these fields to get next available # messages # for i in range(100): while True: data = hReply.get(None, md, gmo) md.set(MsgId=b'') # clear this so we get rest of messages in group header, data = pcf.parse_data(buffer=data, strip="yes", debug=0) if (header["Reason"] != 0): mqpcf.eprint("Reason mqcode:", pcf.header["sReason"]) mqpcf.eprint("error return:", header) if header["Control"] == "LAST": md.set(MsgId=b'') md.set(CorrelId=b'') ret = data js = json.dumps(ret) output.append(js) # dumpData(data) except pymqi.MQMIError as e: if (e.reason) != 2033: print("exception :", e, e.comp, e.reason) hAdmin.close() hReply.close() return output
def test_pubsub_api_managed_non_durable_rfh2(self): topic_string = self.topic_string_template.format(type="API_RFH2", destination="MANAGED", durable="NON DURABLE") subname = self.subname_template.format(type="Api_rfh2", destination="Managed", durable="Non Durable") msg = self.msg_format(topic_string=topic_string) sub_desc = self.get_subscription_descriptor( subname, topic_string, pymqi.CMQC.MQSO_CREATE + pymqi.CMQC.MQSO_MANAGED) # register Subscription sub = self.create_api_subscription() self.sub_desc_list = [(sub, sub_desc, None)] sub.sub(sub_desc=sub_desc) # publish (put) put_mqmd = pymqi.md(Format=pymqi.CMQC.MQFMT_RF_HEADER_2, Encoding=273, CodedCharSetId=1208) put_opts = pymqi.pmo() put_rfh2 = pymqi.RFH2(StrucId=pymqi.CMQC.MQRFH_STRUC_ID, Version=pymqi.CMQC.MQRFH_VERSION_2, StrucLength=188, Encoding=273, CodedCharSetId=1208, Format=pymqi.CMQC.MQFMT_STRING, Flags=0, NameValueCCSID=1208) put_rfh2.add_folder( b"<psc><Command>RegSub</Command><Topic>$topictree/topiccat/topic</Topic><QMgrName>DebugQM</QMgrName><QName>PUBOUT</QName><RegOpt>PersAsPub</RegOpt></psc>" ) put_rfh2.add_folder( b"<testFolder><testVar>testValue</testVar></testFolder>") put_rfh2.add_folder(b"<mcd><Msd>xmlnsc</Msd></mcd>") self.pub_rfh2(msg, topic_string, put_mqmd, put_opts, [put_rfh2]) get_opts = pymqi.GMO(Version=pymqi.CMQC.MQGMO_VERSION_4, WaitInterval=15000, Options=pymqi.CMQC.MQGMO_NO_SYNCPOINT + \ pymqi.CMQC.MQGMO_FAIL_IF_QUIESCING + \ pymqi.CMQC.MQGMO_WAIT) get_rfh2_list = [] data = sub.get_rfh2(None, pymqi.md(Version=pymqi.CMQC.MQMD_VERSION_2), get_opts, get_rfh2_list) sub.close(sub_close_options=0, close_sub_queue=True) self.assertEqual(data, msg)
def awaitResponse(msgId, correlId): logger.info('Attempting get from Reply Queue') # Message Descriptor md = pymqi.MD() md.MsgId = msgId md.CorrelId = correlId # Get Message Options gmo = pymqi.GMO() gmo.Options = pymqi.CMQC.MQGMO_WAIT | \ pymqi.CMQC.MQGMO_FAIL_IF_QUIESCING | \ pymqi.CMQC.MQGMO_NO_PROPERTIES gmo.WaitInterval = 5000 # 5 seconds #gmo.MatchOptions = pymqi.CMQC.MQMO_MATCH_MSG_ID gmo.MatchOptions = pymqi.CMQC.MQMO_MATCH_CORREL_ID gmo.Version = pymqi.CMQC.MQGMO_VERSION_2 keep_running = True while keep_running: try: # Wait up to to gmo.WaitInterval for a new message. message = dynamic['queue'].get(None, md, gmo) # Process the message here.. msgObject = json.loads(message.decode()) logger.info('Have reply message from Queue') logger.info(msgObject) # Not expecting any more messages keep_running = False except pymqi.MQMIError as e: if e.comp == pymqi.CMQC.MQCC_FAILED and e.reason == pymqi.CMQC.MQRC_NO_MSG_AVAILABLE: # No messages, that's OK, we can ignore it. pass else: # Some other error condition. raise except (UnicodeDecodeError, ValueError) as e: logger.info('Message is not valid json') logger.info(e) logger.info(message) continue except KeyboardInterrupt: logger.info('Have received a keyboard interrupt') keep_running = False
def work_with_property(self, property_value, property_type): messageHandle_get = None queue_get = None queue_put = None try: value_length = self.get_value_length(property_type, property_value) cmho_put = pymqi.CMHO() messageHandle_put = pymqi.MessageHandle(self.qmgr, cmho_put) messageHandle_put.properties.set(self.msg_prop_name, property_value, value_length=value_length, property_type=property_type) pmo = pymqi.PMO(Version=pymqi.CMQC.MQPMO_CURRENT_VERSION) pmo.OriginalMsgHandle = messageHandle_put.msg_handle md_put = pymqi.MD(Version=pymqi.CMQC.MQMD_CURRENT_VERSION) queue_put = pymqi.Queue(self.qmgr, self.queue_name, pymqi.CMQC.MQOO_OUTPUT) queue_put.put(b'', md_put, pmo) queue_put.close() gmo = pymqi.GMO(Version=pymqi.CMQC.MQGMO_CURRENT_VERSION) gmo.Options = pymqi.CMQC.MQGMO_NO_WAIT | pymqi.CMQC.MQGMO_PROPERTIES_IN_HANDLE gmo.MatchOptions = pymqi.CMQC.MQMO_MATCH_MSG_ID cmho_get = pymqi.CMHO(Version=pymqi.CMQC.MQCMHO_CURRENT_VERSION) messageHandle_get = pymqi.MessageHandle(self.qmgr, cmho_get) gmo.MsgHandle = messageHandle_get.msg_handle md_get = pymqi.MD() md_get.MsgId = md_put.MsgId queue_get = pymqi.Queue(self.qmgr, self.queue_name, pymqi.CMQC.MQOO_INPUT_AS_Q_DEF) queue_get.get(None, md_get, gmo) finally: if queue_put: if queue_put.get_handle(): queue_put.close() if queue_get: if queue_get.get_handle(): queue_get.close() return messageHandle_get
def getMessage(queue, syncpoint=False, wait=True, waitInterval=60): try: gmo = pymqi.GMO(Options=CMQC.MQGMO_FAIL_IF_QUIESCING) gmo.Options |= wait and CMQC.MQGMO_WAIT or \ CMQC.MQGMO_NO_WAIT gmo.Options |= syncpoint and CMQC.MQGMO_SYNCPOINT or \ CMQC.MQGMO_NO_SYNCPOINT gmo.WaitInterval = waitInterval * 1000 return queue.get(None, None, gmo) except pymqi.MQMIError, e: if e.comp == CMQC.MQCC_FAILED and \ e.reason == CMQC.MQRC_NO_MSG_AVAILABLE: # no message available return None else: raise
def test_get_truncated(self): """Test truncated without buffer.""" self._put_message() gmo = pymqi.GMO() gmo.Options = pymqi.CMQC.MQGMO_ACCEPT_TRUNCATED_MSG md_get = pymqi.MD() try: self.queue.get(0, md_get, gmo) except pymqi.MQMIError as ex: self.assertEqual(ex.reason, pymqi.CMQC.MQRC_TRUNCATED_MSG_ACCEPTED) self.assertEqual(ex.message, b'') # pylint: disable=no-member self.assertEqual( ex.original_length, # pylint: disable=no-member len(self.message))
def test_put_string_with_ccsid_and_format(self): md = pymqi.MD( CodedCharSetId=1208, # coding: utf8 is set Format=pymqi.CMQC.MQFMT_STRING) self.queue.put('тест', md) # Cyrillic (non-ascii) characters gmo = pymqi.GMO() gmo.Options = gmo.Options & ~pymqi.CMQC.MQGMO_CONVERT message = self.queue.get(None, md, gmo) # In Python3 pymqi should put unicode string self.assertEqual(message, b'\xd1\x82\xd0\xb5\xd1\x81\xd1\x82') self.assertEqual(md.Format, pymqi.CMQC.MQFMT_STRING) self.assertEqual(md.CodedCharSetId, 1208)
def getMessages(): logger.info('Attempting gets from Subscription') subOptions = pymqi.CMQC.MQGMO_NO_SYNCPOINT + \ pymqi.CMQC.MQGMO_FAIL_IF_QUIESCING + \ pymqi.CMQC.MQGMO_WAIT + \ pymqi.CMQC.MQGMO_NO_PROPERTIES gmo = pymqi.GMO(Options=subOptions) gmo["WaitInterval"] = 30 * 1000 # Message Descriptor md = pymqi.MD() keep_running = True while keep_running: try: # 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 #message = subscription.get(None, pymqi.md(), gmo) message = subscription.get(None, md, gmo) # Process the message here.. msgObject = json.loads(message.decode()) logger.info('Have message from Queue') logger.info(msgObject) except pymqi.MQMIError as e: if e.comp == pymqi.CMQC.MQCC_FAILED and e.reason == pymqi.CMQC.MQRC_NO_MSG_AVAILABLE: # No messages, that's OK, we can ignore it. pass else: # Some other error condition. raise except (UnicodeDecodeError, ValueError) as e: logger.info('Message is not valid json') logger.info(e) logger.info(message) continue except KeyboardInterrupt: logger.info('Have received a keyboard interrupt') keep_running = False
def test_put_string(self): md = pymqi.MD() # file coding defined (utf-8) self.queue.put('тест', md) # Cyrillic (non-ascii) characters gmo = pymqi.GMO() gmo.Options = gmo.Options & ~pymqi.CMQC.MQGMO_CONVERT message = self.queue.get(None, md, gmo) self.assertEqual(message, b'\xd1\x82\xd0\xb5\xd1\x81\xd1\x82') # In Python3 pymqi should put unicode string if version_info.major >= 3: self.assertEqual(md.Format, pymqi.CMQC.MQFMT_STRING) self.assertEqual(md.CodedCharSetId, 1208) else: self.assertEqual(md.Format, pymqi.CMQC.MQFMT_NONE)
def test_get_nontruncated_short(self): """Test nontruncated with short buffer.""" md_put = self._put_message() gmo = pymqi.GMO() gmo.MatchOptions = pymqi.CMQC.MQMO_MATCH_MSG_ID md_get = pymqi.MD() md_get.MsgId = md_put.MsgId try: self.queue.get(self.buffer_length, md_get, gmo) except pymqi.MQMIError as ex: self.assertEqual(ex.reason, pymqi.CMQC.MQRC_TRUNCATED_MSG_FAILED) self.assertEqual( ex.original_length, # pylint: disable=no-member len(self.message))
def getMessages(): logger.info('Attempting gets from Queue') # Message Descriptor md = pymqi.MD() # Get Message Options gmo = pymqi.GMO() gmo.Options = pymqi.CMQC.MQGMO_WAIT | pymqi.CMQC.MQGMO_FAIL_IF_QUIESCING gmo.WaitInterval = 5000 # 5 seconds keep_running = True while keep_running: try: # 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 # Wait up to to gmo.WaitInterval for a new message. message = queue.get(None, md, gmo) # Process the message here.. msgObject = json.loads(message.decode()) logger.info('Have message from Queue') logger.info(msgObject) respondToRequest(md, msgObject) except pymqi.MQMIError as e: if e.comp == pymqi.CMQC.MQCC_FAILED and e.reason == pymqi.CMQC.MQRC_NO_MSG_AVAILABLE: # No messages, that's OK, we can ignore it. pass else: # Some other error condition. raise except (UnicodeDecodeError, ValueError) as e: logger.info('Message is not valid json') logger.info(e) logger.info(message) continue except KeyboardInterrupt: logger.info('Have received a keyboard interrupt') keep_running = False
def test_get_truncated_0(self): """Test truncated with zero buffer length.""" md_put = self._put_message() gmo = pymqi.GMO() gmo.Options = pymqi.CMQC.MQGMO_ACCEPT_TRUNCATED_MSG gmo.MatchOptions = pymqi.CMQC.MQMO_MATCH_MSG_ID md_get = pymqi.MD() md_get.MsgId = md_put.MsgId try: self.queue.get(0, md_get, gmo) except pymqi.MQMIError as ex: self.assertEqual(ex.reason, pymqi.CMQC.MQRC_TRUNCATED_MSG_ACCEPTED) self.assertEqual(ex.message, b'') # pylint: disable=no-member self.assertEqual( ex.original_length, # pylint: disable=no-member len(self.message))
def test_get_truncated_big_msg(self): """Test get nontruncated big message""" md_put = pymqi.MD() if version_info.major >= 3: self.queue.put(bytes(4097), md_put) else: self.queue.put(bytes(b'\0' * 4097), md_put) gmo = pymqi.GMO() gmo.Options = pymqi.CMQC.MQGMO_ACCEPT_TRUNCATED_MSG md_get = pymqi.MD() try: message = self.queue.get(None, md_get, gmo) except pymqi.MQMIError as ex: self.assertEqual(ex.reason, pymqi.CMQC.MQRC_TRUNCATED_MSG_ACCEPTED) self.assertEqual( ex.original_length, # pylint: disable=no-member 4097) self.assertEqual(len(ex.message), 0) self.assertEqual(md_put.PutDate, md_get.PutDate)
def run(self): # Request message descriptor, will be reset after processing each # request message. request_md = pymqi.MD() # Get Message Options gmo = pymqi.GMO() gmo.Options = pymqi.CMQC.MQGMO_WAIT | pymqi.CMQC.MQGMO_FAIL_IF_QUIESCING gmo.WaitInterval = 500 # Half a second queue = pymqi.Queue(self.qm, request_queue_name) keep_running = True while keep_running: try: # Wait up to to gmo.WaitInterval for a new message. request_message = queue.get(None, request_md, gmo) # Create a response message descriptor with the CorrelId # set to the value of MsgId of the original request message. response_md = pymqi.MD() response_md.CorrelId = request_md.MsgId response_message = 'Response to message %s' % request_message self.replyto_queue.put(response_message, response_md) # Reset the MsgId, CorrelId & GroupId so that we can reuse # the same 'md' object again. request_md.MsgId = pymqi.CMQC.MQMI_NONE request_md.CorrelId = pymqi.CMQC.MQCI_NONE request_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's OK, we can ignore it. pass else: # Some other error condition. raise
def get_message(qmgr): # Message Descriptor md = pymqi.MD() # Get Message Options gmo = pymqi.GMO() gmo.Options = CMQC.MQGMO_WAIT | CMQC.MQGMO_FAIL_IF_QUIESCING gmo.WaitInterval = 5000 # 5 seconds queue = pymqi.Queue(qmgr, cmdq_name) message = queue.get(None, md, gmo) queue.close() # print out message print message print " " print "#=========================================================#" print "| |" print "| Please check your queue Authorities to confirm scripts |" print "| were run successfully. |" print "| |" print "#=========================================================#" print " "
def workWithProp(self): messageHandle_get = None try: cmho_put = pymqi.CMHO() messageHandle_put = pymqi.MessageHandle(self.qmgr, cmho_put) messageHandle_put.properties.set(self.msg_prop_name, self.msg_prop_value) pmo = pymqi.PMO() pmo.OriginalMsgHandle = messageHandle_put.msg_handle md_put = pymqi.MD() queue_put = pymqi.Queue(self.qmgr, self.queue_name, pymqi.CMQC.MQOO_OUTPUT) queue_put.put(b'', md_put, pmo) queue_put.close() gmo = pymqi.GMO() gmo.Options = pymqi.CMQC.MQGMO_NO_WAIT | pymqi.CMQC.MQGMO_PROPERTIES_IN_HANDLE gmo.MatchOptions = pymqi.CMQC.MQMO_MATCH_MSG_ID cmho_get = pymqi.CMHO() messageHandle_get = pymqi.MessageHandle(self.qmgr, cmho_get) gmo.MsgHandle = messageHandle_get.msg_handle md_get = pymqi.MD() md_get.MsgId = md_put.MsgId queue_get = pymqi.Queue(self.qmgr, self.queue_name, pymqi.CMQC.MQOO_INPUT_AS_Q_DEF) queue_get.get(None, md_get, gmo) finally: if queue_put: if queue_put.get_handle(): queue_put.close() if queue_get: if queue_get.get_handle(): queue_get.close() return messageHandle_get
def create_get_opts(self): get_opts = pymqi.GMO( Options=pymqi.CMQC.MQGMO_NO_SYNCPOINT + pymqi.CMQC.MQGMO_FAIL_IF_QUIESCING + \ pymqi.CMQC.MQGMO_WAIT) get_opts["WaitInterval"] = 15000 return get_opts