/// <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", "消费者监听器注册成功"); }
/// <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 void RegisterConsumerInfo() { ConsumerBLL bll = new ConsumerBLL(); DebugHelper.WriteLine(Context.GetMQPathID(), Context.GetMQPath(), "RegisterConsumerInfo", "消费者开始注册消费者信息"); //注册消费者 SqlHelper.ExcuteSql(Config.ManageConnectString, (c) => { try { //c.BeginTransaction(); //取消注册用户信息 if (Context.ConsumerInfo != null && Context.ConsumerInfo.ConsumerModel != null) { bll.RemoveConsumer(c, Context.ConsumerInfo.ConsumerModel.tempid, Context.ConsumerInfo.ConsumerModel.consumerclientid); } Context.ConsumerInfo = new ConsumerInfo(); Context.ConsumerInfo.ConsumerPartitionModels = new List <Model.tb_consumer_partition_model>(); //注册并获取clientid Context.ConsumerInfo.ConsumerClientModel = bll.RegisterClient(c, Client); if (Context.ConsumerInfo.ConsumerClientModel == null) { throw new BusinessMQException("客户端注册client失败!"); } DebugHelper.WriteLine(Context.GetMQPathID(), Context.GetMQPath(), "RegisterConsumerInfo", "注册并获取clientid成功"); //清理过期不心跳消费端,并检验partitionindex不重复,并注册消费者 Context.ConsumerInfo.ConsumerModel = bll.RegisterConsumer(c, Context.ConsumerInfo.ConsumerClientModel.id, ClientName, PartitionIndexs); if (Context.ConsumerInfo.ConsumerModel == null) { throw new BusinessMQException("当前客户端注册consumer失败!"); } DebugHelper.WriteLine(Context.GetMQPathID(), Context.GetMQPath(), "RegisterConsumerInfo", "清理过期不心跳消费端,并检验partitionindex不重复,并注册消费者成功"); //消费者订阅队列信息 Context.ConsumerInfo.MQPathModel = bll.GetMQPath(c, this.MQPath); DebugHelper.WriteLine(Context.GetMQPathID(), Context.GetMQPath(), "RegisterConsumerInfo", "消费者订阅队列信息"); //注册并更新消费者端分区信息 foreach (var partitionindex in PartitionIndexs) { var model = bll.RegisterConsumerPartition(c, Context.ConsumerInfo.ConsumerClientModel.id, partitionindex, MQPath, Context.ConsumerInfo.ConsumerModel.tempid); if (model != null) { Context.ConsumerInfo.ConsumerPartitionModels.Add(model); } } DebugHelper.WriteLine(Context.GetMQPathID(), Context.GetMQPath(), "RegisterConsumerInfo", "注册并更新消费者端分区信息"); //获取分区节点信息缓存 List <int> datanodepartition = new List <int>(); foreach (var p in Context.ConsumerInfo.ConsumerPartitionModels) { var partitionidinfo = PartitionRuleHelper.GetPartitionIDInfo(p.partitionid); if (!datanodepartition.Contains(partitionidinfo.DataNodePartition)) { datanodepartition.Add(partitionidinfo.DataNodePartition); } } Context.ConsumerInfo.DataNodeModelDic = bll.GetDataNodeModelsDic(c, datanodepartition); DebugHelper.WriteLine(Context.GetMQPathID(), Context.GetMQPath(), "RegisterConsumerInfo", "获取分区节点信息缓存"); //校准服务器时间 Context.ManageServerTime = c.GetServerDate(); //c.Commit(); } catch (Exception exp) { //c.Rollback(); throw exp; } }); DebugHelper.WriteLine(Context.GetMQPathID(), Context.GetMQPath(), "RegisterConsumerInfo", "注册消费者信息完毕"); //获取系统配置信息 ConfigHelper.LoadConfig(Config.ManageConnectString); DebugHelper.WriteLine(Context.GetMQPathID(), Context.GetMQPath(), "RegisterConsumerInfo", "获取系统配置信息"); }
private void Create() { try { DebugHelper.WriteLine(Context.GetMQPathID(), Context.GetMQPath(), "ReceiveMessageQuque-Create", "消费者开始创建内部消息队列"); Init(); cancelSource = new CancellationTokenSource(); System.Threading.Tasks.Task.Factory.StartNew(() => { Run(); }, cancelSource.Token); DebugHelper.WriteLine(Context.GetMQPathID(), Context.GetMQPath(), "ReceiveMessageQuque-Create", "消费者内部消息队列注册消息循环成功"); redislistener = new RedisNetCommandListener(ConfigHelper.RedisServer); redislistener.Name = "消费者内部消息队列"; redislistener.Register((channel, msg) => { RedisListenerCommand(channel, msg); }, cancelSource, Context.ConsumerProvider.MQPath, SystemParamConfig.Redis_Channel_Quque + "." + Context.ConsumerProvider.MQPath); DebugHelper.WriteLine(Context.GetMQPathID(), Context.GetMQPath(), "ReceiveMessageQuque-Create", "消费者内部消息队列注册redis消息监听成功"); LogHelper.WriteLine(Context.GetMQPathID(), Context.GetMQPath(), "ReceiveMessageQuque-Create", "消费者内部队列注册成功"); } catch (Exception exp) { ErrorLogHelper.WriteLine(Context.GetMQPathID(), Context.GetMQPath(), "ReceiveMessageQuque-Create", "消费者内部消息队列创建失败", exp); throw exp; } }