/// <summary> /// 判断消息是否发送成功 /// </summary> /// <param name="record">消息记录</param> /// <returns>成功:true;失败:false</returns> private static bool send(NotifyRecord record, int times, out DateTime requestTime) { requestTime = DateTime.Now; var notifyResult = Utility.HttpRequestUtility.GetHttpResult(record.Content, _timeout); var responseTime = DateTime.Now; var success = notifyResult == "0"; saveLog(record, requestTime, notifyResult, responseTime, success, times); return(success); }
/// <summary> /// /// </summary> /// <param name="record">消息记录</param> protected void OnNotifyRecordReceived(NotifyRecord record) { if (NotifyRecordReceived != null) { var args = new NotifyRecordEventArgs { Record = record }; NotifyRecordReceived(this, args); } }
/// <summary> /// 处理消息方法 /// </summary> /// <param name="record">消息</param> public void Inject(NotifyRecord record) { if (record == null) { return; } _failedProcessor.Remove(record.Id); DateTime requestTime; if (!send(record, 1, out requestTime)) { _failedProcessor.Add(record, requestTime); } }
/// <summary> /// 将发送消息的情况记录日志,并在控制台打印相应记录 /// </summary> /// <param name="record">消息记录</param> /// <param name="requestTime">请求时间</param> /// <param name="notifyResult">消息内容</param> /// <param name="responseTime">响应时间</param> /// <param name="success">发送是否成功</param> private static void saveLog(NotifyRecord record, DateTime requestTime, string notifyResult, DateTime responseTime, bool success, int times) { var log = new Service.Log.Domain.NotifyLog { OrderId = record.Id, Type = record.Type, Request = record.Content, RequestTime = requestTime, Response = notifyResult, ResponseTime = responseTime, Success = success }; Service.LogService.SaveNotifyLog(log); var message = string.Format("业务单号:{1} 类型:{2}{0}请求时间:{3} 请求次数:{4}{0}请求内容:{5}{0}响应时间:{6} 响应内容:{7}{0}处理状态:{8}", Environment.NewLine, log.OrderId, log.Type, log.RequestTime.ToString("HH:mm:ss"), times, log.Request, log.ResponseTime.ToString("HH:mm:ss"), log.Response, success); Program.Print(message); }
/// <summary> /// 添加发送失败的消息记录,并开启该线程 /// </summary> /// <param name="record">消息记录</param> public void Add(NotifyRecord record, DateTime failedRequestTime) { if (record == null) { return; } lock (_recordLocker) { if (_records.ContainsKey(record.Id)) { return; } var failedRecord = new FailedRecord { Record = record, Times = 1, Container = this, LastTime = failedRequestTime }; _records.Add(record.Id, failedRecord); failedRecord.Start(); } }