zoukankan      html  css  js  c++  java
  • 消息队列操作

    消息队列主要是用来处理两台计算机或者两个应用程序之间的消息传递。

    Receive接受完之后,就删除该Message。Peek接受完不删除该Message。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Messaging;
    using System.Net;
    using System.IO;
    using System.Diagnostics;

    namespace Egensource.MessageQueue.SMS
    {
        public class SMS
        {
            private const string smsUrl = System.Configuration.ConfigurationSettings.AppSettings["SMSUrl"].ToString();
            /// <summary>
            /// 发送消息队列,用来发送短信
            /// </summary>
            /// <param name="mqName">消息队列名称</param>
            /// <param name="smsinfo">短信信息对象</param>
            public void SendMessage(string mqName, SMSInfo smsinfo)
            {
                MessageQueue mq;
                if (MessageQueue.Exists(@".\private$\" + mqName))
                {
                    mq = new MessageQueue(@".\private$\" + mqName);
                }
                else
                {
                    mq = MessageQueue.Create(@".\private$\" + mqName, true);
                }
                if (mq.Transactional)
                {
                    MessageQueueTransaction myTransaction = new MessageQueueTransaction();
                    try
                    {
                        myTransaction.Begin();
                        mq.Send(string.Format(smsUrl, smsinfo.Mobile, smsinfo.Body, myTransaction));
                        myTransaction.Commit();
                    }
                    catch (Exception ex)
                    {
                        myTransaction.Abort();
                        WriteLog(ex.ToString());
                    }

                }
            }

            /// <summary>
            /// 发送短信
            /// </summary>
            /// <param name="mqName"></param>
            public void SendSMS(string mqName)
            {
                MessageQueue mq;
                string strResult = string.Empty;
                if (MessageQueue.Exists(@".\private$\" + mqName))
                {
                    mq = new MessageQueue(@".\private$\" + mqName);
                    if (mq.Transactional)
                    {
                        Message[] messages = mq.GetAllMessages();
                        foreach (Message message1 in messages)
                        {
                            MessageQueueTransaction myTransaction = new MessageQueueTransaction();
                            try
                            {
                                myTransaction.Begin();
                                System.Messaging.Message message = mq.Receive(myTransaction);
                                message.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });                           
                                myTransaction.Commit();
                            }
                            catch (Exception ex)
                            {
                                myTransaction.Abort();
                                WriteLog("发送失败。返回值:" + ex.ToString() + "。");
                            }
                        }
                    }
                }
            }      

            #region Write System Log
            public static void WriteLog(string ex)
            {
                if (!EventLog.SourceExists("MessageQueue"))
                {
                    EventLog.CreateEventSource("MessageQueue", "MessageQueueLog");
                }
                EventLog.WriteEntry("MessageQueue", ex.ToString(), EventLogEntryType.Error);

            }
            #endregion
        }
    }

    0
    0
  • 相关阅读:
    springboot+mybatis集成多数据源MySQL/Oracle/SqlServer
    向Spring容器中注册组件的方法汇总小结
    使用spring initialization创建SpringBoot项目
    mybatis-generator 的坑我都走了一遍
    初识Jsp,JavaBean,Servlet以及一个简单mvc模式的登录界面
    【Linux】用户只显示$问题
    【Linux】Ubuntu创建用户、删除用户、设置用户密码,root和普通用户切换
    【linux】ubuntu安装ssh
    【整理】【JS】map的基本操作
    【整理】【JS】数组定义、添加、删除、替换、遍历基本操作
  • 原文地址:https://www.cnblogs.com/aaa6818162/p/1535091.html
Copyright © 2011-2022 走看看