def test_put_rfh2_single(self): """Create and put a new rfh2 and use get to get it from the queue. Compare it against know correct message. """ try: put_mqmd = pymqi.md() put_mqmd["Format"] = CMQC.MQFMT_RF_HEADER_2 put_mqmd["Encoding"] = 273 put_mqmd["CodedCharSetId"] = 1208 put_opts = pymqi.pmo() put_rfh2 = pymqi.RFH2() put_rfh2["StrucId"] = CMQC.MQRFH_STRUC_ID put_rfh2["Version"] = CMQC.MQRFH_VERSION_2 put_rfh2["StrucLength"] = 188 put_rfh2["Encoding"] = 273 put_rfh2["CodedCharSetId"]= 1208 put_rfh2["Format"] = CMQC.MQFMT_STRING put_rfh2["Flags"] = 0 put_rfh2["NameValueCCSID"] = 1208 put_rfh2.add_folder("<psc><Command>RegSub</Command><Topic>$topictree/topiccat/topic</Topic><QMgrName>DebugQM</QMgrName><QName>PUBOUT</QName><RegOpt>PersAsPub</RegOpt></psc>") put_rfh2.add_folder("<testFolder><testVar>testValue</testVar></testFolder>") put_rfh2.add_folder("<mcd><Msd>xmlnsc</Msd></mcd>") put_msg = "<testData><testVar>testValue</testVar></testData>" self.put_queue.put_rfh2(put_msg, put_mqmd, put_opts, [put_rfh2]) get_mqmd = pymqi.md() get_opts = pymqi.gmo() get_msg = self.get_queue.get(None, get_mqmd, get_opts) self.assertEqual(get_msg, self.single_rfh2_message, "Message got from Queue does not match known correct RFH2 message.") except Exception as e: self.fail(e)
def test_get_rfh2_multiple(self): """Use get_rfh2 to get a known correct 3rd party message containing Multiples RFH2 headers. """ try: put_mqmd = pymqi.md() put_mqmd["Format"] = CMQC.MQFMT_RF_HEADER_2 put_mqmd["Encoding"] = 273 put_mqmd["CodedCharSetId"] = 1208 self.put_queue.put(self.multiple_rfh2_message, put_mqmd) get_mqmd = pymqi.md() get_opts = pymqi.gmo() rfh2_list = [] msg = self.get_queue.get_rfh2(None, get_mqmd, get_opts, rfh2_list) self.assertEqual(len(rfh2_list), 2, "Number of RFH2's incorrect. Should be 2. But is %i" % len(rfh2_list)) rfh2_1 = rfh2_list[0] self.assertEqual(len(rfh2_1.get()), 12, "Number of attributes incorrect. Should be 12? But is %s" % str(len(rfh2_1.get()))) self.assertEqual(rfh2_1["StrucId"], CMQC.MQRFH_STRUC_ID, "StrucId has incorrect value. Should be: %s But is: %s" % (CMQC.MQRFH_STRUC_ID, str(rfh2_1["StrucId"]))) self.assertEqual(rfh2_1["Version"], CMQC.MQRFH_VERSION_2, "Version has incorrect value. Should be: %i But is: %s" % (CMQC.MQRFH_VERSION_2, str(rfh2_1["Version"]))) self.assertEqual(rfh2_1["StrucLength"], 252, "StrucLength has incorrect value. Should be: %i But is: %s" % (284, str(rfh2_1["StrucLength"]))) self.assertEqual(rfh2_1["Encoding"], 273, "Encoding has incorrect value. Should be: %i But is: %s" % (273, str(rfh2_1["Encoding"]))) self.assertEqual(rfh2_1["CodedCharSetId"], 1208, "CodedCharSetId has incorrect value. Should be: %i But is: %s" % (1208, str(rfh2_1["CodedCharSetId"]))) self.assertEqual(rfh2_1["Format"], CMQC.MQFMT_RF_HEADER_2, "Format has incorrect value. Should be: %s But is: %s" % (CMQC.MQFMT_RF_HEADER_2, str(rfh2_1["Format"]))) self.assertEqual(rfh2_1["Flags"], 0, "Flags has incorrect value. Should be: %i But is: %s" % (0, str(rfh2_1["Flags"]))) self.assertEqual(rfh2_1["NameValueCCSID"], 1208, "NameValueCCSID has incorrect value. Should be: %i But is: %s" % (1208, str(rfh2_1["NameValueCCSID"]))) self.assertEqual(rfh2_1["pscLength"], 152, "pscLength has incorrect value. Should be: %i But is: %s" % (152, str(rfh2_1["pscLength"]))) self.assertEqual(rfh2_1["psc"], "<psc><Command>RegSub</Command><Topic>$topictree/topiccat/topic</Topic><QMgrName>DebugQM</QMgrName><QName>PUBOUT</QName><RegOpt>PersAsPub</RegOpt></psc> ", "psc has incorrect value. Should be: %s But is: %s" % ("<psc><Command>RegSub</Command><Topic>$topictree/topiccat/topic</Topic><QMgrName>DebugQM</QMgrName><QName>PUBOUT</QName><RegOpt>PersAsPub</RegOpt></psc> ", ">" + str(rfh2_1["psc"]) + "<")) self.assertEqual(rfh2_1["testFolderLength"], 56, "testFolderLength has incorrect value. Should be: %i But is: %s" % (56, str(rfh2_1["testFolderLength"]))) self.assertEqual(rfh2_1["testFolder"], "<testFolder><testVar>testValue</testVar></testFolder> ", "testFolder has incorrect value. Should be: %s But is: %s" % ("<testFolder><testVar>testValue</testVar></testFolder> ", str(rfh2_1["testFolder"]))) rfh2_2 = rfh2_list[1] self.assertEqual(len(rfh2_2.get()), 14, "Number of attributes incorrect. Should be 14? But is %s" % str(len(rfh2_2.get()))) self.assertEqual(rfh2_2["StrucId"], CMQC.MQRFH_STRUC_ID, "StrucId has incorrect value. Should be: %s But is: %s" % (CMQC.MQRFH_STRUC_ID, str(rfh2_2["StrucId"]))) self.assertEqual(rfh2_2["Version"], CMQC.MQRFH_VERSION_2, "Version has incorrect value. Should be: %i But is: %s" % (CMQC.MQRFH_VERSION_2, str(rfh2_2["Version"]))) self.assertEqual(rfh2_2["StrucLength"], 284, "StrucLength has incorrect value. Should be: %i But is: %s" % (284, str(rfh2_2["StrucLength"]))) self.assertEqual(rfh2_2["Encoding"], 273, "Encoding has incorrect value. Should be: %i But is: %s" % (273, str(rfh2_2["Encoding"]))) self.assertEqual(rfh2_2["CodedCharSetId"], 1208, "CodedCharSetId has incorrect value. Should be: %i But is: %s" % (1208, str(rfh2_2["CodedCharSetId"]))) self.assertEqual(rfh2_2["Format"], CMQC.MQFMT_STRING, "Format has incorrect value. Should be: %s But is: %s" % (CMQC.MQFMT_STRING, str(rfh2_2["Format"]))) self.assertEqual(rfh2_2["Flags"], 0, "Flags has incorrect value. Should be: %i But is: %s" % (0, str(rfh2_2["Flags"]))) self.assertEqual(rfh2_2["NameValueCCSID"], 1208, "NameValueCCSID has incorrect value. Should be: %i But is: %s" % (1208, str(rfh2_2["NameValueCCSID"]))) self.assertEqual(rfh2_2["pscLength"], 152, "pscLength has incorrect value. Should be: %i But is: %s" % (152, str(rfh2_2["pscLength"]))) self.assertEqual(rfh2_2["psc"], "<psc><Command>RegSub</Command><Topic>$topictree/topiccat/topic</Topic><QMgrName>DebugQM</QMgrName><QName>PUBOUT</QName><RegOpt>PersAsPub</RegOpt></psc> ", "psc has incorrect value. Should be: %s But is: %s" % ("<psc><Command>RegSub</Command><Topic>$topictree/topiccat/topic</Topic><QMgrName>DebugQM</QMgrName><QName>PUBOUT</QName><RegOpt>PersAsPub</RegOpt></psc> ", ">" + str(rfh2_2["psc"]) + "<")) self.assertEqual(rfh2_2["testFolderLength"], 56, "testFolderLength has incorrect value. Should be: %i But is: %s" % (56, str(rfh2_2["testFolderLength"]))) self.assertEqual(rfh2_2["testFolder"], "<testFolder><testVar>testValue</testVar></testFolder> ", "testFolder has incorrect value. Should be: %s But is: %s" % ("<testFolder><testVar>testValue</testVar></testFolder> ", str(rfh2_2["testFolder"]))) self.assertEqual(rfh2_2["mcdLength"], 28, "mcdLength has incorrect value. Should be: %i But is: %s" % (28, str(rfh2_2["mcdLength"]))) self.assertEqual(rfh2_2["mcd"], "<mcd><Msd>xmlnsc</Msd></mcd>", "mcd has incorrect value. Should be: %s But is: %s" % ("<mcd><Msd>xmlnsc</Msd></mcd>", str(rfh2_2["mcd"]))) self.assertEqual(msg, self.multiple_rfh2_message[rfh2_1["StrucLength"] + rfh2_2["StrucLength"]:], "Message Payloads do not match?") except Exception as e: self.fail(e)
def test_put_get_rfh2_multiple(self): """Create and put a new RFH2 message containing multiple RFH2 headers with put_rfh2 and use get_rfh2 to get it from the queue. Compare it against know correct message. """ try: put_mqmd = pymqi.md() put_mqmd["Format"] = CMQC.MQFMT_RF_HEADER_2 put_mqmd["Encoding"] = 273 put_mqmd["CodedCharSetId"] = 1208 put_opts = pymqi.pmo() put_rfh2_1 = pymqi.RFH2() put_rfh2_1["StrucId"] = CMQC.MQRFH_STRUC_ID put_rfh2_1["Version"] = CMQC.MQRFH_VERSION_2 put_rfh2_1["StrucLength"] = 188 put_rfh2_1["Encoding"] = 273 put_rfh2_1["CodedCharSetId"]= 1208 put_rfh2_1["Format"] = CMQC.MQFMT_RF_HEADER_2 put_rfh2_1["Flags"] = 0 put_rfh2_1["NameValueCCSID"] = 1208 put_rfh2_1.add_folder("<psc><Command>RegSub</Command><Topic>$topictree/topiccat/topic</Topic><QMgrName>DebugQM</QMgrName><QName>PUBOUT</QName><RegOpt>PersAsPub</RegOpt></psc>") put_rfh2_1.add_folder("<testFolder><testVar>testValue</testVar></testFolder>") put_rfh2_2 = pymqi.RFH2() put_rfh2_2["StrucId"] = CMQC.MQRFH_STRUC_ID put_rfh2_2["Version"] = CMQC.MQRFH_VERSION_2 put_rfh2_2["StrucLength"] = 188 put_rfh2_2["Encoding"] = 273 put_rfh2_2["CodedCharSetId"]= 1208 put_rfh2_2["Format"] = CMQC.MQFMT_STRING put_rfh2_2["Flags"] = 0 put_rfh2_2["NameValueCCSID"] = 1208 put_rfh2_2.add_folder("<psc><Command>RegSub</Command><Topic>$topictree/topiccat/topic</Topic><QMgrName>DebugQM</QMgrName><QName>PUBOUT</QName><RegOpt>PersAsPub</RegOpt></psc>") put_rfh2_2.add_folder("<testFolder><testVar>testValue</testVar></testFolder>") put_rfh2_2.add_folder("<mcd><Msd>xmlnsc</Msd></mcd>") put_rfh2_list = [put_rfh2_1, put_rfh2_2] put_msg = "<testData><testVar>testValue</testVar></testData>" self.put_queue.put_rfh2(put_msg, put_mqmd, put_opts, put_rfh2_list) get_mqmd = pymqi.md() get_opts = pymqi.gmo() get_rfh2_list = [] get_msg = self.get_queue.get_rfh2(None, get_mqmd, get_opts, get_rfh2_list) self.assertEqual(len(get_rfh2_list), len(put_rfh2_list), "Number of RFH2's incorrect. Should be %i. But is %i" % (len(get_rfh2_list), len(put_rfh2_list))) self.assertEqual(get_rfh2_list[0].get(), put_rfh2_list[0].get()), "Put and Get RFH2 Lists do not match." self.assertEqual(get_rfh2_list[1].get(), put_rfh2_list[1].get()), "Put and Get RFH2 Lists do not match." self.assertEqual(get_msg, put_msg, "Put and Get messages do not match.") except Exception as e: self.fail(e)
def browse_messages_in_queue(self, queue_name): queue = pymqi.Queue(self.connection, queue_name, CMQC.MQOO_BROWSE) message_descriptors = pymqi.md() get_message_options = pymqi.gmo() get_message_options.Options = CMQC.MQGMO_BROWSE_NEXT messages = [] while True: try: message_body = queue.get(None, message_descriptors, get_message_options) messages.append(Message(message_descriptors, message_body)) #These are required in order to move the cursor to the next entry. message_descriptors['MsgId'] = '' message_descriptors['CorrelId'] = '' except pymqi.MQMIError, e: if e.comp == CMQC.MQCC_FAILED and e.reason == CMQC.MQRC_NO_MSG_AVAILABLE: break else: raise
def run(self): while True: try: # logging.debug("before connect %s %s %s" % # (self.queue_manager_name, self.server_conn_chl, # self.socket)) file_pid = open( "/tmp/%s_current.pid" % (self.config_name.replace("://", "-") + "_" + str(self.thread_id)), "r").read().decode() logging.debug("%%%%%% this pid:" + str(self.getName()) + " File pid:" + str(file_pid)) if self.getName().strip() != file_pid.strip(): # logging.debug("$$$$ Stopping... this pid:" + # str(self.getName()) + " File pid:" + str(file_pid)) done = True sys.exit(1) else: pass # logging.debug("!!! NOT Stopping... this pid:" + # str(self.getName()) + " File pid:" + str(file_pid)) cd = pymqi.cd() #set the max message length to maximum cd["MaxMsgLength"] = 104857600 if self._qm is None: self._qm = pymqi.QueueManager(None) logging.debug("Connecting to " + str(self.queue_manager_name) + " using channel " + str(self.server_conn_chl) + " and address " + self.socket + ".") self._qm.connect_tcp_client(self.queue_manager_name, cd, self.server_conn_chl, self.socket, self.mq_user_name, self.mq_password) logging.debug("Successfully Connected to " + self.queue_manager_name + " using channel " + self.server_conn_chl + " and address " + self.socket + ".") else: if not self.persistent_connection: self._qm = pymqi.QueueManager(None) logging.debug("Connecting to " + str(self.queue_manager_name) + " using channel " + str(self.server_conn_chl) + " and address " + self.socket + ".") self._qm.connect_tcp_client(self.queue_manager_name, cd, self.server_conn_chl, self.socket, self.mq_user_name, self.mq_password) logging.debug("Successfully Connected to " + self.queue_manager_name + " using channel " + self.server_conn_chl + " and address " + self.socket + ".") else: if not self._qm._is_connected(): self._qm = pymqi.QueueManager(None) logging.debug("Connecting to " + str(self.queue_manager_name) + " using channel " + str(self.server_conn_chl) + " and address " + self.socket + ".") self._qm.connect_tcp_client( self.queue_manager_name, cd, self.server_conn_chl, self.socket, self.mq_user_name, self.mq_password) logging.debug("Successfully Connected to " + self.queue_manager_name + " using channel " + self.server_conn_chl + " and address " + self.socket + ".") queues = [] logging.debug("Queue name list: %s" % str(self.queue_name_list)) for queue_name in self.queue_name_list: try: queues.append((queue_name, pymqi.Queue(self._qm, queue_name, CMQC.MQOO_INPUT_SHARED))) # logging.debug("queue loop. queue name:" + # str(queue_name)) except Exception as ex: logging.error("Unable to open queue:" + str(queue_name) + " Exception: " + str(ex)) get_opts = pymqi.gmo(Options=CMQC.MQGMO_FAIL_IF_QUIESCING) # logging.debug("after get_opts") msg_desc = pymqi.md() # logging.debug("Start get") for (queue_name, queue_obj) in queues: # logging.debug("queue = %s " % queue_name) # logging.debug("Current tid:" + # str(self.getName()) + # " queue:" + queue_name) # logging.debug("Check pid: queue:" + queue_name) file_pid = str( open( "/tmp/%s_current.pid" % (self.config_name.replace("://", "-") + "_" + str(self.thread_id)), "r").read()) # logging.debug("%%%%%% this pid:" + str(self.getName()) + # " File pid:" + str(file_pid)) if self.getName().strip() != file_pid.strip(): # logging.debug("$$$$ Stopping... this pid:" + # str(self.getName()) + # " File pid:" + str(file_pid)) done = True sys.exit(1) else: pass # logging.debug("!!! NOT Stopping... this pid:" + # str(self.getName()) + " File pid:" + # str(file_pid)) done = False while not done: try: logging.debug("Before MQGET") msg_desc['MsgId'] = CMQC.MQMI_NONE msg_desc['CorrelId'] = CMQC.MQCI_NONE msg_data = queue_obj.get(None, msg_desc, get_opts) logging.debug("Got message. ") # MQMD:" + str(msg_desc.get()) + "MSG:" + msg_data) handle_output(self.splunk_host, self.queue_manager_name, queue_name, msg_data, msg_desc, False, **self.kw) logging.debug("Handled output") except pymqi.MQMIError as e: if e.reason == 2033: logging.debug("Done! 2033. No more messages!") done = True else: logging.error("MQ Exception occurred: %s " % (str(e))) done = True queue_obj.close() if not self.persistent_connection: self._qm.disconnect() except pymqi.MQMIError as e: if e.reason == 2033: pass else: logging.error("MQ Exception occurred: %s " % (str(e))) if self._qm is not None: if not self.persistent_connection \ and self._qm._is_connected(): self._qm.disconnect() except: # catch *all* exceptions e = sys.exc_info()[1] logging.error("Stopping. Exception in QueuePoller: %s" % str(e)) sys.exit(1) time.sleep(float(self.mqinput_interval))
def run(self): #logging.debug("in run") while True: try: #logging.debug("before connect %s %s %s" % (self.queue_manager_name, self.server_connection_channel, self.socket)) file_pid = str(open("/tmp/%s_current.pid" % self.config_name.replace("://", "-"), "r").read()) #logging.debug("%%%%%% this pid:" + str(self.getName()) + " File pid:" + str(file_pid)) if self.getName().strip() != file_pid.strip(): #logging.debug("$$$$ Stopping... this pid:" + str(self.getName()) + " File pid:" + str(file_pid)) done = True sys.exit(1) else: pass #logging.debug("!!! NOT Stopping... this pid:" + str(self.getName()) + " File pid:" + str(file_pid)) if self._qm is None: self._qm = pymqi.QueueManager(None) logging.debug("Connecting to " + str(self.queue_manager_name) + str(self.server_connection_channel)) self._qm.connectTCPClient(self.queue_manager_name, pymqi.cd(), str(self.server_connection_channel), self.socket) logging.debug("Successfully Connected to " + str(self.queue_manager_name) + str(self.server_connection_channel)) else: if not self.persistent_connection: self._qm = pymqi.QueueManager(None) logging.debug("Connecting to " + str(self.queue_manager_name) + str(self.server_connection_channel)) self._qm.connectTCPClient(self.queue_manager_name, pymqi.cd(), str(self.server_connection_channel), self.socket) logging.debug("Successfully Connected to " + str(self.queue_manager_name) + str(self.server_connection_channel)) else: if not self._qm._is_connected(): self._qm = pymqi.QueueManager(None) logging.debug("Connecting to " + str(self.queue_manager_name) + str(self.server_connection_channel)) self._qm.connectTCPClient(self.queue_manager_name, pymqi.cd(), str(self.server_connection_channel), self.socket) logging.debug("Successfully Connected to " + str(self.queue_manager_name) + str(self.server_connection_channel)) queues = [] logging.debug("Queue name list: %s" % str(self.queue_name_list)) for queue_name in self.queue_name_list: try: queues.append((queue_name, pymqi.Queue(self._qm, queue_name, CMQC.MQOO_INPUT_SHARED))) #logging.debug("queue loop. queue name:" + str(queue_name)) except Exception, ex: logging.error("Unable to open queue:" + str(queue_name) + " Exception: " + str(ex)) get_opts = pymqi.gmo(Options = CMQC.MQGMO_FAIL_IF_QUIESCING) #logging.debug("after get_opts") msg_desc = pymqi.md() #logging.debug("Start get") for (queue_name, queue_obj) in queues: #logging.debug("queue = %s " % queue_name) #logging.debug("Current tid:" + str(self.getName()) + " queue:" + queue_name) #logging.debug("Check pid: queue:" + queue_name) file_pid = str(open("/tmp/%s_current.pid" % self.config_name.replace("://", "-"), "r").read()) #logging.debug("%%%%%% this pid:" + str(self.getName()) + " File pid:" + str(file_pid)) if self.getName().strip() != file_pid.strip(): #open("/opt/esb/stop.txt", "a").write("$$$$ Stopping... this pid:" + str(self.getName()) + " File pid:" + str(file_pid)) #logging.debug("$$$$ Stopping... this pid:" + str(self.getName()) + " File pid:" + str(file_pid)) done = True sys.exit(1) else: pass #logging.debug("!!! NOT Stopping... this pid:" + str(self.getName()) + " File pid:" + str(file_pid)) done = False while not done: try: logging.debug("Before MQGET") msg_desc['MsgId'] = '' msg_desc['CorrelId'] = '' msg_data = queue_obj.get(None, msg_desc, get_opts) logging.debug("Got message. ")# MQMD:" + str(msg_desc.get()) + "MSG:" + msg_data) handle_output(self.splunk_host, self.queue_manager_name, queue_name, msg_data, msg_desc, False, **self.kw) logging.debug("Handled output") except pymqi.MQMIError, e: if e.reason == 2033: logging.debug("Done! 2033. No more messages!") done = True else: logging.error("MQ Exception occurred: %s " % (str(e))) done = True queue_obj.close() if not self.persistent_connection: self._qm.disconnect()