public ConsumerHeartbeatProtect(ConsumerContext context) { try { Context = context; DebugHelper.WriteLine(Context.GetMQPathID(), Context.GetMQPath(), "ConsumerHeartbeatProtect", "消费者准备心跳注册"); _lastupdatetimeofmqpath = GetLastUpdateTimeOfMqPath(); DebugHelper.WriteLine(Context.GetMQPathID(), Context.GetMQPath(), "ConsumerHeartbeatProtect", "消费者获取队列最后更新时间成功"); cancelSource = new CancellationTokenSource(); System.Threading.Tasks.Task.Factory.StartNew(() => { HeartBeatRun();//开启心跳检查 }, cancelSource.Token); DebugHelper.WriteLine(Context.GetMQPathID(), Context.GetMQPath(), "ConsumerHeartbeatProtect", "消费者开启心跳循环成功"); redislistener = new RedisNetCommandListener(ConfigHelper.RedisServer); redislistener.Name = "消费者" + context.ConsumerProvider.Client; redislistener.Register((channel, msg) => { RedisListenerCommand(channel, msg); }, cancelSource, Context.ConsumerProvider.MQPath, SystemParamConfig.Redis_Channel); DebugHelper.WriteLine(Context.GetMQPathID(), Context.GetMQPath(), "ConsumerHeartbeatProtect", "消费者开启Redis消息订阅成功"); LogHelper.WriteLine(Context.ConsumerInfo.MQPathModel.id, Context.ConsumerInfo.MQPathModel.mqpath, "ConsumerHeartbeatProtect", "消费者心跳注册成功"); } catch (Exception exp) { ErrorLogHelper.WriteLine(Context.GetMQPathID(), Context.GetMQPath(), "ConsumerHeartbeatProtect", "消费者心跳守护初始化", exp); throw exp; } }
/// <summary> /// 注册监听器 /// </summary> /// <param name="action"></param> /// <param name="maxReceiveMQThread"></param> /// <param name="context"></param> public virtual void Register(Action <MQMessage> action, ConsumerContext context) { DebugHelper.WriteLine(context.GetMQPathID(), context.GetMQPath(), "ReceiveMessageListener", "消费者开始监听器"); context.Listener = this; context.ActionInfo.InnerAction = action; var quque = new ReceiveMessageQuque(context); //注册队列 heartbeat = new ConsumerHeartbeatProtect(context); //注册心跳 DebugHelper.WriteLine(context.GetMQPathID(), context.GetMQPath(), "ReceiveMessageListener", "消费者监听器注册成功"); LogHelper.WriteLine(context.GetMQPathID(), context.GetMQPath(), "ReceiveMessageListener", "消费者监听器注册成功"); }
public void Dispose() { if (Context != null) { try { DebugHelper.WriteLine(Context.GetMQPathID(), Context.GetMQPath(), "Dispose", "开始消费者资源释放"); if (Context != null) { Context.Dispose(); } DebugHelper.WriteLine(Context.GetMQPathID(), Context.GetMQPath(), "Dispose", "消费者上下文资源释放成功"); } catch (Exception exp) { ErrorLogHelper.WriteLine(Context.GetMQPathID(), Context.GetMQPath(), "Dispose", "消费者资源释放出错", exp); throw new BusinessMQException("释放订阅客户端消息处理资源失败", exp); } finally { try { ConsumerBLL bll = new ConsumerBLL(); //取消注册 SqlHelper.ExcuteSql(Config.ManageConnectString, (c) => { bll.RemoveConsumer(c, Context.ConsumerInfo.ConsumerModel.tempid, Context.ConsumerInfo.ConsumerModel.consumerclientid); }); DebugHelper.WriteLine(Context.GetMQPathID(), Context.GetMQPath(), "Dispose", "消费者注销注册信息成功"); LogHelper.WriteLine(Context.GetMQPathID(), Context.GetMQPath(), "Dispose", "消费者资源释放成功"); } catch (Exception exp1) { ErrorLogHelper.WriteLine(Context.GetMQPathID(), Context.GetMQPath(), "Dispose", "消费者资源释放出错", exp1); throw new BusinessMQException("移除订阅客户端注册信息失败", exp1); } finally { Context = null; } } } }
/// <summary> /// 注册消息循环 /// </summary> public void RegisterReceiveMQListener <T>(Action <BusinessMQResponse <T> > action) { try { if (Context != null) { throw new BusinessMQException("当前实例不能打开多个Consumer监听"); } PartitionIndexs = (from o in PartitionIndexs orderby o select o).Distinct().ToList(); Context = new ConsumerContext(); Context.ConsumerProvider = this; DebugHelper.WriteLine(Context.GetMQPathID(), Context.GetMQPath(), "RegisterReceiveMQListener", "消费者开始注册消息回调"); //注册信息 RegisterConsumerInfo(); //注册消息回调 Context.Listener = new ReceiveMessageListener(); Context.ActionInfo = new ConsumerActionInfo() { Action = action, ReturnType = typeof(T) }; Context.Listener.Register((c) => { BusinessMQResponse <T> r = new BusinessMQResponse <T>(); r.InnerObject = c; r.ObjMsg = ((MQMessage)c).MessageObj <T>(); action.Invoke(r); }, Context); DebugHelper.WriteLine(Context.GetMQPathID(), Context.GetMQPath(), "RegisterReceiveMQListener", "注册消费者监听成功"); LogHelper.WriteLine(Context.GetMQPathID(), Context.GetMQPath(), "RegisterReceiveMQListener", "注册消费者监听成功"); } catch (Exception exp) { ErrorLogHelper.WriteLine(Context.GetMQPathID(), Context.GetMQPath(), "RegisterReceiveMQListener", "消费者注册MQ监听出错", exp); try { this.Dispose(); } catch { } throw exp; } }
private Dictionary <int, Exception> errorpartitions = new Dictionary <int, Exception>();//分区错误信息缓存 public ReceiveMessageQuque(ConsumerContext context) { Context = context; Context.Quque = this; Create(); }