/// <summary> /// 开始执行 /// </summary> public void Start() { //重置 _tasks = new List <Task>(); _busses = new List <IBus>(); _dataVersionMsgs = new ConcurrentDictionary <string, long>(); #region 数据目录 string fileDirectory = string.Format(@"{0}\Data\", AppDomain.CurrentDomain.BaseDirectory); if (!Directory.Exists(fileDirectory)) { Directory.CreateDirectory(fileDirectory); } #endregion if (_configs != null && _configs.Count > 0) { #region 监听补充版本信息 var dvsrs = ConfigurationManager.AppSettings["DataVersionSupplementRabbitmqConnectionString"]; var dvsQueue = ConfigurationManager.AppSettings["DataVersionSupplementRabbitmqQueue"]; if (!string.IsNullOrWhiteSpace(dvsrs) && !string.IsNullOrWhiteSpace(dvsQueue)) { //rabbitmq client var bus = ClientFactory.CreateBus(dvsrs, x => x.Register <IRabbitLogger>(_ => new RabbitLogger(_log))); _busses.Add(bus); _tasks.Add(_factory.StartNew(() => { bus.Subscribe(dvsQueue, x => x.Add <DataVersionMsg>( msg => { string key = msg.DbConnectionKey.ToLower() + msg.TableName.ToLower(); if (_dataVersionMsgs.ContainsKey(key)) { _dataVersionMsgs[key] = msg.Version; } else { _dataVersionMsgs.Add(key, msg.Version); } }), c => c.WithPrefetchCount(1) ); })); } #endregion #region 监听数据库变更任务 foreach (var taskConfig in _configs) { var config = (TrackTaskConfig)taskConfig; var bus = ClientFactory.CreateBus(config.RabbitmqConnectionString, x => x.Register <IRabbitLogger>(_ => new RabbitLogger(_log))); _busses.Add(bus); _tasks.Add(_factory.StartNew(() => Handel(config, bus))); } #endregion } }
/// <summary> /// 开始 /// </summary> public void Start() { _tasks = new List<Task>(); if (_config != null) { //rabbitmq client _bus = ClientFactory.CreateBus(_config.RabbitmqConnectionString, x => x.Register<IRabbitLogger>(_ => new RabbitLogger(_log))); for (int i = 0; i < _config.ThreadCount; i++) { _tasks.Add(_taskFactory.StartNew(Monitor)); } } }
/// <summary> /// 检测并发送消息 /// </summary> /// <param name="start"></param> /// <param name="end"></param> private void CheckStartEndVenstions(Dictionary <string, long> start, Dictionary <string, long> end) { try { if (string.IsNullOrEmpty(_indexConfig.DBTables)) { return; } var dvsrs = ConfigurationManager.AppSettings["DataVersionSupplementRabbitmqConnectionString"]; var dvsExChange = ConfigurationManager.AppSettings["DataVersionSupplementRabbitmqExChange"]; var dvsRoutingKey = ConfigurationManager.AppSettings["DataVersionSupplementRabbitmqRoutingKey"]; if (string.IsNullOrEmpty(dvsrs) || string.IsNullOrEmpty(dvsExChange)) { _log.Warn(string.Format("{0}:数据初始化过程中有数据变更,单未配置变更消息队列,未发出同步消息!\r\n", _indexType)); return; } bus = ClientFactory.CreateBus(dvsrs); foreach (var e in end) { if (e.Value <= 0) { continue; } if (start.ContainsKey(e.Key) && e.Value <= start[e.Key]) { continue; } if (start.ContainsKey(e.Key)) { //包含时发送开始 bus.Publish(new DataVersionMsg() { DbConnectionKey = e.Key.Split('|')[0], TableName = e.Key.Split('|')[1], Version = start[e.Key] }, dvsExChange, dvsRoutingKey); _log.Info(string.Format("{0}:发送{1}版本{2}至跟踪服务...\r\n", _indexType, e.Key, start[e.Key])); } else { //不包含时发送0 基本不会走这里 bus.Publish(new DataVersionMsg() { DbConnectionKey = e.Key.Split('|')[0], TableName = e.Key.Split('|')[1], Version = 0 }, dvsExChange, dvsRoutingKey); _log.Info(string.Format("{0}:发送{1}版本{2}至跟踪服务...\r\n", _indexType, e.Key, 0)); } } bus.Dispose(); } catch (Exception ex) { _log.Error("检测数据版本的时候出错", ex); } }