Click here to Skip to main content
15,891,473 members
Articles / Programming Languages / C#

Message queuing: A message notification system and a brief introduction

Rate me:
Please Sign up or sign in to vote.
2.20/5 (3 votes)
21 Aug 2007CPOL3 min read 24K   177   14  
A simple application which can be a used for furture expansion and also an introcudtion to MSMQ.
using System;
using System.Collections;
using System.Messaging;
using System.Windows.Forms;
using Message = System.Messaging.Message;

namespace MessageNotification
{
	/// <summary>
	/// Summary description for MessageDealer.
	/// </summary>
	public class MessageDealer
	{
		public MessageDealer()
		{
			//
			// TODO: Add constructor logic here
			//
		}
		#region send

		public void SendObject(string ID,Object obj)
		{
			
			try
			{	
				Configuration conf = new Configuration();
				string machineName = conf.DBServerName();
				MessageQueue mQ = new System.Messaging.MessageQueue(machineName + "\\" + ID);
				mQ.Formatter = new BinaryMessageFormatter();
				mQ.Send(obj);
			}
			catch(Exception e)
			{

			}
			
		}
		public void Send(string queuePath,object[] objs)
		{
			MessageQueue mQ = new System.Messaging.MessageQueue (queuePath);
			mQ.Formatter = new BinaryMessageFormatter();
			foreach(object obj in objs)
				mQ.Send(obj);

		}
		#endregion

		#region receive
		
		
		public Hashtable ReceiveMessages(MessageQueue[] MQ)
		{
			
			Hashtable MessageVsID = null;
			foreach(MessageQueue mq in MQ)
			{
				MessageQueue myQueue = new MessageQueue(mq.Path);
				myQueue.Formatter = new BinaryMessageFormatter();
				try
				{
					Message[] myMessages = myQueue.GetAllMessages();
					MessageVsID.Add(myQueue.QueueName,myMessages);
				}
				catch (MessageQueueException)
				{
					return null;
				}
				catch (InvalidOperationException e)
				{
					return null;
				}
			}
			return MessageVsID;
		}
		public System.Messaging.Message[] ReciveAllMessages(string ID)
		{
			Configuration conf = new Configuration();
			string machineName = conf.DBServerName();
			MessageQueue myQueue = new MessageQueue(machineName + "\\" + ID);
			return myQueue.GetAllMessages();
		}
		public Message ReciveMessage(string ID)
		{

			Configuration conf = new Configuration();
			string machineName = conf.DBServerName();
            MessageQueue myQueue = new MessageQueue(machineName + "\\" + ID);
			myQueue.MessageReadPropertyFilter.CorrelationId = true;
			Message returnMessage = null;
			myQueue.Formatter = new BinaryMessageFormatter();
			try
			{
				Message myMessage = myQueue.Receive(new TimeSpan(0,0,3));

				returnMessage = myMessage;
			}
			catch (MessageQueueException)
			{
				return null;
			}
			catch (InvalidOperationException e)
			{
				return null;
			}
			return returnMessage;
			 
		}
		
		#endregion

		
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Iran (Islamic Republic of) Iran (Islamic Republic of)
I Hate Programming But I Have To Come Up With It .

Comments and Discussions