public void Vistor(QuqueName topicName, MqCommandInfo commandInfo) { try { if (commandInfo.MessageType != NMSMessageType.SyncByTableName || string.IsNullOrEmpty(commandInfo.Body)) { return; } var content = JsonUtils.Deserialize <SyncTableContent>(commandInfo.Body); if (content == null || !content.HasData) { _logger.Error(string.Format("内容解析无效 StoreId: {0} Body:{1}", commandInfo.StoreId, commandInfo.Body)); return; } //TODO 上报数据插入数据库 InsertOrUpdate(content); } catch (Exception ex) { //错误处理,缓存到本地文件中 _cacheManager.AddOnlyFileCache(Guid.NewGuid(), commandInfo); _logger.Error(ex, "Vistor"); } }
public void Vistor(QuqueName queueName, MqCommandInfo commandInfo) { if (queueName != QuqueName.ClientHeart && commandInfo.MessageType != NMSMessageType.Heart && !commandInfo.Properties.ContainsKey(MqConsts.MqDevice)) { return; } var deviceType = commandInfo.Properties[MqConsts.MqDevice]; LoggerFactory.GetLog().Error(string.Format("{0}设备心跳包处理", deviceType)); }
private void CreateConsumer(IConnection connection, QuqueName ququeName) { //通过连接创建一个会话 ISession session = connection.CreateSession(); //通过会话创建一个消费者,这里就是Queue这种会话类型的监听参数设置 string whereClause = string.Format("{0}='{1}' OR {2}='0'", MqConsts.MqInstanceId, _configInfo.MqInstanceId, MqConsts.MqInstanceAll); IMessageConsumer consumer = session.CreateConsumer(new ActiveMQQueue(GetQueueName(ququeName)), whereClause); //注册监听事件 consumer.Listener += message => { ConsumerOnListener(ququeName, message); }; _logger.Debug(string.Format("消费者创建:{0} IP:{1}:{2} where:{3}", ququeName, _configInfo.IpAddress, _configInfo.Port, whereClause)); }
private MsgDeliveryMode GetMsgDeliveryMode(QuqueName command) { MsgDeliveryMode mode = MsgDeliveryMode.NonPersistent; switch (command) { case QuqueName.BusinessProcessing: mode = MsgDeliveryMode.Persistent; break; } return(mode); }
/// <summary> /// /// </summary> /// <param name="arg1"></param> /// <param name="arg2"></param> protected virtual void OnQuqueDispatch(QuqueName arg1, MqCommandInfo arg2) { var handler = QuqueDispatch; if (handler != null) { handler(arg1, arg2); } else { _logger.Error("未找到队列消息分配处理程序"); } }
private MsgPriority GetMsgPriority(QuqueName command) { MsgPriority priority = MsgPriority.Normal; switch (command) { case QuqueName.BusinessProcessing: priority = MsgPriority.High; break; case QuqueName.ClientHeart: case QuqueName.StoreReportData: priority = MsgPriority.Normal; break; } return(priority); }
private void ServiceOnQuqueDispatch(QuqueName ququeName, MqCommandInfo commandInfo) { foreach (var mqCommandTopicVistor in _queueVistors) { commandInfo.ExcuteCount++; mqCommandTopicVistor.Vistor(ququeName, commandInfo); } ExcuteInfo info; _excuteInfoDictionary.TryGetValue(commandInfo.ClientId, out info); if (info != null) { lock (LockExcuteInfoObj) { info.TotalCount++; info.TotalSize += commandInfo.Length; } } }
public void Vistor(QuqueName queueName, MqCommandInfo commandInfo) { if (queueName != QuqueName.BusinessProcessing && commandInfo.MessageType != NMSMessageType.None && !commandInfo.Properties.ContainsKey(MqConsts.MqDevice)) { return; } var deviceType = TypeParse.StrToInt(commandInfo.Properties[MqConsts.MqAppType]); if (deviceType == 0) { //不是有效的app类型 return; } var data = JsonUtils.Deserialize <MqDeviceRegisterInfo>(commandInfo.Body); if (data == null) { return; } //App心跳 //_deviceAccess.Exists(string.Format("StoreId='{0}' AND ", commandInfo.StoreId)); }
public void Vistor(QuqueName queueName, MqCommandInfo commandInfo) { if (queueName != QuqueName.ClientHeart && commandInfo.MessageType != NMSMessageType.Heart && !commandInfo.Properties.ContainsKey(MqConsts.MqAppType)) { return; } var appType = Utils.GetEnum <AppType>(commandInfo.Properties[MqConsts.MqAppType]); if (appType == AppType.Empty) { //不是有效的app类型 return; } var data = JsonUtils.Deserialize <MqHeartbeatInfo>(commandInfo.Body); if (data == null) { return; } //App心跳 _listenerService.Heartbeat(commandInfo.StoreId, appType, data); }
/// <summary> /// 客户端为消息生成者 /// </summary> /// <param name="name"></param> /// <returns></returns> public string GetQueueName(QuqueName name) { return("tcsoft.client." + name.ToString().ToLower()); }
private void ConsumerOnListener(QuqueName ququeName, IMessage message) { try { string msgContext = ""; if (message is ActiveMQBytesMessage) { bool isZipCompress = false; if (message.Properties.Contains(MqConsts.IsZipCompress)) { isZipCompress = message.Properties.GetBool(MqConsts.IsZipCompress); } ActiveMQBytesMessage msg = (ActiveMQBytesMessage)message; if (isZipCompress) { msgContext = StringZipHelper.GZipDecompress(msg.Content); } else { msgContext = System.Text.Encoding.UTF8.GetString(msg.Content); } } else if (message is ActiveMQTextMessage) { ActiveMQTextMessage msg = (ActiveMQTextMessage)message; msgContext = msg.Text; } else { _logger.Error("消费者监听事件处理错误.暂时只接受ActiveMQBytesMessage、ActiveMQTextMessage消息体"); } var clientId = GetClientId(message); if (clientId == Guid.Empty || clientId == null) { _logger.Error(string.Format("消息无效,没有匹配的门店与命令号 ")); return; } var msgType = message.NMSType; NMSMessageType messageType = NMSMessageType.None; if (!string.IsNullOrEmpty(msgType)) { messageType = Utils.GetEnum <NMSMessageType>(msgType); } MqCommandInfo commandInfo = new MqCommandInfo() { StoreId = clientId.Value, Body = msgContext, Length = msgContext.Length, MessageType = messageType, NMSMessageId = message.NMSMessageId, MqInstancId = _configInfo.MqInstanceId, ClientId = this.ClientId, Properties = new Dictionary <string, string>() }; if (message.Properties != null) { foreach (var key in message.Properties.Keys) { if (key == null) { continue; } var keystr = key.ToString(); if (!commandInfo.Properties.ContainsKey(keystr)) { commandInfo.Properties.Add(keystr, message.Properties.GetString(keystr)); } } } OnQuqueDispatch(ququeName, commandInfo); if (commandInfo.ExcuteCount == 0) { _logger.Error(string.Format("消息未找到处理程序 类型: {0}大小: {1}", commandInfo.MessageType, commandInfo.Length)); } } catch (Exception ex) { _logger.Error(ex, "消费者监听事件处理错误.ConsumerOnListener"); } }
/// <summary> /// /// </summary> /// <param name="isTopic"></param> /// <param name="clientId"></param> /// <param name="command"></param> /// <param name="content"></param> /// <param name="timeSpan"></param> /// <param name="propertydic"></param> /// <returns></returns> private bool SendMessage(bool isTopic, Guid clientId, QuqueName command, TopicName topicName, object content, TimeSpan timeSpan, NMSMessageType?messageType = null, Dictionary <string, string> propertydic = null) { var mqClientId = clientId.ToString().ToLower(); if (propertydic == null) { propertydic = new Dictionary <string, string>(); } //设置消息对象的属性,这个很重要哦,是Queue的过滤条件,也是P2P消息的唯一指定属性 if (propertydic.ContainsKey(MqConsts.ClientId)) { propertydic[MqConsts.ClientId] = mqClientId; } else { propertydic.Add(MqConsts.ClientId, mqClientId); } var json = ""; try { if (content != null) { if (!(content is string)) { json = JsonUtils.SerializeObject(content); } else { json = content as string; } } int mqInstanceId = 0; var connection = _poolConnectionManager.GetPoolConnection(clientId, out mqInstanceId); //通过连接创建Session会话 using (ISession session = connection.CreateSession()) { //通过会话创建生产者,方法里面new出来的是MQ中的Queue IMessageProducer prod = isTopic ? session.CreateProducer(new Apache.NMS.ActiveMQ.Commands.ActiveMQTopic(GetTopicName(topicName))) : session.CreateProducer(new Apache.NMS.ActiveMQ.Commands.ActiveMQQueue(GetQueueName(command))); //创建一个发送的消息对象 ITextMessage message = prod.CreateTextMessage(json); if (messageType.HasValue && messageType.Value != NMSMessageType.None) { message.NMSType = messageType.ToString(); } foreach (var pair in propertydic) { if (string.IsNullOrEmpty(pair.Key) || string.IsNullOrEmpty(pair.Value) ) { continue; } message.Properties.SetString(pair.Key, pair.Value); } //生产者把消息发送出去,几个枚举参数MsgDeliveryMode是否长链,MsgPriority消息优先级别,发送最小单位,当然还有其他重载 if (isTopic) { prod.Send(message, MsgDeliveryMode.Persistent, MsgPriority.Normal, timeSpan); } else { prod.Send(message, GetMsgDeliveryMode(command), GetMsgPriority(command), timeSpan); } return(true); } } catch (Exception ex) { _logger.Error(ex, string.Format("{0}SendMessage isTopic:{1} storeId:{2} command:{3} json:{4}", this.GetType(), isTopic, clientId, command, json)); } return(false); }
/// <summary> /// 某人消息队列有效期1天 /// </summary> /// <param name="clientId"></param> /// <param name="command"></param> /// <param name="content"></param> public bool SendQueueMessage(Guid clientId, QuqueName command, object content, NMSMessageType?messageType = null) { return(SendQueueMessage(clientId, command, content, TimeSpan.FromDays(1), messageType)); }
/// <summary> /// /// </summary> /// <param name="clientId"></param> /// <param name="command"></param> /// <param name="content"></param> /// <param name="timeSpan"></param> /// <param name="propertydic"></param> public bool SendQueueMessage(Guid clientId, QuqueName command, object content, TimeSpan timeSpan, NMSMessageType?messageType = null, Dictionary <string, string> propertydic = null) { return(SendMessage(false, clientId, command, TopicName.PushMessage, content, timeSpan, messageType, propertydic)); }