private void RepeatSaveMessage(object dataBuffer) { IDataBuffer buffer = dataBuffer as IDataBuffer; if (buffer != null && buffer.Count > 0) { List <object> list = buffer.GetList <object>(); foreach (object obj in list) { bool ret = false; string key = ""; if (obj is NotifyMessage) { key = ((NotifyMessage)obj).MessageId; ret = _notifyMessageBus.Save((NotifyMessage)obj); } else if (obj is PushMessageModel) { key = ((PushMessageModel)obj).PushId; ret = _pushMessageBus.Save((PushMessageModel)obj); } int count = m_repeatSaveTimes[key]; if (!ret) { if (count <= 5) { m_repeatSaveTimes[key] = count + 1; _repeatSavePool.Write(obj); } else { lock (m_repeatSaveTimesLockObj) { m_repeatSaveTimes.Remove(key); } } } else { lock (m_repeatSaveTimesLockObj) { m_repeatSaveTimes.Remove(key); } } Process.Debug(key, "消息补偿保存记录", "RepeatSaveMessage", key, string.Format("保存次数:【{0}】,更新结果:【{1}】", count + 1, ret), ""); Thread.Sleep(100); } } }
private void DirectSaveNotifyMessage(List <NotifyMessage> messageList) { ThreadPool.QueueUserWorkItem(x => { bool ret = false; object[] arr = (object[])x; List <NotifyMessage> list = (List <NotifyMessage>)arr[0]; IDataBufferPool pool = (IDataBufferPool)arr[1]; object lockObj = arr[2]; Dictionary <string, int> repeatSaveTimes = (Dictionary <string, int>)arr[3]; try { ret = _notifyMessageBus.Save(list); Process.Debug("保存消息通知", "_notifyMessageBus.Save", string.Format("结果:【{0}】,条数:【{1}】", ret, list.Count), ""); } catch (Exception ex) { Process.Error("保存消息通知", "_notifyMessageBus.Save", string.Format("结果:【{0}】,条数:【{1}】,异常:【{2}】", ret, list.Count, ex.GetString()), ""); } if (!ret) { list.ForEach(a => { pool.Write(a); lock (lockObj) { repeatSaveTimes[a.MessageId] = 1; } }); } }, new object[] { messageList, _repeatSavePool, m_repeatSaveTimesLockObj, m_repeatSaveTimes }); }
private void RepeatRescovryLog(object state) { try { string[] filenameArr = Directory.GetFiles(_failedLogMessageStorePath, "*.json"); if (filenameArr != null && filenameArr.Length > 0) { foreach (string filename in filenameArr) { bool isHandle = filename.Substring(filename.Length - 6, 1) == "1" ? true : false; DataTable table = Newtonsoft.Json.JsonConvert.DeserializeObject <DataTable>(File.ReadAllText(filename)); string key = table.TableName; int count = m_repeatBatchSaveTimes[key]; string errMsg = ""; bool ret = InsertInternal(table, isHandle, out errMsg) > 0; Logger.Info(string.Format("从备份文件补偿保存日志结果:【{0}】,errMsg:{1}", ret, errMsg)); if (ret) { DeleteBackFile(table.TableName, isHandle); } Thread.Sleep(10); } } } catch (Exception ex) { Logger.Info(string.Format("从备份文件补偿保存日志产生异常:{0}", GetString(ex))); } finally { _repeatRescovryDataPool.Write(1); } }
public static void Process(this LogMessage logMessage) { ThreadPool.QueueUserWorkItem(x => { Interlocked.Increment(ref LogProcessCount); try { m_logProcessPool.Write(logMessage); } catch (Exception ex) { try { if (logMessage != null) { logMessage.Content = string.Format("ex: {0}, log: {1}", ex.GetString(), logMessage.Content); } InsertLog(logMessage, false); m_localLog.Error(ex); } catch (Exception) { // ignored } } Interlocked.Decrement(ref LogProcessCount); }); }
public LogMessageDAL() { _repeatBatchSaveHandlePool = new DataBufferPool(new WaitCallback(RepeatBatchSaveHandleLog), 10, false); _repeatBatchSaveProcessPool = new DataBufferPool(new WaitCallback(RepeatBatchSaveProcessLog), 12, false); #if APPSERVER _repeatRescovryDataPool = new DataBufferPool(new WaitCallback(RepeatRescovryLog), 60, false); _repeatRescovryDataPool.Write(1); #endif }
private void SavePushMessage(List <PushMessageModel> messageList) { if (_savePushPool != null) { messageList.ForEach(x => _savePushPool.Write(x)); } else { DirectSavePushMessage(messageList); } }
private void SaveNotifyMessage(NotifyMessage message) { if (_saveNotifyPool != null) { _saveNotifyPool.Write(message); } else { DirectSaveNotifyMessage(message); } }
public int Insert(DataTable table, bool isHandle) { int count = InsertInternal(table, isHandle); if (count == 0) { lock (m_repeatBatchSaveTimesLockObj) { m_repeatBatchSaveTimes[table.TableName] = 1; } if (isHandle) { _repeatBatchSaveHandlePool.Write(table); } else { _repeatBatchSaveProcessPool.Write(table); } } return(count); }
/// <summary> /// IAppLoggger.LogMessage 实现. /// </summary> /// <param name="objMessage">消息对象</param> public void LogMessage(object objMessage) { int count = 0; LogMessage objActualMsg = (LogMessage)objMessage; if (objActualMsg.IsHandle) { m_handlePool.Write(objActualMsg); } else { m_processPool.Write(objActualMsg); } }
public int Insert(DataTable table, bool isHandle, bool hasSendToMQWhileFailed) { string errMsg = ""; int count = InsertInternal(table, isHandle, out errMsg); if (count == 0) { lock (m_repeatBatchSaveTimesLockObj) { m_repeatBatchSaveTimes[table.TableName] = 1; } if (isHandle) { _repeatBatchSaveHandlePool.Write(table); } else { _repeatBatchSaveProcessPool.Write(table); } Logger.Fatal(string.Format("往{0}表插入数据出现严重错误,影响数据{1}条,出现错误:{2}", table.TableName, table.Rows.Count, errMsg)); } return(count); }
public void SendPushMessage(PushMessage message, bool isWriteToBuffer, out bool isSended) { isSended = IsSending(message); if (!isSended) { if (isWriteToBuffer) { _sendMessagePool.Write(message); } else { while (m_sendMessageCDE.CurrentCount > 1000) { Process.Info(message.PushId, "消息通知单条发送接口", "SendPushMessage", "", "m_sendMessageCDE:" + m_sendMessageCDE.CurrentCount.ToString(), "阻塞"); Thread.Sleep(100); } ThreadPool.QueueUserWorkItem((o) => { try { Process.Info(message.PushId, "消息通知单条发送接口", "SendPushMessage", "", string.Format("批次【{0}】,数量【{1}】", ++callCount, 1), ""); m_sendMessageCDE.TryAddCount(1); m_client.Receive(message); if (m_sendMessageCDE.CurrentCount > 0) { m_sendMessageCDE.TryAddCount(-1); } Process.Info(message.PushId, "消息通知单条发送接口_返回", "SendPushMessage", "", string.Format("批次【{0}】,数量【{1}】", ++callCount, 1), ""); } catch (Exception ex) { if (m_sendMessageCDE.CurrentCount > 0) { m_sendMessageCDE.TryAddCount(-1); } if (m_client.State == CommunicationState.Closed || m_client.State == CommunicationState.Faulted) { m_client = new ServiceAgent.SenderSendServiceSOA.SendServiceClient(); } //如果推送异常,去掉标示的推送缓存 string cacheKey = string.Format(CacheKeys.PushMessageToSender_Arg2, message.PushId, message.PushCount); DistributedCache.Delete(cacheKey); Process.Error(message.PushId, "消息通知单条发送接口", "SendPushMessage", "", "异步发送推送消息到消息通知接口出现异常:" + ex.GetString(), "Error"); } }); } } }
public PushMessageResult Send(PushMessage message) { Process.Debug(message.PushId, "消息发送中心", "Send", message.MessageId, string.Format("消息内容:{0}", JsonConvert.SerializeObject(message)), ""); Check.IsNull(message, "PushMessage为空"); bool isSendMessage = true; switch (message.MessagePriority) { case MessagePriorityEnum.High: if (_highPool != null) { _highPool.Write(message); } else { isSendMessage = false; } break; case MessagePriorityEnum.Middle: if (_middlePool != null) { _middlePool.Write(message); } else { isSendMessage = false; } break; case MessagePriorityEnum.Normal: if (_normalPool != null) { _normalPool.Write(message); } else { isSendMessage = false; } break; case MessagePriorityEnum.Low: if (_lowPool != null) { _lowPool.Write(message); } else { isSendMessage = false; } break; } if (!isSendMessage) { Send(message, Callback); } PushMessageResult response = new PushMessageResult(); response.Success = true; return(response); }
private void CallbackInternal(PushMessageResult result) { Check.IsNull(result, "CallbackInternal的参数Obj"); PushMessageModel model = new PushMessageModel(); Check.IsNull(result.PushId, "PushMessageResult.PushId"); PushMessage pushMsg = result.ReferObject as PushMessage; Check.IsNull(pushMsg, "PushId查找到的PushMessageModel对象"); model.PushCount = pushMsg.PushCount; model.SettingId = pushMsg.SettingId; RequestProfile.RequestKey = result.PushId + "_" + pushMsg.PushCount.ToString(); RequestProfile.MessageKey = result.PushId; switch (result.PushStatus) { case PushResultEnum.Abort: model.PushId = result.PushId; model.PushStatus = PushStatusEnum.Abort; model.PushCount++; model.Memo = string.Format("Abort:{0},{1:yyyy-MM-dd HH:mm:ss};{2}", result.ErrMsg, DateTime.Now, Environment.NewLine); break; case PushResultEnum.Success: model.PushId = result.PushId; model.PushStatus = PushStatusEnum.Pushed; model.PushCount++; model.Memo = string.Format("Success:{0},{1:yyyy-MM-dd HH:mm:ss};{2}", result.ErrMsg, DateTime.Now, Environment.NewLine); break; case PushResultEnum.Failed: model.PushId = result.PushId; model.PushStatus = PushStatusEnum.Failed; bool isPushNext = false; //model.PushCount == 3 //第四次推送的返回 model.PushCount++; model.NextPushTime = ComputePushTimeBusiness.ComputeNextPushTime(model, out isPushNext); if (isPushNext) { model.PushStatus = PushStatusEnum.UnPush; } model.Memo = string.Format("Failed:{0},B-{1:yyyy-MM-dd HH:mm:ss},N-{2:yyyy-MM-dd HH:mm:ss};{3}", result.ErrMsg, DateTime.Now, model.NextPushTime, Environment.NewLine); break; case PushResultEnum.Pushing: model.PushId = result.PushId; model.PushStatus = PushStatusEnum.Pushing; model.Memo = string.Format("Pushing:{0},{1:yyyy-MM-dd HH:mm:ss};{2}", result.ErrMsg, DateTime.Now, Environment.NewLine); break; default: break; } if (!string.IsNullOrWhiteSpace(model.PushId) && (model.PushStatus & (PushStatusEnum.Abort | PushStatusEnum.Pushed | PushStatusEnum.Failed | PushStatusEnum.UnPush | PushStatusEnum.Pushing)) == model.PushStatus) { model.LastModifyTime = DateTime.Now; bool ret = _pushMessageBus.Edit(model); if (!ret) { lock (m_repeatUpdateTimesLockObj) { //进入补偿更新队列 m_repeatUpdateTimes[model.PushId] = 1; } _repeatUpdatePool.Write(model); } else { if (model.PushStatus == PushStatusEnum.UnPush) { string cacheKey = string.Format(CacheKeys.SendToMessageHandler_Arg2, model.PushId, model.PushCount - 1); DistributedCache.Delete(cacheKey); } } Process.Debug(result.PushId, "消息推送记录", "Callback", result.PushId, string.Format("推送状态:【{0}】,推送结果:【{1}】,更新结果:【{2}】", result.PushStatus.ToString(), result.ErrMsg, ret), ""); } }
public NotifyMessageResult Receive(NotifyMessage message) { Check.IsNull(message, "NotifyMessage为空"); bool isSendMessage = true; switch (message.MessagePriority) { case MessagePriorityEnum.High: if (_highPool != null) { _highPool.Write(message); } else { isSendMessage = false; } break; case MessagePriorityEnum.Middle: if (_middlePool != null) { _middlePool.Write(message); } else { isSendMessage = false; } break; case MessagePriorityEnum.Normal: if (_normalPool != null) { _normalPool.Write(message); } else { isSendMessage = false; } break; case MessagePriorityEnum.Low: if (_lowPool != null) { _lowPool.Write(message); } else { isSendMessage = false; } break; } if (!isSendMessage) { Build(message); } NotifyMessageResult response = new NotifyMessageResult(); response.Success = true; return(response); }
private static void AddDataToBuffer() { m_dataBufferPool.Write((byte)1); }
private void GetScanMessageList(object setting) { string ikey = Guid.NewGuid().ToString(); ScanServiceSetting.ScanSetting scanSetting = (ScanServiceSetting.ScanSetting)setting; Process.Debug("m_scanConditionDic", "m_scanConditionDic", string.Format("scanSetting:【{0}】", scanSetting.ScanCount), ""); int scanIntervalTime = scanSetting.IntervalTime * (-1); ScanOrderCondition con = new ScanOrderCondition(); con.OrderBy = scanSetting.OrderBy; con.ScanOrderIdInit = scanSetting.ScanOrderIdInit; con.PageSize = scanSetting.ScanCount; con.Status = scanSetting.OrderStatus; con.Includes = string.Join(",", scanSetting.Include.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)); DateTime endTime = GetDateTimeNow(ikey); con.StartTime = endTime.AddSeconds(scanIntervalTime); con.EndTime = endTime; Handle.Debug(ikey, "订单扫描开始", "GetScanMessageList", scanSetting.MessType, string.Format("条件参数【{0}】,订单扫描初开始,间隔:{1}", JsonConvert.SerializeObject(con), scanIntervalTime.ToString()), ""); try { System.Diagnostics.Stopwatch execTime = new System.Diagnostics.Stopwatch(); while (true) { execTime.Restart(); ikey = Guid.NewGuid().ToString(); List <NotifyOrderEntity> orderList = GetOrdersList(con); if (orderList != null && orderList.Count > 0) { foreach (NotifyOrderEntity orderEnt in orderList) { if (!IsSendingDataCache(orderEnt, scanSetting.MessType)) { string errMsg = ""; NotifyMessage message = new NotifyMessage(); message.MessageKey = SetMessageKey(orderEnt, scanSetting.MessType); message.SourceFrom = "OrderScan手动扫描"; message.AppId = ConfigurationAppSetting.AppId; string data = string.Format("orderno={0}&OutTime={1}&SalesmanID={2}&ProviderID={3}&ProxyerID={4}", orderEnt.OrderNo, orderEnt.OutTime, orderEnt.SalesmanID, orderEnt.ProviderID, orderEnt.ProxyerID); message.NotifyData = data; message.MessagePriority = JinRi.Notify.Model.MessagePriorityEnum.None; message.MessageType = scanSetting.MessType; DateTime dt = DateTime.Now; bool ret = SendOrder(message, out errMsg); Double spendTime = (DateTime.Now - dt).TotalMilliseconds; Process.Debug(ikey, "订单扫描记录", "GetScanMessageList", orderEnt.OrderNo, string.Format("订单扫描记录:发送时间【{0}】,调用耗时【{1}】ms,返回结果:【{2}】", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), spendTime, errMsg), "发送"); if (!ret) { lock (m_repeatNotifyTimesLockObj) { //进入补偿推送队列 m_repeatNotifyTimes[orderEnt.OrderNo] = 1; } _repeatNotifyPool.Write(message); } } Process.Debug(ikey, "订单扫描记录", "GetScanMessageList", orderEnt.OrderNo, "订单扫描记录", "订单"); } } execTime.Stop(); Process.Debug(ikey, "订单扫描记录", "GetScanMessageList", scanSetting.MessType, string.Format("扫描开始时间:【{0}】,扫描截止时间:【{1}】,扫描条数【{2}】,执行时间【{3}】ms", con.StartTime, con.EndTime, orderList.Count, execTime.ElapsedMilliseconds), ""); int sleep = (int)execTime.ElapsedMilliseconds; if (scanSetting.IdleSleepTime > sleep) { Thread.Sleep(scanSetting.IdleSleepTime - sleep); } con.StartTime = endTime.AddSeconds(scanIntervalTime); con.EndTime = GetDateTimeNow(ikey); endTime = con.EndTime; TimeSpan timeSpan = con.EndTime - DateTime.Now; double sleepTime = timeSpan.TotalMilliseconds; if (sleepTime > 0) { Process.Debug(ikey, "订单扫描记录", "GetScanMessageList", scanSetting.MessType, string.Format("休眠时间【{0}】", sleepTime.ToString(), "")); Thread.Sleep((int)sleepTime); } } } catch (Exception ex) { Process.Fatal(ikey, "订单扫描记录", "GetScanMessageList", scanSetting.MessType, string.Format("重扫线程异常结束ex【{2}】,扫描开始时间:【{0}】,扫描截止时间:【{1}】", con.StartTime.ToString("yyyy-MM-dd HH:mm:ss"), con.EndTime.ToString("yyyy-MM-dd HH:mm:ss"), ex.ToString()), ""); ThreadPool.QueueUserWorkItem(x => { try { Scan(); } catch { } } ); } }
public NotifyMessageResult Receive(NotifyMessage message) { Check.IsNull(message, "NotifyMessage为空"); bool isSendToBuffer = false; bool isDirectSendMessage = false; InitNotifyMessage(message); Process.Debug(message.MessageId, "消息接收中心", "Receive", message.MessageKey, string.Format("消息接收内容:{0}", JsonConvert.SerializeObject(message)), ""); NotifyMessageResult response; string errMsg = ""; if (!Valid(message, out errMsg)) { response = new NotifyMessageResult(); response.Success = false; response.ErrMsg = errMsg; return(response); } if (ReceiveServiceSetting.EnableJudgeHasReceived && HasReceived(message)) { Process.Debug(message.MessageId, "消息接收中心", "Receive", message.MessageKey, string.Format("此类型消息已经接收过了,消息接收内容:{0}", JsonConvert.SerializeObject(message)), ""); response = new NotifyMessageResult(); response.Success = true; response.ErrMsg = string.Format("此类型消息已经接收过了,MessageId: {0}", message.MessageId); return(response); } if (ReceiveServiceSetting.SystemStatus == SystemStatusEnum.Stopping || ReceiveServiceSetting.SystemStatus == SystemStatusEnum.Stopped) { SendToMQ(message); } else { switch (message.MessagePriority) { case MessagePriorityEnum.High: isDirectSendMessage = ReceiveServiceSetting.IsDirectRouteHighToBuilderService; if (_highPool != null) { isSendToBuffer = true; } break; case MessagePriorityEnum.Middle: isDirectSendMessage = ReceiveServiceSetting.IsDirectRouteMiddleToBuilderService; if (_middlePool != null) { isSendToBuffer = true; } break; case MessagePriorityEnum.Normal: isDirectSendMessage = ReceiveServiceSetting.IsDirectRouteNormalToBuilderService; if (_normalPool != null) { isSendToBuffer = true; } break; case MessagePriorityEnum.Low: isDirectSendMessage = ReceiveServiceSetting.IsDirectRouteLowToBuilderService; if (_lowPool != null) { isSendToBuffer = true; } break; } if (!isDirectSendMessage) { if (isSendToBuffer) { switch (message.MessagePriority) { case MessagePriorityEnum.High: _highPool.Write(message); break; case MessagePriorityEnum.Middle: _middlePool.Write(message); break; case MessagePriorityEnum.Normal: _normalPool.Write(message); break; case MessagePriorityEnum.Low: _lowPool.Write(message); break; } } else { SendToMQ(message); } } else { SendToBuilderService(message); } } response = new NotifyMessageResult(); response.Success = true; response.ErrMsg = string.Format("消息接收成功,MessageId:{0}", message.MessageId); return(response); }