在C#中使用MSMQ的方法
MSMQ(Microsoft消息队列)是Windows中默认可用的消息队列。作为跨计算机系统发送和接收消息的可靠方法,MSMQ提供了一个可伸缩、线程安全、简单和使用方便的队列,同时为你提供了在Windows数据库中持久化消息的机会。MSDN指出:“消息队列(MSMQ)技术使在不同时间运行的应用程序能够在可能暂时离线的异构网络和系统之间进行通信。应用程序将消息发送到队列并从队列读取消息。”
在使用MSMQ时,通常有两个不同的应用程序——发送者和接收者。当消息由发送者发送时,接收者应用程序不需要处于执行状态——消息实际上存储在由主机操作系统维护的队列中,当接收方应用程序需要它们时,这些消息就会脱离队列。
创建一个队列
你可以通过控制面板上的“打开或关闭Windows功能”选项打开你系统中的MSMQ。在系统中安装了MSMQ之后,创建队列就很简单了。只要到“我的电脑”,右击并选择管理。在“计算机管理”窗口中,你可以从“消息队列”节点创建一个新队列。还可以通过编程方式创建队列。
C#中的MSMQ
要使用MSMQ,你需要引入System.Messaging命名空间。要以编程方式创建队列,需要利用MessageQueue类的Create方法。下面的代码片段说明了这一点。
MessageQueue.Create(@".\Private$\IDG");
要创建队列并向其发送消息,可以使用以下代码片段:
messageQueue=newMessageQueue(@".\Private$\IDG");
messageQueue.Label="Thisisatestqueue.";
messageQueue.Send("Thisisatestmessage.","IDG");
现在,假设你想检查队列是否存在,如果存在,则发送一条消息给它。如果队列不存在,你可能希望创建一个新队列,然后向它发送消息。这正是下面的代码清单所做的。
staticvoidMain(string[]args)
{
MessageQueuemessageQueue=null;
stringdescription="Thisisatestqueue.";
stringmessage="Thisisatestmessage.";
stringpath=@".\Private$\IDG";
try
{
if(MessageQueue.Exists(path))
{
messageQueue=newMessageQueue(path);
messageQueue.Label=description;
}
else
{
MessageQueue.Create(path);
messageQueue=newMessageQueue(path);
messageQueue.Label=description;
}
messageQueue.Send(message);
}
catch
{
throw;
}
finally
{
messageQueue.Dispose();
}
}
下面的代码清单演示了如何使用c#处理存储在消息队列中的消息。
privatestaticListReadQueue(stringpath) { List lstMessages=newList (); using(MessageQueuemessageQueue=newMessageQueue(path)) { System.Messaging.Message[]messages=messageQueue.GetAllMessages(); foreach(System.Messaging.Messagemessageinmessages) { message.Formatter=newXmlMessageFormatter( newString[]{"System.String,mscorlib"}); stringmsg=message.Body.ToString(); lstMessages.Add(msg); } } returnlstMessages; }
接下来,可以调用ReadQueue方法来检索存储在消息队列中的消息,如下面的代码片段所示。
stringpath=@".\Private$\IDG"; ListlstMessages=ReadQueue(path);
你还可以在消息队列中存储对象。例如,假设你需要将日志消息存储到队列中。日志消息存储在LogMessage类的一个实例中,该实例包含与日志消息的详细信息相关的必要属性。下面是LogMessage类——我只使用了两个属性来使它变得简单。
publicclassLogMessage
{
publicstringMessageText{get;set;}
publicDateTimeMessageTime{get;set;}
}
你应该修改LogMessage类,以包含其他必要的属性,例如,消息严重性等。下面的方法说明了如何将LogMessage类的实例存储到消息队列中。
privatestaticvoidSendMessage(stringqueueName,LogMessagemsg)
{
MessageQueuemessageQueue=null;
if(!MessageQueue.Exists(queueName))
messageQueue=MessageQueue.Create(queueName);
else
messageQueue=newMessageQueue(queueName);
try
{
messageQueue.Formatter=newXmlMessageFormatter(newType[]{typeof(LogMessage)});
messageQueue.Send(msg);
}
catch
{
//Writecodeheretodothenecessaryerrorhandling.
}
finally
{
messageQueue.Close();
}
}
下面的代码片段说明了如何创建LogMessage类的实例,向其填充数据,然后调用SendMessage方法来存储在消息队列中创建的实例。
LogMessagemsg=newLogMessage()
{
MessageText="Thisisatestmessage.",
MessageTime=DateTime.Now
};
SendMessage(@".\Private$\IDGLog",msg);
下面的代码清单演示了如何读取存储在消息队列中的LogMessage实例。
privatestaticLogMessageReceiveMessage(stringqueueName)
{
if(!MessageQueue.Exists(queueName))
returnnull;
MessageQueuemessageQueue=newMessageQueue(queueName);
LogMessagelogMessage=null;
try
{
messageQueue.Formatter=newXmlMessageFormatter(newType[]{typeof(LogMessage)});
logMessage=(LogMessage)messageQueue.Receive().Body;
}
catch{}
finally
{
messageQueue.Close();
}
returnlogMessage;
}
以上就是在C#中使用MSMQ的方法的详细内容,更多关于c#使用msmq的资料请关注毛票票其它相关文章!