def run(self): while True: # Put the request message. put_mqmd = pymqi.MD() # Set the MsgType to request. put_mqmd["MsgType"] = 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=CMQC.MQPMO_NO_SYNCPOINT + 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=CMQC.MQGMO_NO_SYNCPOINT + CMQC.MQGMO_FAIL_IF_QUIESCING + CMQC.MQGMO_WAIT) # Version must be set to 2 to correlate. get_opts["Version"] = CMQC.MQGMO_VERSION_2 # Tell MQ that we are matching on CorrelId. get_opts["MatchOptions"] = 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, 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 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 click.secho('Pushing message onto queue: {0}'.format(queue), dim=True) click.secho('Message (truncated): {0}'.format(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 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 mqqueue.close() qmgr.disconnect() click.secho('Message successfully pushed onto the queue.', fg='green')
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 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
host = '127.0.0.1' port = '1414' queue_name = 'TEST.1' message = 'Hello from Python!' property_name = 'Property_1' conn_info = '%s(%s)' % (host, port) user = '******' password = '******' qmgr = pymqi.connect(queue_manager, channel, conn_info, user, password) put_msg_h = pymqi.MessageHandle(qmgr) put_msg_h.properties.set(property_name, message) # Default type is CMQC.MQTYPE_STRING pmo = pymqi.PMO( Version=pymqi.CMQC.MQPMO_VERSION_3) # PMO v3 is required properties pmo.OriginalMsgHandle = put_msg_h.msg_handle put_md = pymqi.MD(Version=pymqi.CMQC.MQMD_CURRENT_VERSION) put_queue = pymqi.Queue(qmgr, queue_name) put_queue.put(b'', put_md, pmo) get_msg_h = pymqi.MessageHandle(qmgr) gmo = pymqi.GMO(Version=pymqi.CMQC.MQGMO_CURRENT_VERSION) gmo.Options = pymqi.CMQC.MQGMO_PROPERTIES_IN_HANDLE gmo.MsgHandle = get_msg_h.msg_handle get_md = pymqi.MD() get_queue = pymqi.Queue(qmgr, queue_name)
queue_manager = "QM01" channel = "SVRCONN.1" host = "192.168.1.135" port = "1434" queue_name = "TEST.1" message = "Hello from Python!" property_name = "Propertie_1" conn_info = "%s(%s)" % (host, port) qmgr = pymqi.connect(queue_manager, channel, conn_info) put_msg_h = pymqi.MessageHandle(qmgr) put_msg_h.properties.set(property_name, message) #default type is CMQC.MQTYPE_STRING pmo = pymqi.PMO( Version=pymqi.CMQC.MQPMO_VERSION_3) #PMO v3 is minimal for using propeties pmo.OriginalMsgHandle = put_msg_h.msg_handle put_md = pymqi.MD(Version=pymqi.CMQC.MQMD_CURRENT_VERSION) put_queue = pymqi.Queue(qmgr, queue_name) put_queue.put(b'', put_md, pmo) #getting message with propertie get_msg_h = pymqi.MessageHandle(qmgr) gmo = pymqi.GMO(Version=pymqi.CMQC.MQGMO_CURRENT_VERSION) gmo.Options = pymqi.CMQC.MQGMO_PROPERTIES_IN_HANDLE gmo.MsgHandle = get_msg_h.msg_handle get_md = pymqi.MD()