Jimmypony的技术汇总区
很多都不会,很多都不懂,不要浮躁,静心学习

学习了beniao的消息队列,自己也敲打了些代码写出了自己设计的简单消息队列

1 简单的消息队列的Demo

 

//附带的实体类
    public class people
    
{
        
private string _name;
        
private int? _age;
        
private DateTime _birthday;
        
private bool _IsMale;

        
public string Name
        
{
            
get return _name; }
            
set { _name = value; }
        }

        
public int? Age
        
{
            
get return _age; }
            
set { _age = value; }
        }

        
public DateTime Birthday
        
{
            
get return _birthday; }
            
set { _birthday = value; }
        }

        
public bool IsMale
        
{
            
get return _IsMale; }
            
set { _IsMale = value; }
        }

    }

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Messaging;       //reference添加引用

namespace demos
{

    
class Msmq
    
{
       
private MessageQueue _msmq;                     //事务队列
        private Message _msg;                           //消息体
        private XmlMessageFormatter _formatter;         //消息格式(通过XML序列化和反序列化方式)

        
/// <summary>
        
/// 构造函数 如果队列不存在创建,设置内置对象为队列实例,并新建消息体
        
/// </summary>
        
/// <param name="msmqPath">消息队列的路径</param>
        
/// <param name="formatter">消息和队列格式</param>

        public Msmq(string msmqPath,XmlMessageFormatter formatter)
        
{
            
if (!MessageQueue.Exists(msmqPath))
            
{
                
try
                
{
                    MessageQueue.Create(msmqPath);
                }

                
catch (Exception)
                
{
                    
throw;
                }

            }

            _msmq 
= new MessageQueue(msmqPath);
            _formatter 
= formatter;
            _msg 
= new Message();
            
            
//消息格式和队列格式都必须一模一样
            _msmq.Formatter = _formatter;   //设置队列格式
            _msg.Formatter = _formatter;    //设置消息格式
        }


        
public void SendMessage(object msgInfo)
        
{
            
try
            
{
                _msg.Body 
= msgInfo;
                _msmq.Send(_msg);
            }

            
catch (Exception)
            
{
                
throw;
            }

        }


        
public object GetMessage()
        
{
            Message msg 
= _msmq.Receive();
            
return msg.Body;
        }

    }


    
class Program
    
{
        
static void Main(string[] args)
        
{
            Msmq m 
=new Msmq(@".\private$\MsmqTest",new XmlMessageFormatter(new Type[] typeof(object) }));
                  people p = new people();
                  p.Name = "aa";
                  p.Age = 20;
                  p.Birthday = DateTime.Now;
                  p.IsMale = false;
                  m.SendMessage(p);
                  Console.WriteLine((m.GetMessage() as people).Name);
                  Console.Read();

        }

    }

}

posted on 2008-07-20 13:44  Jim~  阅读(266)  评论(0编辑  收藏  举报