/// <summary> /// Start the listen to the queue for incoming messages and /// notifiy the handlers as new messges arrive /// </summary> private void StartQueueListener() { //create a separate connection to the message queue System.Messaging.MessageQueue listenermq = new System.Messaging.MessageQueue(formatName); ((XmlMessageFormatter)listenermq.Formatter).TargetTypeNames = (string[])(supportedTypes.ToArray(typeof(System.String))); System.Messaging.Message message = null; queueListenerStarted = true; try { //listen to the queue continusly through loop while (queueListenerStarted == true) { System.Threading.Thread.Sleep(sleepTime); if (handler.GetInvocationList().Length > 0) { //this is a call that will block the thread if no //message is in the queue. message = listenermq.Receive(); SAF.MessageQueue.Message safMessage = new SAF.MessageQueue.Message(); safMessage.Label = message.Label; safMessage.Content = message.Body; //fire the event handler(safMessage, queueName); } } } finally { //close the connetion listenermq.Close(); } }
/// <summary> /// Retrieve the message from the MSMQ /// </summary> /// <returns>retrieved message object from the queue</returns> public SAF.MessageQueue.Message Retrieve() { //set type information queue can use to deserialize the message ((XmlMessageFormatter)mq.Formatter).TargetTypeNames = (string[])(supportedTypes.ToArray(typeof(System.String))); System.Messaging.Message message = mq.Receive(); SAF.MessageQueue.Message safMessage = new SAF.MessageQueue.Message(); safMessage.Label = message.Label; safMessage.Content = message.Body; return(safMessage); }
/// <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> /// Sends the message to the MSMQ /// </summary> /// <param name="m">message object</param> public void Send(SAF.MessageQueue.Message m) { //set type information queue can use to serialize the message ((XmlMessageFormatter)mq.Formatter).TargetTypeNames = (string[])(supportedTypes.ToArray(typeof(System.String))); mq.Send(m.Content, m.Label); }