/// <summary> /// Start the listen to the queue for incoming messages and /// notifiy the handlers as new messges arrive /// </summary> public void StartQueueListener() { //create a separate connection to the message queue queueListenerStarted = true; MQQueueManager listenermqm = (MQQueueManager)queueSession.AccessQueueManager(queueManager); MQQueue listenerqueue = (MQQueue)mqm.AccessQueue(QueueName, (int)MQ.MQOO_INPUT_AS_Q_DEF + (int)MQ.MQOO_OUTPUT, "", "", ""); listenerqueue.Open(); try { MQMessage message = (MQMessage)queueSession.AccessMessage(); MQGetMessageOptions messageOption = (MQGetMessageOptions)queueSession.AccessGetMessageOptions(); while (queueListenerStarted == true) { System.Threading.Thread.Sleep(sleepTime); if (handler.GetInvocationList().Length > 0) { try { //GET will raise an exception if no message is in the queue. //we want to keep listening despite of the exception, see exception block //for detail listenerqueue.Get(message, messageOption, System.Reflection.Missing.Value); SAF.MessageQueue.Message safMessage = new SAF.MessageQueue.Message(); safMessage.Label = message.MessageId; safMessage.Content = message.ReadString(message.MessageLength); //fire the event handler(safMessage, QueueName); } catch (System.Runtime.InteropServices.COMException ex) { //-2147467259 represents the error code for retrieving //message from an empty queue. do nothing if gotting this error code. if (ex.ErrorCode != -2147467259) { throw ex; } } } } } finally { //close the connetion listenerqueue.Close(); listenermqm.Disconnect(); } }
/// <summary> /// open the connection to the message queue /// </summary> public void Open() { mqm = (MQQueueManager)queueSession.AccessQueueManager(queueManager); queue = (MQQueue)mqm.AccessQueue(QueueName, (int)MQ.MQOO_INPUT_AS_Q_DEF + (int)MQ.MQOO_OUTPUT, "", "", ""); queue.Open(); }