import pymqi

# qmgr = pymqi.connect('QM.1', 'SVRCONN.CHANNEL.1', '192.168.1.121(1434)')
# getq = pymqi.Queue(qmgr, 'VIMS.LSP_ASN.BMW')
# qmgr = pymqi.connect('VIMSRRI10', 'SYSTEM.DEF.SVRCONN', '172.18.21.5(1415)')

queue_manager = 'VIMSRRI10'
channel = 'SYSTEM.DEF.SVRCONN'
host = '172.18.21.5'
port = '1415'
queue_name = 'VIMS.LSP_ASN.BMW'
conn_info = '%s(%s)' % (host, port)

# 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

qmgr = pymqi.connect(queue_manager, channel, conn_info)
pcf = pymqi.PCFExecute(qmgr)

queue = pymqi.Queue(qmgr, queue_name, pymqi.CMQC.MQOO_INPUT_SHARED)
# message = queue.get(None, md, gmo)
# print('Here is the message:', message)

keep_running = True

while keep_running:
	try:
# Wait up to to gmo.WaitInterval for a new message.
		message = queue.get(None, md, gmo)

# Process the message here..
		print('Here is the message:', message)
		
# 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

	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
	
queue.close()
qmgr.disconnect()
	