.net的MSMQ异步调用

来源:互联网 发布:python excel 修改 编辑:程序博客网 时间:2024/05/29 16:02
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

1、引用System.messagiong.dll2、using System.messagiong 命名空间3、创建/建立自己需要的队列(这里监测专有队列)

string queuename="./private$/testQueue";   MessageQueue q;   if(MessageQueue.Exists(queuename) )   {    q=new System.Messaging.MessageQueue (queuename);    }   else   {    q=MessageQueue.Create(queuename);    }这代代码执行后,会在消息队列中的专有队列看到名为testQueue的队列。具体为管理工具--〉计算机管理--〉服务和应用程序--〉消息队列--〉专用队列可以看到队列中存在的消息4、向队列发送消息两种发送消息的方式:简单消息,直接利用q.send("....");的方式发送,复杂消息(譬如一个特定对象,当然是可序列化的)采用Message m=new Message();   m.Label ="msg1";   m.Body ="test body";   q.Send (m);发送。消息会采用缺省的XML格式编码送到队列中5、消息接收  主动方式,会同步阻塞当前线程    Message m=Receive(); //接收同时,从队列中删除消息    or    Message m=Peek();  //不删除接收到的消息  被动方式,异步调用方式,不会阻塞当前线程    q.ReceiveCompleted +=new ReceiveCompletedEventHandler( ReceivedEvt); //事件    q.BeginReceive();     其中ReceivedEvt要么为静态函数,要么必须是属于实例化的对象的可访问函数     private static void ReceivedEvt(object source, ReceiveCompletedEventArgs asyncResult)  {   try   {        MessageQueue mq = (MessageQueue)source;      Message m = mq.EndReceive(asyncResult.AsyncResult);                 //此事m为异步接收到的消息    //在此插入处理消息的代码     Console.WriteLine("接收到消息"+m.Label );

    mq.BeginReceive();//接收下一次事件   }   catch(MessageQueueException)   {   }                     return;   }  

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击