2012年4月16日 星期一

[C#] 建立第一支C# MSMQ程式 (First MSMQ program in C#)

How to create my first MSMQ program in C#

The FORCE studio

建立第一支C# MSMQ程式
作者:JB

We can read this article : 瞭解MSMQ,控制ASP進程 to get how a Message Queue Service works.

Before writing a MSMQ program , we must install the Message Queue Service in Windows.

We will use the library called
System.Messaging.MessageQueu to create and send our message in the C# project.

1         InstallTake Windows Vista for example
1.1      Install Message Queue service


1.2      The MSMQ manager is in 「控制台」「電腦管理」「訊息佇列」


1.3      We can check our MSMQ service as well.


2         Create my first MSMQ program in C# (Use a windows projectfor example)

2.1      The final output winform would be looked like this :

TexBox : The string message which will be send to the MSMQ.
Send msg to MQ】:Send the String in TextBox to MSMQ.
Start to get MQ】:Start the timer to get a new message from MSMQ.
Stop to get MQ】:Stop the timer to cancel getting messages from MSMQ.
TimerUsed to get the message from MSMQ every other time.

2.2       The Common method
MessageQueue.Formatter
MessageQueue.Label
MessageQueue.Path
MessageQueue.Peek
MessageQueue.Receive
MessageQueue.Send

Now we are going to write the codes!

2.3      Make a Queue Class
2.3.1             Property
public MessageQueue mq;
               public String sReceiveData;

2.3.2               Constructer
public Queue(String sType, String QueuePath)
{
 
try
 
{
    //
建立一個MSMQ
   
if (!MessageQueue.Exists(QueuePath))
   
{
     
MessageQueue.Create(QueuePath);
   
}
    //
MessageQueue物件指向 指定的MSMQ
   
mq = new MessageQueue(QueuePath);
   
mq.Formatter = new System.Messaging.XmlMessageFormatter();
   
//mq.ReceiveCompleted += MQ_ReceiveCompleted;
   
mq.Refresh();
   
sReceiveData = "";
 
}
 
catch (Exception ex)
 
{
   
throw ex;
 
}
}

2.3.3               Send message event
public void SendByLabel(String sBody, String sMsg)
{
 
try
 
{
   
mq.Send(sBody, sMsg);
 
}
 
catch (Exception ex)
 
{
   
throw ex;
 
}
}

2.3.4               Peek event
public bool Peek()
{
 
try
 
{
   
TimeSpan timeout = new TimeSpan(0, 0, 10); //設定timeout
   
if (mq.Peek(timeout) != null)
   
{
     
return true;
   
}
   
else
   
{
     
return false;
   
}
 
}
 
catch (MessageQueueException ex)
 
{
   
Console.WriteLine(ex.ErrorCode);
   
if (ex.ErrorCode == -2147467259) //已無訊息佇列
   
{
     
return false;
   
}
   
else
   
{
     
throw ex;
   
}
 
}
}

2.3.5               Receive event
public void Receive()
{
 
try
 
{
   
TimeSpan timeout = new TimeSpan(0, 0, 10); //設定timeout
   
Message msg = mq.Receive(timeout);
   
sReceiveData = msg.Label;
 
}
 
catch (Exception ex)
 
{
   
throw ex;
 
}
}

2.3.6               Done with class Queue.

2.4      Next step, we will create a global Queue item in our MainForm.cs:

Queue
MyQueue;

 And in the constructer of MainForm, we initial it …

MyQueue = new Queue("private", ".\\Private$\\MyQueue");

2.5      In the Click event of Send msg to MQ, we will send a message to MyQueue.
MyQueue.SendByLabel("MSMQ Msg Title", tb_send.Text);

Send a message to MSMQ will be looked like this…




2.6      Now how receive the message from MSMQ? We can only get ONE message each time.
So we can use the TIMER to repeat this step.
I set the Timer’s
Interval to 10000 (10 sec).
Surely you might change the value, but if the interval is too small, the program will PEEK the MSMQ too often that will get the performance pretty bad.
So in timer_Tick event, we write the codes for receiving a message…

private void timer_Tick(object sender, EventArgs e)
{
 
timer.Stop();
 
try
 
{
   
if (MyQueue.Peek() == true) //Peek訊息佇列
   
{
     
MyQueue.Receive(); //取出訊息佇列
     
String sMsg = MyQueue.sReceiveData;
     
if (sMsg.Length > 0)
     
{
       
lb_ReciveData.Items.Add(sMsg);
     
}
   
}
 
}
 
catch (Exception ex)
 
{
   
MessageBox.Show("timer_Tick失敗:" + ex.Message);
 
}
 
finally
 
{
   
timer.Start();
 
}
}

So we take a PEEK step before we RECEIVE it. The PEEK will return the first message in the MSMQ. The RECEIVE step will get it, that means delete the message after receiving it.

2.7      Since we finished the Timer event, the Start to get MQ/Stop to get MQ is used to start/stop the Timer.

2.8       Important!
Always set a TIMEOUT when doing PEEKRECEIVE.
   If not, your program will suspend while there is no message in the MSMQ.
If there is no message in the MSMQ, the method of PEEKRECEIVE will return a  
  
MessageQueueException. The error code is -2147467259.
   Remember to catch it!



3         Test screenshot




沒有留言:

張貼留言