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 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_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 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_get_nontruncated_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) md_get = pymqi.MD() message = self.queue.get(None, md_get) self.assertEqual(len(message), 4097) self.assertEqual(md_put.PutDate, md_get.PutDate)
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 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 putMessage(): logger.info('Attempting put to Queue') try: # queue.put(json.dumps(msgObject).encode()) # queue.put(json.dumps(msgObject)) # Prepare a Message Descriptor for the request message. logger.info('Dynamic Queue Name is ') logger.info(dynamic['name']) md = pymqi.MD() md.ReplyToQ = dynamic['name'] md.MsgType = pymqi.CMQC.MQMT_REQUEST md.Format = pymqi.CMQC.MQFMT_STRING # Send the message. #queue.put(str(json.dumps(msgObject)), md) queue.put(EnvStore.stringForVersion(json.dumps(msgObject)), md) # queue.put(str(json.dumps(msgObject)), md) # queue.put("Hello") logger.info("Put message successful") #logger.info(md.CorrelID) return md.MsgId, md.CorrelId # return md.CorrelId except pymqi.MQMIError as e: logger.error("Error in put to queue") logger.error(e)
def test_get_nontruncated(self): """Test nontruncated without buffer.""" self._put_message() md_get = pymqi.MD() message = self.queue.get(None, md_get) self.assertEqual(self.message, message)
def test_get_nontruncated_enough(self): """Test nontruncated with big enough buffer.""" md_put = self._put_message() md_get = pymqi.MD() message = self.queue.get(len(self.message), md_get) self.assertEqual(self.message, message)
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 pop(queue, save_to, skip_confirmation): """ Pop a message off the queue. """ if not skip_confirmation: click.secho( 'WARNING: This action will REMOVE the message from the selected queue!\n' + 'Consider the --save-to flag to save the message you are about to pop.', fg='yellow') if not click.confirm('Are you sure?'): click.secho('Did not receive confirmation, bailing...') return qmgr = pymqi.connect(mqstate.qm_name, mqstate.channel, mqstate.get_host(), mqstate.username, mqstate.password) try: queue = pymqi.Queue(qmgr, str(queue)) request_md = pymqi.MD() message = queue.get(None, request_md) except pymqi.MQMIError as dme: if dme.comp == pymqi.CMQC.MQCC_FAILED and dme.reason == pymqi.CMQC.MQRC_NO_MSG_AVAILABLE: click.secho('No messages to pop from the queue.', fg='yellow') return else: raise dme t = get_table_handle( ['Date', 'Time', 'User', 'Format', 'App Name', 'Data'], markdown=False) t.append_row([ mq_string(request_md.PutDate), mq_string(request_md.PutTime), mq_string(request_md.UserIdentifier), mq_string(request_md.Format), mq_string(request_md.PutApplName), mq_string(message), ]) click.secho('') click.secho(t.get_string()) # save to file if we got a file argument if save_to: save_to.write(message) click.secho(f'\nSaved message data to file: {save_to.name}', fg='green') queue.close() qmgr.disconnect()
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 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 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_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 push(queue, source_file, source_string): """ Push a message onto the queue. """ if source_file is None and source_string is None: click.secho('Please provide either a source file or a source string.', fg='red') return if source_file and source_string: click.secho( 'Both a source file and string was specified. Only one is allowed.', fg='red') return if source_file: message = source_file.read() else: message = source_string.encode() click.secho(f'Pushing message onto queue: {queue}', dim=True) click.secho(f'Message (truncated): {message[:150]}', dim=True) qmgr = pymqi.connect(mqstate.qm_name, mqstate.channel, mqstate.get_host(), mqstate.username, mqstate.password) try: put_mqmd = pymqi.MD() put_mqmd.Format = pymqi.CMQC.MQFMT_STRING # https://github.com/dsuch/pymqi/blob/master/code/examples/put_get_correl_id.py#L69-L71 put_opts = pymqi.PMO(Options=pymqi.CMQC.MQPMO_NO_SYNCPOINT + pymqi.CMQC.MQPMO_FAIL_IF_QUIESCING) mqqueue = pymqi.Queue(qmgr, str(queue)) mqqueue.put(message, put_mqmd, put_opts) except pymqi.MQMIError as dme: # if we are not allowed to GET on this queue, mention that and quit if dme.comp == pymqi.CMQ.MQCC_FAILED and dme.reason == pymqi.CMQC.MQRC_PUT_INHIBITED: click.secho('PUT not allowed on queue with current credentials.', fg='red') return else: raise dme mqqueue.close() qmgr.disconnect() click.secho('Message successfully pushed onto the queue.', fg='green')
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 test_get_nontruncated_short(self): """Test nontruncated with short buffer.""" md_put = self._put_message() md_get = pymqi.MD() try: self.queue.get(self.buffer_length, md_get) 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 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 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 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 publishMessage(): logger.info('Attempting publish to Topic') try: md = pymqi.MD() md.Format = pymqi.CMQC.MQFMT_STRING # queue.put(json.dumps(msgObject).encode()) # queue.put(json.dumps(msgObject)) # topic.pub(str(json.dumps(msgObject))) topic.pub(EnvStore.stringForVersion(json.dumps(msgObject)), md) logger.info("Publish message successful") except pymqi.MQMIError as e: logger.error("Error in publish to topic") logger.error(e)
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 create_admin_MD(self,hReplyToQ=None): """ Create an MQ for reply to queue, and fillin the replyto queue """ md = pymqi.MD() # create an MD if hReplyToQ is None: raise ValueError("You must specify the hReplOD in getAdminMD, to provide the reply to queue") qname = hReplyToQ.inquire(pymqi.CMQC.MQCA_Q_NAME) # and fill in the fields md.ReplyToQ = qname md.MsgType = pymqi.CMQC.MQMT_REQUEST md.Feedback = pymqi.CMQC.MQFB_NONE md.Format = pymqi.CMQC.MQFMT_ADMIN return md
def create_admin_MD_CMD(self,replyToQ=None): """ Create an MQ for reply to queue, and fillin the replyto queue """ md = pymqi.MD() # create an MD if replyToQ is None: raise ValueError("You must specify the replyToQ") # and fill in the fields md.ReplyToQ = replyToQ md.MsgType = pymqi.CMQC.MQMT_REQUEST md.Feedback = pymqi.CMQC.MQFB_NONE md.Format = pymqi.CMQC.MQFMT_STRING return md
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))