标签:
本篇文章主要实现了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("消息生产者未被识别..."); } } }
/// <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 }
标签:
原文地址:http://www.cnblogs.com/lori/p/4604365.html