public void Subscribe(RabbitMQBus rabbitMQ, MessagePriorityEnum priority, Action <NotifyMessage> handler) { try { string topic = priority.ToString().ToUpper(); rabbitMQ.Bus.Subscribe(QueueDic[topic], (body, props, info) => Task.Factory.StartNew(() => { MetricsKeys.RabbitMQ_Subscribe.MeterMark("Success"); if (DataCache.Add(props.MessageId, 1, DateTime.Now.AddMinutes(10))) { try { if (props.Type == "M") { #if DEBUG System.Diagnostics.Stopwatch watch = System.Diagnostics.Stopwatch.StartNew(); #endif List <NotifyMessage> list = JsonConvert.DeserializeObject <List <NotifyMessage> >(Encoding.UTF8.GetString(body), serializerSettings); for (int i = 0; i < list.Count; i++) { handler(list[i]); } #if DEBUG watch.Stop(); Process.Debug("订阅消息", "Consume", string.Format("数据模式:【{0}】,条数:【{1}】,耗时:【{2}】", props.Type, list.Count, watch.ElapsedMilliseconds), ""); #endif } else if (props.Type == "S") { handler(JsonConvert.DeserializeObject <NotifyMessage>(Encoding.UTF8.GetString(body), serializerSettings)); #if DEBUG Process.Debug("订阅消息", "Consume", string.Format("数据模式:【{0}】,条数:【{1}】", props.Type, 1), ""); #endif } } catch (Exception ex) { m_logger.Error("处理消息发生异常:" + ex.GetString()); } } else { RepeatMessageDic[props.MessageId] = RepeatMessageDic[props.MessageId] + 1; ComsumeMessage(props.MessageId, RepeatMessageDic[props.MessageId]); } })); } catch (Exception ex) { MetricsKeys.RabbitMQ_Subscribe.MeterMark("Error"); m_logger.Error("订阅消息发生异常:" + ex.GetString()); throw new RabbitMQException("订阅消息发生异常", ex); } }
/// <summary> /// Returns a <see cref="System.String"/> that represents this instance. /// </summary> /// <returns> /// A <see cref="System.String"/> that represents this instance. /// </returns> public override string ToString() { StringBuilder sb = new StringBuilder(base.ToString()); sb.Append(", TargetId: ["); sb.Append(mTargetId); sb.Append("], Priority: ["); sb.Append(mPriority.ToString()); sb.Append("], MessageType: ["); sb.Append(mMessageType.ToString()); sb.Append("]"); return(sb.ToString()); }
public void Publisher(RabbitMQBus rabbitMQ, MessagePriorityEnum priority, List <NotifyMessage> messageList) { try { string topic = priority.ToString().ToUpper(); var body = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(messageList, serializerSettings)); MessageProperties prop = new MessageProperties() { AppId = ConfigurationAppSetting.AppId, Priority = (byte)priority, MessageId = messageList[0].MessageId, Type = "M" }; rabbitMQ.Bus.OpenPublishChannel().Publish(ExchangeDic[topic], "", prop, body); MetricsKeys.RabbitMQ_Publish.MeterMark("Success"); } catch (Exception ex) { MetricsKeys.RabbitMQ_Publish.MeterMark("Error"); m_logger.Error("推送消息发生异常:" + ex.GetString()); throw new RabbitMQException("推送消息发生异常", ex); } }
private void Subscribe(RabbitMQBus bus, MessagePriorityEnum priority) { Process.Debug("并行生成消息", "Subscribe", string.Format("开始订阅,消息优先级:【{0}】", priority.ToString()), ""); RabbitMQBusiness.Instance.Subscribe(bus, priority, m_buildBus.Build); }
public List <NotifyMessage> GetList(MessagePriorityEnum mesPrvEnm, string messTypeEnm) { Stopwatch watch = Stopwatch.StartNew(); watch.Start(); List <NotifyMessage> list = new List <NotifyMessage>(); int count = 1000; for (int i = 0; i < count; i++) { NotifyMessage mess = new NotifyMessage() { MessageId = IdentityGenerator.New(), AppId = "11011", MessagePriority = mesPrvEnm, MessageKey = DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + mesPrvEnm.ToString() + "_" + i.ToString(), MessageType = messTypeEnm.ToString(), NotifyData = "a=1&b=2", SourceFrom = "test", ClientIP = "127.0.0.1", CreateTime = DateTime.Now }; list.Add(mess); } watch.Stop(); long min = watch.ElapsedMilliseconds; return(list); }