/// <summary> /// 单个消息处理 /// </summary> /// <param name="message"></param> private static void SingleDataHandler(RMqSendMessage message) { if (LongRunRabbitProducer.IsAlive()) { bool result = LongRunRabbitProducer.SendMessage(message, ex => { NLogHelper.Error($"id={message.Id}的消息处理失败:{ex}"); }); if (result) { } else { } } else { NLogHelper.Error($"mq连接异常,消息id={message.Id}的消息发送失败"); } }
/// <summary> /// 关闭定时器 /// </summary> public static void Stop() { try { if (_mqTimer != null) { _mqTimer.Dispose(); _mqTimer = null; } } catch { // ignored } //关闭消息处理 RMqProducerMessageHandler.Stop(); //关闭MQ LongRunRabbitProducer.Stop(); }
/// <summary> /// 列表发送 /// </summary> /// <param name="msgList"></param> private static void DataListHandler(List <RMqSendMessage> msgList) { if (LongRunRabbitProducer.IsAlive()) { bool result = LongRunRabbitProducer.SendMessage(msgList, ex => { NLogHelper.Error($"{msgList.Count}条消息处理失败:{ex}"); }); if (result) { } else { } } else { NLogHelper.Error($"mq连接异常,{msgList.Count}条消息发送失败"); } }
/// <summary> /// MQ回调 /// </summary> /// <param name="state"></param> private static void MqTimerCallBack(object state) { lock (Slocker) { if (_isRunning) { return; } _isRunning = true; } try { if (!LongRunRabbitProducer.IsAlive()) { Thread.Sleep(5500); //延迟,使其能够自动恢复 if (!LongRunRabbitProducer.IsAlive()) { if (_isFirstTimeToStartMq) { _isFirstTimeToStartMq = false; NLogHelper.Debug("首次尝试启动MQConsumer"); LongRunRabbitProducer.Start(ex => { NLogHelper.Error($"开启MQ失败:{ex}"); }); } else { _restartCount++; if (_restartCount > 10000000) { _restartCount = 10000; } NLogHelper.Warn($"MQ异常,第{_restartCount}次尝试重启 MQConsumer"); LongRunRabbitProducer.Start(ex => { NLogHelper.Error($"开启MQ失败:{ex}"); }); } } } else { NLogHelper.Trace("检测MQ连接正常"); } } catch (Exception ex) { NLogHelper.Error("MQ定时器异常:" + ex); } finally { lock (Slocker) { _isRunning = false; } } }