def getQueue(queueName, forInput): logger.info('Connecting to Queue') try: # Works with single call, but object Descriptor # provides other options # q = pymqi.Queue(qmgr, MQDetails[EnvStore.QUEUE_NAME]) q = pymqi.Queue(qmgr) od = pymqi.OD() od.ObjectName = queueName if (forInput): odOptions = pymqi.CMQC.MQOO_INPUT_AS_Q_DEF else: od.ObjectType = pymqi.CMQC.MQOT_Q odOptions = pymqi.CMQC.MQOO_OUTPUT q.open(od, odOptions) return q except pymqi.MQMIError as e: logger.error("Error getting queue") logger.error(e) return None
def publish(): # Late import to not require it for e2e import pymqi conn_info = "%s(%s)" % (common.HOST, common.PORT) qmgr = pymqi.connect(common.QUEUE_MANAGER, common.CHANNEL, conn_info, common.USERNAME, common.PASSWORD) queue = pymqi.Queue(qmgr, common.QUEUE) for i in range(10): try: message = 'Hello from Python! Message {}'.format(i) log.info("sending message: %s", message) queue.put(message.encode()) except Exception as e: log.info("exception publishing: %s", e) queue.close() qmgr.disconnect() return queue.close() qmgr.disconnect()
def put_message(self, message: str, queue_name: str) -> None: """ Put a message into a queue. *Args:*\n _message_ - COntent of messgae to put into queue;\n _queue_name_ - Name of the target queue;\n *Raises:*\n MQ Error: Error message according PyMQI. *Example:*\n | Put Message | 'Hello World!' | 'TEST.SERVICENAME.REQUEST' """ try: print('[PyMQI::put_message]:: Start with message=[', message, '], queue_name=[', queue_name, ']...') queue = pymqi.Queue(self.connection, queue_name) queue.put(message.encode("utf-8")) print('[PyMQI::put_message]:: Ended successfully.') except pymqi.MQMIError as err: raise Exception("[PyMQI::Put Message] Error:", str(err))
def purge_queue(self, queue_name: str) -> None: """ Purge all message from a queue. *Args:*\n _queue_name_ - Name of the queue to purge;\n *Raises:*\n MQ Error: Error message according PyMQI. *Example:*\n Purge Queue | 'TEST.SERVICENAME.REPLY' """ msg_cnt = 0 try: print('[PyMQI::purge_queue]:: Start with queue_name=[', queue_name, ']...') queue = pymqi.Queue(self.connection, queue_name) message = queue.get() while message is not None: msg_cnt += 1 #print('[PyMQI::purge_queue]:: Purged message [', msg_cnt, ']: [', message.decode("utf-8"),']') message = queue.get() print('[PyMQI::purge_queue]:: Ended successfully with [', msg_cnt, '] messages.') print('') except pymqi.MQMIError as err: str_err = str(err) if (str_err.find('MQRC_NO_MSG_AVAILABLE') >= 0): print('[PyMQI::purge_queue]:: Ended with [', msg_cnt, '] messages.') print('') else: raise Exception("[PyMQI::purge_queue] Error:", str(err))
def test_pubsub_api_provided_durable(self): topic_string = self.topic_string_template.format(type="API", destination="PROVIDED", durable="DURABLE") subname = self.subname_template.format(type="Api", destination="Provided", durable="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_DURABLE) queue_name = self.queue_name_template.format(type="API", durable="DURABLE") self.create_queue(queue_name) # create queue open_opts = pymqi.CMQC.MQOO_INPUT_AS_Q_DEF sub_queue = pymqi.Queue(self.qmgr, queue_name, open_opts) # register Subscription sub = self.create_api_subscription() self.sub_desc_list = [(sub, sub_desc, queue_name)] sub.sub(sub_desc=sub_desc, sub_queue=sub_queue) # publish (put) self.pub(msg, topic_string) get_opts = self.create_get_opts() data = sub.get(None, pymqi.md(), get_opts) sub.close(sub_close_options=0, close_sub_queue=True) self.assertEqual(data, msg)
def put_message(qmgr): out_message = '!## Process Completed ##!' queue = pymqi.Queue(qmgr, cmdq_name) queue.put(out_message) queue.close()
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)
import pymqi import datetime import boto3 import json #Connect with MQSeries queue_manager = "QUEUE_MANAGER_NAME" channel = "SVRCONN.1" host = "mq.meudominio.com" port = "1434" conn_info = "%s(%s)" % (host, port) qmgr = pymqi.QueueManager(None) qmgr.connectTCPClient(queue_manager, pymqi.cd(), channel, conn_info) getQ = pymqi.Queue(qmgr, 'QNAME') #Connect with Kinesis kinesis = boto3.client('kinesis') fieldspecs = [ ('card_number', 0, 16), ('tran_datetime', 16, 14), ('value', 30, 15), ('pdv_id', 45, 10), ('status', 56, 1), ] json_field = [] for field in fieldspecs:
def sniff(queue, store, directory): """ Sniff messages on a queue, non-destructively. Sniffs queues messages on a queue by opening the queue in a read only mode. Incoming messages will be dumped to the screen by default. If the --store flag is specified, messages will also be written to disk as they arrive. """ mqstate.validate(['host', 'port']) # check if a directory was set but store was not if directory and not store: click.secho('A directory was set to store messages but --store flag was not provided, ignoring...', bold=True, fg='yellow') # Prepare the destination directory if messages should also be saved if store: # Automatically generate a directory to save messages to if not directory: directory = safe_filename(mqstate.host + '_' + safe_filename(queue)) click.secho(f'Messages will be saved to directory \'{directory}\'', dim=True, fg='green') # check that the directory is ready for use absolute_path = os.path.abspath(directory) if not os.path.exists(absolute_path): click.secho(f'Creating {absolute_path} to save messages in...', dim=True) os.makedirs(absolute_path, mode=0o755) qmgr = pymqi.connect(mqstate.qm_name, mqstate.channel, mqstate.get_host(), mqstate.username, mqstate.password) # https://www.ibm.com/support/knowledgecenter/en/SSFKSJ_7.5.0/com.ibm.mq.ref.dev.doc/q096780_.htm # https://github.com/dsuch/pymqi/blob/master/code/examples/put_get_correl_id.py # https://github.com/dsuch/pymqi/blob/master/code/examples/get_wait_multiple_messages.py gmo = pymqi.GMO() gmo.Options = pymqi.CMQC.MQGMO_BROWSE_NEXT | pymqi.CMQC.MQGMO_WAIT | pymqi.CMQC.MQGMO_FAIL_IF_QUIESCING gmo.WaitInterval = 2 * 1000 # 5 seconds queue = pymqi.Queue(qmgr, str(queue), pymqi.CMQC.MQOO_BROWSE) request_md = pymqi.MD() # simple counter for statistics message_count = 0 click.secho('Waiting for messages to arrive...', dim=True) while True: try: # grab the message message = queue.get(None, request_md, gmo) message_count += 1 # Save the message if we need to we do this early as # messages could be reformatted to be printed to screen. if store: file_name = filename_from_attributes( mq_string(request_md.PutDate), mq_string(request_md.PutTime), '.' + hashlib.sha1(request_md.MsgId).hexdigest() + '.', request_md.MsgType, request_md.Expiry, mq_string(request_md.UserIdentifier), mq_string(request_md.Format), mq_string(request_md.PutApplName)) # try get a safe filename from all of that file_name = slugify(file_name) with open(os.path.join(absolute_path, file_name), 'wb') as f: f.write(message) click.secho(f'{message_count}: Wrote message to {file_name}', bold=True, fg='green') # check if we have a MQSTR message. If we don't, try and filter # non-printables. if request_md.Format.strip() not in ['MQSTR', '']: # remove non-printables and update the Format # column with (stripped) so that it is visible message = message.decode('ascii', errors='ignore') request_md.Format = mq_string(request_md.Format) + ' (stripped)' click.secho(f'Message #{message_count}', fg='green') table = get_table_handle([ 'Date', 'Time', 'MsgID', 'MsgType', 'Expiry', 'User', 'Format', 'App Name'], markdown=False) table.append_row([ mq_string(request_md.PutDate), mq_string(request_md.PutTime), request_md.MsgId.decode('ascii', errors='ignore'), request_md.MsgType, request_md.Expiry, mq_string(request_md.UserIdentifier), mq_string(request_md.Format), mq_string(request_md.PutApplName), ]) # Print a 'header' for the message click.secho(table.get_string()) # Print the message itself click.secho('' + '*' * 40 + ' BEGIN MESSAGE DATA ' + '*' * 40, dim=True) click.secho(message, bold=True) click.secho('*' * 41 + ' END MESSAGE DATA ' + '*' * 41 + '\n', dim=True) # reset the request descriptor so we can reuse it for the next message. request_md.MsgId = pymqi.CMQC.MQMI_NONE request_md.CorrelId = pymqi.CMQC.MQCI_NONE request_md.GroupId = pymqi.CMQC.MQGI_NONE request_md.Format = pymqi.CMQC.MQGI_NONE except pymqi.MQMIError as dme: # No messages, that's OK, we can ignore it. if dme.comp == pymqi.CMQC.MQCC_FAILED and dme.reason == pymqi.CMQC.MQRC_NO_MSG_AVAILABLE: continue # 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_GET_INHIBITED: click.secho('GET not allowed on queue with current credentials.', fg='red') break else: # Some other error condition. raise dme except KeyboardInterrupt as _: click.secho('Stopping...', fg='yellow') break click.secho(f'\nSniffed {message_count} message(s) in total.', dim=True) queue.close() qmgr.disconnect()
def fetchdata(): # === Local === # # RabbitMQ Variables user = os.getenv('RABBITMQ_USER') password = os.getenv('RABBITMQ_PASSWORD') host = os.getenv('RABBITMQ_HOST') port = os.getenv('RABBITMQ_PORT') vhost = os.getenv('RABBITMQ_VHOST') exchange = os.getenv('RABBITMQ_BPMEXCHANGE') routing_key = os.getenv('RABBITMQ_ROUTING_KEY') #user = "******" #password = "******" #host = "172.20.0.220" #port = 5671 #vhost = "isnr" #exchange ="bpm" qm = QueueManager(user, password, host, port, vhost, True) # TODO: Add feature for switch local and cloud app setting # === Cloud App === # # Load RabbitMQ from system variable # vcap_services_json = os.getenv('VCAP_SERVICES') # vcap_services = json.loads(vcap_services_json) # # Cannot get RabbitMQ settings # if 'p-rabbitmq' not in vcap_services and len(vcap_services['p-rabbitmq']) < 1: # sys.exit('Error: cannot get RabbitMQ settings!') # rabbitmq_uri = vcap_services['p-rabbitmq'][0]['credentials']['protocols']['amqp']['uri'] # initial queues init_queue(qm, exchange) # TODO: Split main function for oop # # IBMMQ Variables queue_manager = os.getenv('IBMQUEUE_MANAGER') channel = os.getenv("IBMCHANNEL") host = os.getenv("IBMHOST") port = os.getenv("IBMPORT") queue_name = os.getenv("IBMQUEUE_NAME") user = os.getenv('IBMUSER') password = os.getenv('IBMPASSWORD') #queue_manager = "TIABPM" #channel = "TAISNR.SVRCONN" #host = "10.2.67.76" #port = "1414" #queue_name = "TAISNR.LOCAL.QUEUE" #user = '******' #password = '******' #routing_key = 'bpm_rk' conn_info = "%s(%s)" % (host, port) pymqi.queue_manager = queue_manager pymqi.channel = channel pymqi.host = host pymqi.port = port pymqi.queue_name = queue_name pymqi.conn_info = conn_info #qmgr = pymqi.connect(queue_manager, channel, conn_info) #Connecting in client mode with username/password credentials keep_connect = True while keep_connect: try: qmgr = pymqi.connect(queue_manager, channel, conn_info, user, password) printmsg = str(datetime.datetime.now()) + " Connect success !! " keep_connect = False except Exception as pymqierror: printmsg = str(datetime.datetime.now() ) + " Connect fail !! " + str(pymqierror) print(printmsg) with open("Output.txt", "a") as text_file: print(f"{printmsg}", file=text_file) with open("Output1.txt", "a") as text_file: print(f"{printmsg}", file=text_file) time.sleep(10) #Connecting in client mode ibmqueue = pymqi.Queue(qmgr, queue_name) #How to put the message on a queue # try: # message = 'Hello from Python!' # ibmqueue.put(message) # except Exception as identifier: # print(str(identifier)) keep_running = True count = 0 while keep_running: try: #How to get the message off a queue msg_body = ibmqueue.get().decode('utf-16') # routing_key = '{}_rk'.format(api['name'].lower()) # qm.publish(exchange, routing_key, msg_body) printmsg = str( datetime.datetime.now()) + " success!! " + msg_body printmsg1 = str(datetime.datetime.now()) + " success!! " except Exception as pymqierror: printmsg = str( datetime.datetime.now()) + " fail!! " + str(pymqierror) printmsg1 = str( datetime.datetime.now()) + " fail!! " + str(pymqierror) if count > 10: keep_running = False keep_connect = True count = count + 1 print(printmsg) with open("Output.txt", "a") as text_file: print(f"{printmsg}", file=text_file) with open("Output1.txt", "a") as text_file: print(f"{printmsg1}", file=text_file) time.sleep(10) ibmqueue.close() qmgr.disconnect()
def put_msg(self, msg): self.queue = pymqi.Queue(self.qmgr, self.queue_name) self.queue.put(msg) self.queue.close()
port = "1434" conn_info = "%s(%s)" % (host, port) message = "Please reply to a dynamic queue, thanks." dynamic_queue_prefix = "MY.REPLIES.*" request_queue = "TEST.1" qmgr = pymqi.connect(queue_manager, channel, conn_info) # Dynamic queue's object descriptor. dyn_od = pymqi.OD() dyn_od.ObjectName = "SYSTEM.DEFAULT.MODEL.QUEUE" dyn_od.DynamicQName = dynamic_queue_prefix # Open the dynamic queue. dyn_input_open_options = pymqi.CMQC.MQOO_INPUT_EXCLUSIVE dyn_queue = pymqi.Queue(qmgr, dyn_od, dyn_input_open_options) dyn_queue_name = dyn_od.ObjectName.strip() # Prepare a Message Descriptor for the request message. md = pymqi.MD() md.ReplyToQ = dyn_queue_name # Send the message. queue = pymqi.Queue(qmgr, request_queue) queue.put(message, md) # Get and process the response here.. dyn_queue.close() queue.close() qmgr.disconnect()
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))
password) except pymqi.MQMIError as e: MQPCF.eprint("pymqi.connect(qm,channel,conname,userid,password)", args.qm, args.channel, args.conname, args.userid, "********") MQPCF.eprint("pymqi.MQMIError: ", e) raise mqpcf = MQPCF.mqpcf() # open the events queues od = pymqi.OD() od.ObjectName = args.queue.encode( ) # passed value was a string - needs to be b'' input_open_options = pymqi.CMQC.MQOO_INPUT_EXCLUSIVE input_queue = pymqi.Queue(qmgr, od, input_open_options) #mqpcf = MQPCF.MQPCF() md = pymqi.MD() gmo = pymqi.GMO() md = pymqi.MD() gmo.Options = pymqi.CMQC.MQGMO_WAIT | pymqi.CMQC.MQGMO_FAIL_IF_QUIESCING | pymqi.CMQC.MQGMO_CONVERT gmo.WaitInterval = args.wait * 1000 # 20 seconds start = '' try: for i in range(args.count): md = pymqi.MD() rfh = [] mqpcf.resetMD(md=md) # msg = input_queue.get_rfh2(None, md, gmo,rfh )
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()
def main(): queue_manager_name = "QM1" queue_name = "DEV.QUEUE.1" queue_manager = create_queue_manager() queue = pymqi.Queue(queue_manager, queue_name) queue.put("Hello from Python !")
def _add_queue(self, queue_name): queue = pymqi.Queue(self.manager, queue_name) self.queues[queue_name] = queue
exit_with_state(STATE_UNKNOWN) try: pcf = pymqi.PCFExecute(qmgr) stats = pcf.MQCMD_INQUIRE_Q_STATUS({CMQC.MQCA_Q_NAME: queueName}) except pymqi.MQMIError, e: print "UNKNOWN - Can not get status information for queue:%s, reason: %s" % ( queueName, e) exit_with_state(STATE_UNKNOWN) q_depth = stats[0][CMQC.MQIA_CURRENT_Q_DEPTH] oldest_msg_age = stats[0][CMQCFC.MQIACF_OLDEST_MSG_AGE] if oldest_msg_age == -1: #NOT available queue level monitoring is off # fall back to q inquire try: q = pymqi.Queue(qmgr, queueName, CMQC.MQOO_BROWSE | CMQC.MQOO_INQUIRE) except pymqi.MQMIError, e: print "UNKNOWN - unable to open %s, reason: %s" % (queueName, e) exit_with_state(STATE_UNKNOWN) try: q_depth = q.inquire(CMQC.MQIA_CURRENT_Q_DEPTH) except pymqi.MQMIError, e: print "UNKNOWN - unable to inquire %s depth" % (queueName) exit_with_state(STATE_UNKNOWN) if ((critical_sec) and not (critical_sec == -1) and (oldest_msg_age > critical_sec)) or (((oldest_msg_age == -1) or (critical_sec == -1)) and (critical_depth) and (q_depth >= critical_depth)): exit_state = STATE_CRITICAL elif ((warning_sec) and not (warning_sec == -1) and
# See discussion and more examples at http://packages.python.org/pymqi/examples.html # or in doc/sphinx/examples.rst in the source distribution. import pymqi queue_manager = "QM01" channel = "SVRCONN.1" host = "192.168.1.135" port = "1434" queue_name = "TEST.1" message = "Here's a reply" conn_info = "%s(%s)" % (host, port) qmgr = pymqi.connect(queue_manager, channel, conn_info) md = pymqi.MD() queue = pymqi.Queue(qmgr, queue_name) message = queue.get(None, md) reply_to_queue_name = md.ReplyToQ.strip() reply_to_queue = pymqi.Queue(qmgr, reply_to_queue_name) reply_to_queue.put(message) queue.close() qmgr.disconnect()
try: qmgr = pymqi.connect(queue_manager, channel, conn_info) except Exception as e: logging.debug("Exception occurred", exc_info=True) sys.exit(1) try: with open(filename, "rb") as b: bo = b.read() except Exception as e: logging.debug("Exception occurred", exc_info=True) sys.exit(1) try: q = pymqi.Queue(qmgr, "PYMQI.TEST.1") q.put(bo) qmgr.commit() logging.debug("Message sent and comitted") qmgr.disconnect() except Exception as e: logging.debug("Exception occurred", exc_info=True) logging.debug("Unable to send message, exception") qmgr.disconnect() sys.exit(1) # No logging option (stack trace to console): # import traceback # except: # traceback.print_exc()
def dump(queue, limit): """ Dump messages from a queue, non-destructively. """ click.secho(f'Dumping a maximum of {limit} messages from {queue}...', dim=True) click.secho('Only printing ASCII characters.', dim=True) qmgr = pymqi.connect(mqstate.qm_name, mqstate.channel, mqstate.get_host(), mqstate.username, mqstate.password) # https://www.ibm.com/support/knowledgecenter/en/SSFKSJ_7.5.0/com.ibm.mq.ref.dev.doc/q096780_.htm # https://github.com/dsuch/pymqi/blob/master/code/examples/put_get_correl_id.py gmo = pymqi.GMO() gmo.Options = pymqi.CMQC.MQGMO_BROWSE_NEXT queue = pymqi.Queue(qmgr, str(queue), pymqi.CMQC.MQOO_BROWSE) message_count = 0 while message_count < limit: try: request_md = pymqi.MD() message = queue.get(None, request_md, gmo) # check if we have a MQSTR message. if request_md.Format.strip() not in ['MQSTR', '']: # remove non-printables and update the Format # column with (stripped) so that it is visible message = message.decode('ascii', errors='ignore') request_md.Format = mq_string(request_md.Format) + ' (stripped)' table = get_table_handle(['Date', 'Time', 'MsgID', 'MsgType', 'Expiry', 'User', 'Format', 'App Name'], markdown=False) table.append_row([ mq_string(request_md.PutDate), mq_string(request_md.PutTime), request_md.MsgId.decode('ascii', errors='ignore'), request_md.MsgType, request_md.Expiry, mq_string(request_md.UserIdentifier), mq_string(request_md.Format), mq_string(request_md.PutApplName), ]) # Print a 'header' for the message click.secho(table.get_string()) # Print the message itself click.secho('\n' + '*' * 40 + ' BEGIN MESSAGE DATA ' + '*' * 40, dim=True) click.secho(message, bold=True) click.secho('*' * 41 + ' END MESSAGE DATA ' + '*' * 41 + '\n', dim=True) except pymqi.MQMIError as dme: if dme.comp == pymqi.CMQC.MQCC_FAILED and dme.reason == pymqi.CMQC.MQRC_NO_MSG_AVAILABLE: click.secho('Dump complete. No more messages on the queue.', fg='yellow') break # 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_GET_INHIBITED: click.secho('GET not allowed on queue with current access.', fg='red') break else: raise dme message_count += 1 click.secho('') click.secho(f'\nGot {message_count} message(s) in total.', dim=True) queue.close() qmgr.disconnect()
def connectmq_put(recv_mq, str1): queue = pymqi.Queue(qmgr, recv_mq["recv_queue"]) queue.put(str1) queue.close()
def save(queue, limit, directory): """ Save messages from a queue to a file, non-destructively. """ mqstate.validate(['host', 'port', 'channel']) # Automatically generate a directory to save messages to if not directory: directory = safe_filename(mqstate.host + '_' + safe_filename(queue)) click.secho(f'Saving messages to \'{directory}\'...', dim=True, fg='green') # check that the directory is ready for use absolute_path = os.path.abspath(directory) if not os.path.exists(absolute_path): click.secho(f'Creating {absolute_path} to save messages in...', dim=True) os.makedirs(absolute_path, mode=0o755) click.secho(f'Saving a maximum of {limit} messages from {queue}...', dim=True) qmgr = pymqi.connect(mqstate.qm_name, mqstate.channel, mqstate.get_host(), mqstate.username, mqstate.password) # https://www.ibm.com/support/knowledgecenter/en/SSFKSJ_7.5.0/com.ibm.mq.ref.dev.doc/q096780_.htm # https://github.com/dsuch/pymqi/blob/master/code/examples/put_get_correl_id.py gmo = pymqi.GMO() gmo.Options = pymqi.CMQC.MQGMO_BROWSE_NEXT queue = pymqi.Queue(qmgr, str(queue), pymqi.CMQC.MQOO_BROWSE) message_count = 0 while message_count < limit: try: request_md = pymqi.MD() message = queue.get(None, request_md, gmo) file_name = filename_from_attributes( mq_string(request_md.PutDate), mq_string(request_md.PutTime), '.' + hashlib.sha1(request_md.MsgId).hexdigest() + '.', request_md.MsgType, request_md.Expiry, mq_string(request_md.UserIdentifier), mq_string(request_md.Format), mq_string(request_md.PutApplName)) # try get a safe filename from all of that file_name = slugify(file_name) with open(os.path.join(absolute_path, file_name), 'wb') as f: f.write(message) click.secho(f'{message_count}: Wrote message to {file_name}', bold=True, fg='green') except pymqi.MQMIError as dme: if dme.comp == pymqi.CMQC.MQCC_FAILED and dme.reason == pymqi.CMQC.MQRC_NO_MSG_AVAILABLE: click.secho('Dump complete. No more messages on the queue.', fg='yellow') break # 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_GET_INHIBITED: click.secho('GET not allowed on queue with current access.', fg='red') break else: raise dme message_count += 1 click.secho(f'Saved {message_count} message(s) in total.', bold=True) queue.close() qmgr.disconnect()
def connectmq_get(recv_mq): queue = pymqi.Queue(qmgr, recv_mq["recv_queue"]) str1 = queue.get() queue.close() #qmgr.disconnect() return str1
def put_queue(): queue = pymqi.Queue(qmgr, args.put_queue_name) queue.put(args.put_message) print("Putting message onto " + str(args.put_queue_name)) queue.close()
def get_message_in_queue(self, queue_name): queue = pymqi.Queue(self.connection, queue_name) message = queue.get() queue.close() return message
# See discussion and more examples at http://packages.python.org/pymqi/examples.html # or in doc/sphinx/examples.rst in the source distribution. import pymqi import CMQC queue_manager = "QM01" channel = "SVRCONN.1" host = "192.168.1.135" port = "1434" queue_name = "TEST.1" message = "Hello from Python!" alternate_user_id = "myuser" conn_info = "%s(%s)" % (host, port) qmgr = pymqi.connect(queue_manager, channel, conn_info) od = pymqi.OD() od.ObjectName = queue_name od.AlternateUserId = alternate_user_id queue = pymqi.Queue(qmgr) queue.open(od, CMQC.MQOO_OUTPUT | CMQC.MQOO_ALTERNATE_USER_AUTHORITY) queue.put(message) queue.close() qmgr.disconnect()
def put_message_in_queue(self, queue_name, message): queue = pymqi.Queue(self.connection, queue_name) queue.put(message) queue.close()
for queueName in queuesList: prefix = '' + queueName + '' queue_type = pymqi.CMQC.MQQT_LOCAL args = { pymqi.CMQC.MQCA_Q_NAME: prefix, pymqi.CMQC.MQIA_Q_TYPE: queue_type } pcf = pymqi.PCFExecute(qmgr) try: response = pcf.MQCMD_INQUIRE_Q(args) except pymqi.MQMIError, e: if e.comp == pymqi.CMQC.MQCC_FAILED and e.reason == pymqi.CMQC.MQRC_UNKNOWN_OBJECT_NAME: logging.error("Can't find queue %s." % queueName) else: raise else: for queue_info in response: queue_name = queue_info[pymqi.CMQC.MQCA_Q_NAME] queue_name = queue_name.replace(' ', '') logging.info('Found queue %s' % (queue_name)) queue = pymqi.Queue(qmgr, queue_name) try: while queue.get(): queue.get() except pymqi.MQMIError, e: logging.info("Queue '%s' is empty." % queue_name) qmgr.disconnect() break
host = "192.168.1.135" port = "1434" queue_name = "TEST.1" conn_info = "%s(%s)" % (host, port) ssl_cipher_spec = "TLS_RSA_WITH_AES_256_CBC_SHA" key_repo_location = "/var/mqm/ssl-db/client/KeyringClient" message = "Hello from Python!" cd = pymqi.CD() cd.ChannelName = channel cd.ConnectionName = conn_info cd.ChannelType = pymqi.CMQC.MQCHT_CLNTCONN cd.TransportType = pymqi.CMQC.MQXPT_TCP cd.SSLCipherSpec = ssl_cipher_spec sco = pymqi.SCO() sco.KeyRepository = key_repo_location qmgr = pymqi.QueueManager(None) qmgr.connect_with_options(queue_manager, cd, sco) put_queue = pymqi.Queue(qmgr, queue_name) put_queue.put(message) get_queue = pymqi.Queue(qmgr, queue_name) logging.info("Here's the message again: [%s]" % get_queue.get()) put_queue.close() get_queue.close() qmgr.disconnect()