有关于MSMQ (继续增加中。。。)

本文介绍了消息队列(Message Queue, MQ)的多种应用场景,包括异步接收消息、格式化消息、使用不同路径语法创建队列及发送接收自定义类对象等。通过具体代码示例,展示了如何使用C#实现这些功能。
1.异步接收的例子
2.格式化消息的例子
3.使用各种路径名语法类型创建新的 MessageQueue 对象例子
4.创建一个类,并发送和接收类中的值 例子

None.gif     //异步接收
ContractedBlock.gifExpandedBlockStart.gif
        ReceiveAsynchronously#region ReceiveAsynchronously
InBlock.gif
InBlock.gif        
private void ReceiveAsynchronously()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//创建一个MessageQueue实例,并设为Path and Formatter
InBlock.gif
            MessageQueue mq = new MessageQueue(".\\MyQueue");
ExpandedSubBlockStart.gifContractedSubBlock.gif            ((XmlMessageFormatter)mq.Formatter).TargetTypeNames 
= new String[] dot.gif"System.String" };
InBlock.gif
InBlock.gif            
//设置事件句柄(利用委托机制)
InBlock.gif
            mq.ReceiveCompleted += new ReceiveCompletedEventHandler(OnReceiveCompleted);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void OnReceiveCompleted(object sender, ReceiveCompletedEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            MessageQueue mq 
= (MessageQueue)sender;
InBlock.gif            Message message 
= mq.EndReceive(e.AsyncResult);
InBlock.gif            Console.WriteLine(
"Message:" + (String)m.Body);
InBlock.gif
InBlock.gif            
//调用BeginReceive方法开始进行异步接收的操作
InBlock.gif
            mq.BeginReceive();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
//以上BeginReceive只能接收一条消息,如果想要持续的接收消息的话,你必须做一个线程,然后进行间隔的调用(OnReceiveCompleted).:)
InBlock.gif

ExpandedBlockEnd.gif        
#endregion

None.gif
None.gif        
//用于格式化消息
ContractedBlock.gifExpandedBlockStart.gif
        FormatterMessage#region FormatterMessage
InBlock.gif
InBlock.gif        
public static void FormatterMessage(MessageQueue mq, String Msg_Content, Msg_Type Msg_Type_Instance)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
switch (Msg_Type_Instance)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
case Msg_Type.XML:
InBlock.gif                    
//XmlMessageFormatter 是 MessageQueue 的实例用来序列化写入队列的消息的默认格式化程序。
InBlock.gif                    
//当创建 MessageQueue 的实例时,会为您创建 XmlMessageFormatter 的实例,并将此实例与 MessageQueue 关联。
InBlock.gif                    
//通过在代码中创建格式化程序并将其分配给 MessageQueue 的 Formatter 属性,可以指定其他格式化程序。
InBlock.gif
                    mq.Formatter =new XmlMessageFormatter();
InBlock.gif                    
break;
InBlock.gif                
case Msg_Type.Binary:
InBlock.gif                    
//BinaryMessageFormatter 是非常有效的并且可用于序列化大多数对象。
InBlock.gif                    
//结果非常紧凑并且可以进行快速分析,但不允许像 XmlMessageFormatter 所做的那样进行松耦合消息处理。
InBlock.gif                    
//松耦合意味着客户端和服务器可以独立控制发送和接收的类型的版本。
InBlock.gif                    
//当应用程序使用 MessageQueue 类的实例将消息发送到队列时,格式化程序将对象序列化到流中并且将其插入到消息体。
InBlock.gif                    
//在使用 MessageQueue 从队列中读取时,格式化程序将消息数据反序列化到 Message 的 Body 属性中。
InBlock.gif                    
//BinaryMessageFormatter 提供比 XmlMessageFormatter 更快的吞吐速度。
InBlock.gif                    
//当需要纯速度而非松耦合消息处理时使用 BinaryMessageFormatter。
InBlock.gif
                    mq.Formatter =new BinaryMessageFormatter();
InBlock.gif                    
break;
InBlock.gif                
case Msg_Type.ActiveX:
InBlock.gif                    
//ActiveX 序列化非常紧凑,这使得使用 ActiveXMessageFormatter 和消息队列 COM 控件成为非常快的序列化方法。
InBlock.gif
                    mq.Formatter =new ActiveXMessageFormatter();
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif            }

