C#基础教程(十三)消息队列——MSMQ

Net使用消息队列,借助windows组件来存储要完成的一系列任务,不用程序使用同一个队列,方便不同程序之间的数据共享和协作。

队列分事务性队列和非事务性队列,默认创建的是非事务性队列。那么什么是事务性队列呢?事务性队列将消息保存在磁盘上,实现了持久化,也就是说当我们关机,断电后,下次再启动机器,我们的消息依然保存在队列里面,而非事务性队列则将消息保存在内存中,也就是说我重启电脑后,队列里面的消息将不存在了。

(一) 复杂消息收发(对象)

static void Main(string[] args)
{
    const string queueName = @".\Private$\jiyiqin";
    MessageQueue mq = null;
    if (!MessageQueue.Exists(queueName))// 如果指定的路径queueName中不存在队列,那么在该路径,即queueName中创建一个消息队列。jiyiqin就是你想要创建消息队列的名字
    {
        mq = MessageQueue.Create(queueName);//创建名称jiyiqin的消息队列的实例。
        Console.WriteLine("创建消息队列完成:" + queueName);
    }
    else  //如果消息队列jiyiqin已经存在,那么创建该消息队列的一个实例
    {
        mq = new MessageQueue(queueName);//创建名称jiyiqin的消息队列的实例。
    }
    mq.SetPermissions("Administrator", MessageQueueAccessRights.FullControl);
    mq.SetPermissions("ANONYMOUS LOGON", MessageQueueAccessRights.FullControl);
    mq.SetPermissions("Everyone", MessageQueueAccessRights.FullControl);

    Message msgTx = new Message();
    msgTx.Formatter = new XmlMessageFormatter(new Type[] { typeof(MsgModel) });
    msgTx.Body = new MsgModel("1", "消息1");

    mq.Send(msgTx);
    Console.Write("成功发送消息," + DateTime.Now + "");

    if (mq.GetAllMessages().Length > 0)
    {
        System.Messaging.Message message = mq.Receive(TimeSpan.FromSeconds(5));
        if (message != null)
        {
            message.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(MsgModel) });//消息类型转换
            MsgModel msg = (MsgModel)message.Body;
            Console.WriteLine(msg.id+msg.Name);
            Console.Read();
        }
    }
}

(二) 本地发/收

//发送
static void Main(string[] args)
{
    MessageQueue MSMQ = CreateMessageQueue(@".\private$\jiyiqin");
    MSMQ.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
    Console.WriteLine("是否继续发送消息:Y/N?");
    string cmd = Console.ReadLine();
    while (cmd.Equals("Y"))
    {
        Sender(MSMQ);
        Console.WriteLine("是否继续发送消息:Y/N?");
        cmd = Console.ReadLine();
    }
    Console.WriteLine("按任意键以停止...");
    Console.ReadKey();
}
private static void Sender(MessageQueue MSMQ)
{
    try
    {
        string random = new Random().Next(0,100).ToString();
        string obj = string.Format("{0} 发送方:{1}",DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), random);
        MSMQ.Send(obj, MessageQueueTransactionType.Single);
        Console.WriteLine(obj);

    }
    catch (Exception ex)
    {
        Console.WriteLine(string.Format("{0} 发送方:{1}",
        DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ex.Message));
    }
}

public static MessageQueue CreateMessageQueue(string path)
{
    MessageQueue mq = null;              
    if (MessageQueue.Exists(path))
    {
        //存在,创建队列实例
        mq = new MessageQueue(path);
    }
    else
    {
        //不存在,创建消息队列
        mq = MessageQueue.Create(path, true);
    }
    return mq;
}

消息队列即使接收端没开启,消息仍会阻塞在队列中,等接收端开启,就可以一条条加载消息。

(三) 异地指定队列增加消息

异地指定队列增加消息,我们测试同一局域网内两台计算机发/收。

///接收端 ip 192.168.2.240
static void Main(string[] args)
{
    MessageQueue MSMQ = CreateMessageQueue(@".\private$\jiyiqin");
    MSMQ.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });

    Receiver(MSMQ);
}
private static void Receiver(MessageQueue MSMQ)
{
    while (true)
    {
        try
        {
            Message m = MSMQ.Receive(MessageQueueTransactionType.Single);
            Console.WriteLine(string.Format("{0} 接收方:[{1}]",DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), m.Body.ToString()));
        }
        catch (Exception ex)
        {
            Console.WriteLine(string.Format("{0} 接收方:{1}",
            DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ex.Message));
        }
    }
}

public static MessageQueue CreateMessageQueue(string path)
{
    MessageQueue mq = null;

    if (MessageQueue.Exists(path))
    {
        mq = new MessageQueue(path);
    }
    else
    {
        mq = MessageQueue.Create(path, true);
    }
    return mq;
}

远程队列的路径格式:string path = @"Formatname:DIRECT=tcp:192.168.2.240\Private$\jiyiqin";  关键字不区分大小写

MSMQ 判断队列是否存在的方法(MessageQueue.Exists(string path))和创建队列(MessageQueue.Create(string path)),都是不支持远程队列的。

1.使用Exists方法会出现错误【无法确定具有指定格式名的队列是否存在】

2.使用Create方法会出现错误【无法创建路径为 FormatName:DIRECT=tcp:192.168.2.240\Private$\jiyiqin】

3.由于前两条的限制,如果要访问远程专用队列,则必须保证事先在远程机器上该队列是存在的。

static void Main(string[] args)
{
    MessageQueue MSMQ = CreateMessageQueue(@"FormatName:Direct=TCP:192.168.2.240\Private$\jiyiqin");
    MSMQ.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });

    Console.WriteLine("是否继续发送消息:Y/N?");
    string cmd = Console.ReadLine();

    while (cmd.Equals("Y"))
    {
        Sender(MSMQ);
        Console.WriteLine("是否继续发送消息:Y/N?");   
        cmd = Console.ReadLine();
    }

    Console.WriteLine("按任意键以停止...");
    Console.ReadKey();
}
private static void Sender(MessageQueue MSMQ)
{
    try
    {
        string random = new Random().Next(0,100).ToString();
        string obj = string.Format("{0} 发送方:{1}",DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), random);
        MSMQ.Send(obj, MessageQueueTransactionType.Single);
        Console.WriteLine(obj);
    }
    catch (Exception ex)
    {
        Console.WriteLine(string.Format("{0} 发送方:{1}",
        DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ex.Message));
    }
}

public static MessageQueue CreateMessageQueue(string path)
{
    MessageQueue mq = null;  
    // MessageQueue.Exists(path)会报错
    if (true)
    {
        //存在,创建队列实例
        mq = new MessageQueue(path);
    }
    else
    {
        //不存在,创建消息队列
        mq = MessageQueue.Create(path, true);
    }
    return mq;
}

当发送消息到远程队列时,系统会在本机的传出队列下创建一个临时队列,每发送一条消息,该消息都会先存在临时队列中,这样做的目的是防止因远程队列无法访问而丢失消息,

总结

特别注意的是,如果远程机器不能成功连接,则消息就一直在临时队列中存放;如果能成功连接,即使要访问的队列并不存在,消息发送程序也不会报错,并且临时队列中的消息会删除。

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值