Example #1
0
        private bool PreparedAndEnqueue()
        {
            if (_canalConnector == null)
            {
                return(false);
            }

            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            var messageList = _canalConnector.GetWithoutAck(_canalOption.GetCountsPerTimes);
            var batchId     = messageList.Id;

            if (batchId < 1)
            {
                return(false);
            }

            CanalBody body = new CanalBody(null, batchId);

            if (messageList.Entries.Count <= 0)
            {
                _canalQueue.Enqueue(new CanalQueueData
                {
                    CanalBody = body
                });
                return(false);
            }

            var canalBody = GetCanalBodyList(messageList.Entries, batchId);

            if (canalBody.Message == null || canalBody.Message.Count < 1)
            {
                _canalQueue.Enqueue(new CanalQueueData
                {
                    CanalBody = body
                });
                return(false);
            }

            stopwatch.Stop();

            var doutime = (int)stopwatch.Elapsed.TotalSeconds;
            var time    = doutime > 1 ? ParseTimeSeconds(doutime) : stopwatch.Elapsed.TotalMilliseconds + "ms";

            var canalQueueData = new CanalQueueData
            {
                Time      = time,
                CanalBody = canalBody
            };

            _canalQueue.Enqueue(canalQueueData);

            return(true);
        }
Example #2
0
        /// <summary>
        /// 获取数据
        /// </summary>
        private void CanalGetData(object state)
        {
            _canalTimer.Change(-1, -1);
            try
            {
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();

                if (_canalConnector == null)
                {
                    return;
                }
                var messageList = _canalConnector.GetWithoutAck(_canalOption.GetCountsPerTimes);
                var batchId     = messageList.Id;
                if (batchId == -1 || messageList.Entries.Count <= 0)
                {
                    return;
                }

                var result = Send(messageList.Entries, batchId);


                stopwatch.Stop();

                if (result.Item1 < 1)
                {
                    return;
                }

                var doutime = (int)stopwatch.Elapsed.TotalSeconds;
                var time    = doutime > 1 ? ParseTimeSeconds(doutime) : stopwatch.Elapsed.TotalMilliseconds + "ms";

                if (result.Item2 == null)
                {
                    return;
                }

                foreach (var item in result.Item2)
                {
                    if (item.Value.Total > 0)
                    {
                        _logger.LogInformation($"【batchId:{batchId}】batchCount:{result.Item1},batchTime:{time},process:{item.Key},processTotal:{item.Value.Total},processSucc:{item.Value.SuccessCount},processTime:{item.Value.ProcessTime}");
                        if (result.Item3 != null && result.Item3.Any())
                        {
                            foreach (var groupResult in result.Item3)
                            {
                                _logger.LogInformation($"batchId:{batchId},process:{item.Key},target:{groupResult.Item1},count:{groupResult.Item2}");
                            }
                        }
                    }
                }
            }
            catch (ObjectDisposedException)
            {
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "canal get data error...");
            }
            finally
            {
                _canalTimer.Change(_canalOption.Timer * 1000, _canalOption.Timer * 1000);
            }
        }