MSMQ 消息队列的封装

介绍MSMQ 消息队列的文章很多,我不想多说,这里写了个对MSMQ 消息队列操作进行封装的类,做为自己初学MSMQ 消息队列的总结。代码如下:


ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
InBlock.gif    
/// 接受消息事件的代理
InBlock.gif    
/// </summary>
InBlock.gif    
/// <param name="src"></param>
ExpandedBlockEnd.gif    
/// <param name="e"></param>

None.gif      public   delegate   void  MSReceivedEventHandler( object  src, MSReceiveEventArgs e);
None.gif
ExpandedBlockStart.gifContractedBlock.gif    
/**/ /// <summary>
InBlock.gif    
/// 消息发出后事件的代理
InBlock.gif    
/// </summary>
InBlock.gif    
/// <param name="src"></param>
ExpandedBlockEnd.gif    
/// <param name="e"></param>

None.gif      public   delegate   void  MSSendedEventHandler( object  src, MSSendedEventArgs e);

ContractedBlock.gif ExpandedBlockStart.gif 发送消息的事件参数类
None.gifpublic class MSSendedEventArgs
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
private object msgContent;
InBlock.gif
InBlock.gif        
public object MsgContent
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.msgContent;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public MSSendedEventArgs(object msgContent)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.msgContent = msgContent;
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

ContractedBlock.gif ExpandedBlockStart.gif 接受消息处理事件参数类
ExpandedBlockStart.gifContractedBlock.gif/**//// <summary>
InBlock.gif    
/// 接受消息处理事件参数类
ExpandedBlockEnd.gif    
/// </summary>

None.gif    public class MSReceiveEventArgs
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
private Message msg;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 接受的消息
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public Message Msg
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.msg;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private bool isAsyContinue;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 接受消息后,是否再等待接受,在同步的情况下,此项无效
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public bool IsAsyContinue
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.isAsyContinue;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 接受消息处理事件参数类
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="message">消息</param>

InBlock.gif        public MSReceiveEventArgs(Message message)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.msg = message;
InBlock.gif            
this.isAsyContinue = false;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 接受消息处理事件
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="message">消息</param>
ExpandedSubBlockEnd.gif        
/// <param name="isAsyContinue">接受消息后,是否再等待接受,在同步的情况下,此项无效</param>

InBlock.gif        public MSReceiveEventArgs(Message message, bool isAsyContinue)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.msg = message;
InBlock.gif            
this.isAsyContinue = isAsyContinue;
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }


ContractedBlock.gif ExpandedBlockStart.gif 用MSMQ发消息的封装类
ExpandedBlockStart.gifContractedBlock.gif/**//// <summary>
InBlock.gif    
/// 用MSMQ发消息的消息类
ExpandedBlockEnd.gif    
/// </summary>

None.gif    public class MSQueue
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
protected MessageQueueTransactionType transactionType = MessageQueueTransactionType.Automatic;
InBlock.gif        
protected MessageQueue queue;
InBlock.gif        
protected TimeSpan timeout;
InBlock.gif        
protected bool isAsyContinue = false;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 接受消息后,是否再等待接受,在同步的情况下,此项无效
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public bool IsAsyContinue
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.isAsyContinue;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.isAsyContinue = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 接受消息后处理的事件
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public event MSReceivedEventHandler OnMSReceived;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 消息发出后的事件
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public event MSSendedEventHandler OnMSSended;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 创建消息类
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="queuePath">消息路径</param>
InBlock.gif        
/// <param name="timeoutSeconds">消息等待时间</param>
ExpandedSubBlockEnd.gif        
/// <param name="formatter">消息格式</param>

InBlock.gif        public MSQueue(string queuePath, int timeoutSeconds, IMessageFormatter formatter)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (MessageQueue.Exists(queuePath))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.queue = new MessageQueue(queuePath);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.queue = MessageQueue.Create(queuePath);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
this.timeout = TimeSpan.FromSeconds(Convert.ToDouble(timeoutSeconds));
InBlock.gif
InBlock.gif            queue.Formatter 
= formatter;
InBlock.gif
InBlock.gif            
// Performance optimization since we don't need these features
InBlock.gif
            queue.DefaultPropertiesToSend.AttachSenderId = false;
InBlock.gif            queue.DefaultPropertiesToSend.UseAuthentication 
= false;
InBlock.gif            queue.DefaultPropertiesToSend.UseEncryption 
= false;
InBlock.gif            queue.DefaultPropertiesToSend.AcknowledgeType 
= AcknowledgeTypes.None;
InBlock.gif            queue.DefaultPropertiesToSend.UseJournalQueue 
= false;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 发送消息
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="messageObj">消息内容</param>
ExpandedSubBlockEnd.gif        
/// <param name="lable">标签</param>

InBlock.gif        public void Send(object messageObj, string lable)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Message m 
= new Message(messageObj, this.queue.Formatter);
InBlock.gif            m.Label 
= lable;
InBlock.gif            
this.queue.Send(m);
InBlock.gif            
if (this.OnMSSended != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.OnMSSended(thisnew MSSendedEventArgs(messageObj));
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 发送消息
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="messageObj">消息内容</param>

InBlock.gif        public void Send(object messageObj)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Message m 
= new Message(messageObj, this.queue.Formatter);
InBlock.gif            
this.queue.Send(m);
InBlock.gif            
if (this.OnMSSended != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.OnMSSended(thisnew MSSendedEventArgs(messageObj));
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 同步接受消息
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public void Receive()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Message m 
= this.queue.Receive();
InBlock.gif            
if (this.OnMSReceived != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.OnMSReceived(thisnew MSReceiveEventArgs(m));
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 异步接受消息
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public void RecevieAsy()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.queue.ReceiveCompleted += new ReceiveCompletedEventHandler(this.ReceivedAsyEvt); //事件
InBlock.gif
            this.queue.BeginReceive(); 
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// ReceiveCompleted事件的实现
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="source"></param>
ExpandedSubBlockEnd.gif        
/// <param name="asyncResult"></param>

InBlock.gif        protected virtual void ReceivedAsyEvt(object source, ReceiveCompletedEventArgs asyncResult)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//异步接受消息
InBlock.gif
                Message m = this.queue.EndReceive(asyncResult.AsyncResult);
InBlock.gif
InBlock.gif                
//在此插入处理消息的代码 
InBlock.gif
                if (this.OnMSReceived != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
this.OnMSReceived(thisnew MSReceiveEventArgs(m, this.isAsyContinue));
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (MessageQueueException)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//错误处理代码
ExpandedSubBlockEnd.gif
            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (this.isAsyContinue)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
this.queue.BeginReceive();//接收下一次事件
ExpandedSubBlockEnd.gif
                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

同时我还写了个用于测试此MSMQ消息队列封装类的Demo, /Files/laiwen/MessageQueueProject.rar
欢迎广大网友多提宝贵意见。

转载于:https://www.cnblogs.com/laiwen/archive/2006/12/21/599519.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值