我心中的核心组件(可插拔的AOP)~消息组件~完善篇

回到目录

为什么要有本篇文章

本篇文章主要实现了RTX消息生产者,并且完成了整体的设计方式,之前在设计时消息生产者全局使用单一的生产方式,即一个项目里使用了Email就不能使用SMS,这种设计方法和实际不相符,虽然在性能上是最佳的(采用了单例模型,维护一个生产者,使用策略模式进行生产者的实现,使用工厂模式生产具体生产者),实际项目中,应该是可以自动选择消息生产者的,当然为了程序的性能,我们还必须使用单例模式来生产具体生产者,这种单例,在本程序中,采用了具体类型,具体单例的方法,即Email自己维护单例,SMS自己维护单例,这样,程序即能满足实际的需要,又可以提高自己的性能!

第五回  消息组件

第六回  消息组件~续

强大的设计

强大的生产者

复制代码
  /// <summary>
    /// 消息生产者
    /// 具体消息生产者是单例,如Email,SMS,Rtx等
    /// </summary>
    public class MessageFactory
    {
        /// <summary>
        /// 消息工厂
        /// </summary>
        public static IMessageManager GetService(MessageType messageType)
        {
            switch (messageType)
            {
                case MessageType.Email:
                    return EmailMessageManager.Instance;
                case MessageType.SMS:
                    return SMSMessageManager.Instance;
                case MessageType.RTX:
                    return RTXMessageManager.Instance;
                default:
                    throw new NotImplementedException("消息生产者未被识别...");
            }
        }

    }
复制代码

RTX生产者的实现

复制代码
   /// <summary>
    /// RTX消息服务
    /// </summary>
    internal class RTXMessageManager : IMessageManager
    {
        #region Singleton
        private static object lockObj = new object();
        public static RTXMessageManager Instance;
        static RTXMessageManager()
        {
            lock (lockObj)
            {
                if (Instance == null)
                    Instance = new RTXMessageManager();
            }
        }
        private RTXMessageManager()
        { }
        #endregion

        private string EncodingString(string str)
        {
            return HttpUtility.UrlEncode(str, Encoding.GetEncoding("GB2312"));
        }

        private string rtxUrl = System.Configuration.ConfigurationManager.AppSettings["RtxApi"] ??
            "http://192.168.1.8:8012/sendnotifynew.cgi?";

        #region IMessageManager 成员

        public void Send(string recipient, string subject, string body)
        {
            Send(new List<string> { recipient }, subject, body);
        }

        public void Send(IEnumerable<string> recipients, string subject, string body)
        {
            try
            {
                var dic = new Dictionary<string, string>()       
                {    {"title",EncodingString(subject)},
                     {"msg",EncodingString(body)},
                     {"receiver", EncodingString(string.Join(",",recipients))},
                     {"delaytime","0"}
                 };
                StringBuilder str = new StringBuilder();
                foreach (var item in dic)
                {
                    str.Append(item.Key + "=" + item.Value + "&");
                }
                new HttpClient().GetAsync(rtxUrl + str.ToString()).Wait();
            }
            catch (Exception ex)
            {
                Logger.Core.LoggerFactory.Instance.Logger_Info(ex.Message);
            }
        }

        public void Send(IEnumerable<string> recipients, string subject, string body, bool isAsync)
        {
            if (isAsync)
                Task.Run(() =>
                {
                    Send(recipients, subject, body);
                });
            else
                Send(recipients, subject, body);
        }

        #endregion
    }
复制代码

 回到目录

posted @   张占岭  阅读(1286)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
历史上的今天:
2012-06-27 爱上MVC3系列~Html.BeginForm与Ajax.BeginForm
2011-06-27 说说Jquery吧之一,$ 是什么?jQuery 是什么?
2011-06-27 今天不说技术,说说中国的十二生肖告诉了我们什么?这就是我们的祖先!
点击右上角即可分享
微信分享提示