InBlock.gif            mq.Send(Msg_Content);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public enum Msg_Type
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            XML,
InBlock.gif            Binary,
InBlock.gif            ActiveX
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedBlockEnd.gif        
#endregion

None.gif

None.gif//使用各种路径名语法类型创建新的 MessageQueue 对象
ContractedBlock.gifExpandedBlockStart.gif
CommonReference#region CommonReference
InBlock.gif
InBlock.gif        
//引用公用队列 
InBlock.gif
        public void SendPublic()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            MessageQueue myQueue 
= new MessageQueue(".\\myQueue");
InBlock.gif            myQueue.Send(
"Public queue by path name.");
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//引用专用队列
InBlock.gif
        public void SendPrivate()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            MessageQueue myQueue 
= new MessageQueue(".\\Private$\\myQueue");
InBlock.gif            myQueue.Send(
"Private queue by path name.");
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//引用label Queue
InBlock.gif
        public void SendByLabel()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            MessageQueue myQueue 
= new MessageQueue("Label:TheLabel");
InBlock.gif            myQueue.Send(
"Queue by label.");
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//References queues by format name.
InBlock.gif
        public void SendByFormatName()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            MessageQueue myQueue 
= new MessageQueue("FormatName:Public=5A5F7535-AE9A-41d4-935C-845C2AFF7112");
InBlock.gif            myQueue.Send(
"Queue by format name.");
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//References computer journal queues.
InBlock.gif
        public void MonitorComputerJournal()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            MessageQueue computerJournal 
= new MessageQueue(".\\Journal$");
InBlock.gif            
while(true)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Message journalMessage 
= computerJournal.Receive();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//引用日记队列
InBlock.gif
        public void MonitorQueueJournal()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            MessageQueue queueJournal 
= new MessageQueue(".\\myQueue\\Journal$");
InBlock.gif            
while(true)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Message journalMessage 
= queueJournal.Receive();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
//引用死信队列
InBlock.gif
        public void MonitorDeadLetter()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            MessageQueue deadLetter 
= new MessageQueue(".\\DeadLetter$");
InBlock.gif            
while(true)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Message deadMessage 
= deadLetter.Receive();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
// References transactional dead-letter queues.
InBlock.gif
        public void MonitorTransactionalDeadLetter()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            MessageQueue TxDeadLetter 
= new 
InBlock.gif                MessageQueue(
".\\XactDeadLetter$");
InBlock.gif            
while(true)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Message txDeadLetter 
= TxDeadLetter.Receive();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }
 
ExpandedBlockEnd.gif    
#endregion

None.gif//发送和接收[Order类]
ContractedBlock.gifExpandedBlockStart.gif
SendObjectAndReceiveObject#region SendObjectAndReceiveObject
InBlock.gif
InBlock.gif        
//创建一个类
InBlock.gif
        public class Order
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
public int orderId;
InBlock.gif            
public DateTime orderTime;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 发送消息
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private static void SendMessage()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Order sendOrder 
= new Order();
InBlock.gif            sendOrder.orderId 
= 3;
InBlock.gif            sendOrder.orderTime 
= DateTime.Now;
InBlock.gif
InBlock.gif            
//手动建立公用队列
InBlock.gif
            MessageQueue myQueue = new MessageQueue(".\\myQueue");
InBlock.gif            myQueue.Send(sendOrder);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 接收消息
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private static void ReceiveMessage()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//手动建立公用队列
InBlock.gif
            MessageQueue myQueue = new MessageQueue(".\\myQueue");
ExpandedSubBlockStart.gifContractedSubBlock.gif            myQueue.Formatter 
= new XmlMessageFormatter(new Type[] dot.giftypeof(Order) });
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                System.Messaging.Message myMessage 
= myQueue.Receive();
InBlock.gif                Order myOrder 
= (Order)myMessage.Body;
InBlock.gif
InBlock.gif                Console.WriteLine(
"Order id:" + myOrder.orderId.ToString());
InBlock.gif                Console.WriteLine(
"Sent:" + myOrder.orderTime.ToString());
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
catch (MessageQueueException) dot.gif{ }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
catch (InvalidOperationException e) dot.gif{ Console.WriteLine(e.Message); }
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif        
#endregion
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值