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; } } } }
private void GetMessagesFromPartitions() { foreach (var partition in Context.ConsumerInfo.ConsumerPartitionModels) { if (cancelSource.IsCancellationRequested) { break; } try { if (errorpartitions.ContainsKey(partition.partitionid)) { throw new BusinessMQException("分区消息处理出现错误", errorpartitions[partition.partitionid]); } var scanlastmqid = _lastpullququeiddic[partition.partitionid]; //上次扫描的mqid var mqidinfo = PartitionRuleHelper.GetMQIDInfo(scanlastmqid); //解析上次扫描的mqid信息 ConsumerBLL consumerbll = new ConsumerBLL(); var partionidinfo = PartitionRuleHelper.GetPartitionIDInfo(partition.partitionid); //解析分区信息 string datanodeconnectstring = consumerbll.GetDataNodeConnectString(SystemParamConfig.Consumer_DataNode_ConnectString_Template, Context.ConsumerInfo.DataNodeModelDic[partionidinfo.DataNodePartition]); //获取节点连接 var messages = GetMessagesOfPatition(partition, scanlastmqid, mqidinfo, datanodeconnectstring, partionidinfo); if (messages.Count > 0) { ConsumeMessages(messages, partition, mqidinfo); } else { CheckIfScanNewTable(partition, mqidinfo, datanodeconnectstring, partionidinfo); } } catch (Exception exp) { ErrorLog.Write(string.Format("MQ消费者端消息循环出错,clientid:{0},partitionid:{1}", partition.consumerclientid, partition.partitionid), 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", "获取系统配置信息"); }