C#MSMQ 消息队列工具

这里写自定义目录标题

MSMQ消息队列工具

在这里插入图片描述
在这里插入图片描述
//定义全局变量
MessageQueue send_queue;
MessageQueue rece_queue;

    public  bool gb_auto_send = false; //自动发送
    public  bool gb_auto_rece = false; //自动接收
    public  bool gb_auto_bak  = false; //自动备份
    public  int  gi_cycle = 1;//线程周期 秒数
    
    System.Threading.Timer threadTimer; //定义Timer类
    public delegate void SetMQService(object value);//定义委托
 public UnMsMqForm()
    {
        InitializeComponent();
    }

///
/// 1.通过Create方法创建使用指定路径的新消息队列
///
///
public Tuple<bool, String> Createqueue_send( string queuePath)
{
try
{

            if (!MessageQueue.Exists(queuePath))
            {
                send_queue = MessageQueue.Create(queuePath);
            }
            else
            {
                send_queue = new MessageQueue(queuePath);

                return new Tuple<bool, string>(true, queuePath + " 已经存在!成功");
                //Console.WriteLine(queuePath + "已经存在!");
                //MessageQueue.Delete(queuePath);
                //MessageQueue.Create(queuePath);
                //Console.WriteLine(queuePath + "删除重建");
            }
            return new Tuple<bool, string>(true, "连接发送消息队列成功");
        }
        catch (MessageQueueException e)
        {
            //Console.WriteLine(e.Message);
            return new Tuple<bool, string>(false, e.Message);
        }
        catch (ArgumentException e)
        {
            //Console.WriteLine(e.Message);
            return new Tuple<bool, string>(false, e.Message);
        }
    }


    /// <summary>
    /// 1.通过Create方法创建使用指定路径的新消息队列
    /// </summary>
    /// <param name="queuePath"></param>
    public Tuple<bool, String> Createqueue_rece(string queuePath)
    {
        try
        {

            if (!MessageQueue.Exists(queuePath))
            {
                rece_queue = MessageQueue.Create(queuePath);
            }
            else
            {
                rece_queue = new MessageQueue(queuePath);

                return new Tuple<bool, string>(true, queuePath + " 已经存在!成功");
                //Console.WriteLine(queuePath + "已经存在!");
                //MessageQueue.Delete(queuePath);
                //MessageQueue.Create(queuePath);
                //Console.WriteLine(queuePath + "删除重建");
            }
            return new Tuple<bool, string>(true, "连接接收消息队列成功");
        }
        catch (MessageQueueException e)
        {
            //Console.WriteLine(e.Message);
            return new Tuple<bool, string>(false, e.Message);
        }
        catch (ArgumentException e)
        {
            //Console.WriteLine(e.Message);
            return new Tuple<bool, string>(false, e.Message);
        }
    }

    /// <summary>
    /// 2.连接消息队列并发送消息到队列
    /// 远程模式:MessageQueue rmQ = new MessageQueue("FormatName:Direct=OS:machinename//private$//queue");
    /// rmQ.Send("sent to regular queue - Atul");对于外网的MSMQ只能发不能收
    /// </summary>
    /// <param name="XMLStr">发送字符串</param>
    /// <param name="EncodingType"></param>
    /// <param name="LableStr"></param>
    /// <param name="MsgTracType"></param>
    /// <returns></returns>
    public  Tuple<bool, string> SendMessage(string XMLStr, string LableStr)
    {
        try
        {
            System.Text.Encoding EncodingType = null;
            MessageQueueTransactionType MsgTracType = MessageQueueTransactionType.Single;

            if (EncodingType == null)
            {
                EncodingType = System.Text.Encoding.Unicode;
            }
            //lock (MsgQueueHelperLocker.MQSendLocker)
            //{
            //连接到本地队列

            //MessageQueue myQueue = new MessageQueue(string.IsNullOrEmpty(StrPath) ? QPath : StrPath);//"FormatName:Direct=TCP:172.20.20.46\\private$\\in"
            System.Messaging.Message myMessage = new System.Messaging.Message();

            Byte[] StrBytes = EncodingType.GetBytes(XMLStr);
            myMessage.BodyStream = new MemoryStream(StrBytes);

            //MSMQ消息标签设置,格式:YJCHDECDSB2B_S_ECDS_企业十位海关编码_0000000000_业务编号__年月日时分秒毫秒_GUID。
            //企业十位海关编码:报文B2B_BE_CHECK节点中的TRADE_CODE。
            //业务编号:报文B2B_BE_CHECK节点中的ERP_NO。
            if (!string.IsNullOrEmpty(LableStr))
                myMessage.Label = LableStr;// "YJCHDECDSB2B_S_ECDS_" + objVal.B2B_BE_CHECK.TRADE_CODE + "_0000000000_" + objVal.B2B_BE_CHECK.ERP_NO + "__" + DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + Guid.NewGuid().ToString();
            //if (myQueue.Transactional)
            //    MsgTracType = MessageQueueTransactionType.Single;
            //发生消息到队列中
            send_queue.Send(myMessage, MsgTracType);
            //}

            return new Tuple<bool, string>(true, "send ok");

        }
        catch (MessageQueueException e)
        {
            return new Tuple<bool, string>(false, e.Message);
            //Console.WriteLine(e.Message);

        }           
        catch (NullReferenceException e)
        {
            return new Tuple<bool, string>(false, e.Message);
            //Console.WriteLine(e.Message);
        }           

        
    }

程序代码下载 https://download.csdn.net/download/u014734340/10946221

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值