Example #1
0
        private void SendMsg(TrackTaskConfig config, IBus bus)
        {
            long version = 0;
            //取文件中最后版本号
            string filePath = string.Format(@"{0}\Data\{1}_{2}.txt", AppDomain.CurrentDomain.BaseDirectory, config.DbConnectionKey, config.TableName);

            if (File.Exists(filePath))
            {
                var content = File.ReadAllText(filePath);
                if (!string.IsNullOrWhiteSpace(content))
                {
                    long.TryParse(content, out version);
                }
            }
            string dataKey = config.DbConnectionKey.ToLower() + config.TableName.ToLower();

            _log.DebugFormat("DbConnectionKey:{0};TableName:{1};Version:{2};Msg:存储版本号;", config.DbConnectionKey, config.TableName, version);
            if (_dataVersionMsgs.ContainsKey(dataKey))
            {
                if (version > _dataVersionMsgs[dataKey])
                {
                    version = _dataVersionMsgs[dataKey];
                    _log.DebugFormat("DbConnectionKey:{0};TableName:{1};Version:{2};Msg:版本号补充后变更;", config.DbConnectionKey, config.TableName, version);
                }
                _dataVersionMsgs.Remove(dataKey);
            }

            _log.DebugFormat("DbConnectionKey:{0};TableName:{1};Version:{2};Msg:开始查询;", config.DbConnectionKey, config.TableName, version);
            //查询
            var list = DL.DataTrackDl.GetDataChangeMsgs(config.DbConnectionKey, config.TableName, version, config.PkName);

            _log.InfoFormat("DbConnectionKey:{0};TableName:{1};Version:{2};Msg:查询完成;Count:{3};", config.DbConnectionKey, config.TableName, version, list == null ? 0 : list.Count);
            //发送
            if (list != null && list.Count > 0)
            {
                foreach (var dataChangeMsg in list)
                {
                    dataChangeMsg.DbConnectionKey = config.DbConnectionKey;
                    if (bus.Publish(dataChangeMsg, config.ExChange, config.RoutingKey))
                    {
                        _log.InfoFormat(
                            "DbConnectionKey:{0};TableName:{1};Type:{2};PkName:{3};PkValue:{4};Version:{5};Status:true;Msg:发送消息成功;",
                            config.DbConnectionKey, config.TableName, dataChangeMsg.Type, dataChangeMsg.PkName,
                            dataChangeMsg.PkValue, dataChangeMsg.Version);
                    }
                    else
                    {
                        _log.ErrorFormat("DbConnectionKey:{0};TableName:{1};Type:{2};PkName:{3};PkValue:{4};Version:{5};Status:false;Msg:发送消息失败;",
                                         config.DbConnectionKey, config.TableName, dataChangeMsg.Type, dataChangeMsg.PkName, dataChangeMsg.PkValue, dataChangeMsg.Version);
                    }
                }
                //保存版本号到文件
                File.WriteAllText(filePath, list.Max(n => n.Version).ToString(CultureInfo.InvariantCulture));
                _log.InfoFormat("DbConnectionKey:{0};TableName:{1};Version:{2};Msg:保存新的版本号;", config.DbConnectionKey, config.TableName, list.Max(n => n.Version));
            }
        }
Example #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="config"></param>
        /// <param name="bus"></param>
        private void Handel(TrackTaskConfig config, IBus bus)
        {
            while (!CancelTokenSource.IsCancellationRequested)
            {
                try
                {
                    SendMsg(config, bus);
                }
                catch (Exception ex)
                {
                    _log.Error(ex);
                }

                Thread.Sleep(config.Interval * 1000);
            }
        }