zoukankan      html  css  js  c++  java
  • MsMq的消息创建、发送和接收操作

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Messaging;
    
    
    namespace MSMQ
    {
        public partial class MsMq
        {
    
            //通过Create方法创建使用指定路径的新消息队列
            /**/
            /// <summary>
            /// 通过Create方法创建使用指定路径的新消息队列
            /// </summary>
            /// <param name="queuePath"></param>
            public static void Createqueue(string queuePath)
            {
                try
                {
                    if (!MessageQueue.Exists(queuePath))
                    {
                        MessageQueue.Create(@".\private$\myQueue");
                    }
                    else
                    {
                        MessageBox.Show(queuePath + "已经存在!");
                    }
                }
                catch (MessageQueueException e)
                {
                    MessageBox.Show(e.Message);
                }
            }
            /**/
            /// <summary>
            /// 连接消息队列并发送消息到队列
            /// </summary>
            public static void SendMessage()
            {
                try
                {
                    //连接到本地的队列
                    MessageQueue myQueue = new MessageQueue(".\\private$\\myQueue");
    
                    MessageQueueTransaction myTransaction = new MessageQueueTransaction();
    
                    System.Messaging.Message myMessage = new System.Messaging.Message();
                    myMessage.Body = System.Guid.NewGuid().ToString() + "消息内容";
                    myMessage.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
                    //发送消息到队列中
                    myQueue.Send(myMessage);
    
                    MessageBox.Show("消息发送成功");
    
                }
                catch (ArgumentException e)
                {
                    MessageBox.Show(e.Message);
                }
            }
    
            /**/
            /// <summary>
            /// 连接消息队列并从队列中接收消息
            /// </summary>
            public static void ReceiveMessage()
            {
                //连接到本地队列
                MessageQueue myQueue = new MessageQueue(".\\private$\\myQueue");
    
                if (message.Length == 0)
                {
                    MessageBox.Show("队列中没有任何消息!");
                    return;
                }
                myQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
    
                try
                {
    
                    //从队列中接收消息
                    System.Messaging.Message myMessage = myQueue.Receive();
    
                    string context = (string)myMessage.Body; //获取消息的内容
                    //Console.WriteLine("消息内容为:" + context);
                    MessageBox.Show("消息内容为:" + context);
    
                }
                catch (MessageQueueException e)
                {
                    MessageBox.Show(e.Message);
                }
                catch (InvalidCastException e)
                {
                    MessageBox.Show(e.Message);
                }
            }
        }
    }
  • 相关阅读:
    FileUpload1上传控件
    docker如何push镜像到docker hub个人的仓库
    docker的ubuntu镜像无ifconfig和ping命令
    keystone同步数据库的时候提示error
    openstack安装dashboard后访问horizon出错 500 or 504
    装了ubuntu之后,只能进入ubuntu系统,不能进入windows系统
    Kernal Panic
    无法获得锁 /var/lib/dpkg/lock -open
    用户 'NT AUTHORITYIUSR' 登录失败
    配置错误:不能在此路径中使用此配置节。
  • 原文地址:https://www.cnblogs.com/iwenwen/p/3129286.html
Copyright © 2011-2022 走看看