Exemplo n.º 1
0
        /// <summary>
        /// 获取消费消息
        /// </summary>
        /// <param name="subscribeAppName">订阅APP名称</param>
        private async Task getSubscribe(string subscribeAppName)
        {
            if (_Queue == null)
            {
                _Cts.Cancel();
                OnStopSubscribe("消息列队对像为Null");
                return;
            }
            while (!_Cts.IsCancellationRequested)
            {
                try
                {
                    var msg = await _Queue.TakeMessageAsync(10);

                    if (msg != null)
                    {
                        var data = msg.GetBody <T>();
                        //通知订阅者
                        OnReceived(data);
                        _Queue.Acknowledge(msg.Id);
                    }
                }
                catch (Exception err)
                {
                    if (XTrace.Debug)
                    {
                        XTrace.WriteException(err);               //TODO:要优化处理日志的记录(此处日志调试用,实际生产环境不记录)
                    }
                    _Cts.Cancel();
                    OnStopSubscribe(err.Message);
                    return;
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 获取消费消息
        /// </summary>
        /// <param name="subscribeAppName">订阅APP名称</param>
        private async Task getSubscribe(string subscribeAppName)
        {
            if (_Queue == null)
            {
                _Cts.Cancel();
                OnDisconnected("消息列队对像为Null");
                return;
            }
            while (!_Cts.IsCancellationRequested)
            {
                if (_Redis == null || _Queue == null)
                {
                    OnDisconnected("获取订阅消息时列队对像为Null。");
                }

                try
                {
                    var msg = await _Queue.TakeMessageAsync(10);

                    if (msg != null)
                    {
                        var data = msg.GetBody <T>();
                        _Queue.Acknowledge(msg.Id);
                        //通知订阅者
                        OnReceived(data);
                    }
                }
                catch (Exception err)
                {
                    if (XTrace.Debug)
                    {
                        XTrace.WriteException(err);
                    }

                    _Cts.Cancel();
                    OnStopSubscribe(err.Message);
                    return;
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>队列消费大循环,处理消息后自动确认</summary>
        /// <typeparam name="T">消息类型</typeparam>
        /// <param name="queue">队列</param>
        /// <param name="onMessage">消息处理。如果处理消息时抛出异常,消息将延迟后回到队列</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <param name="log">日志对象</param>
        /// <returns></returns>
        public static async Task ConsumeAsync <T>(this RedisStream <String> queue, Func <T, Message, CancellationToken, Task> onMessage, CancellationToken cancellationToken = default, ILog log = null)
        {
            // 大循环之前,打断性能追踪调用链
            DefaultSpan.Current = null;

            // 自动创建消费组
            var gis = queue.GetGroups();

            if (gis == null || !queue.Group.IsNullOrEmpty() && !gis.Any(e => e.Name.EqualIgnoreCase(queue.Group)))
            {
                queue.GroupCreate(queue.Group);
            }

            // 主题
            var topic = queue.Key;

            if (topic.IsNullOrEmpty())
            {
                topic = queue.GetType().Name;
            }

            var rds    = queue.Redis;
            var tracer = rds.Tracer;
            var errLog = log ?? XTrace.Log;

            // 超时时间,用于阻塞等待
            var timeout = rds.Timeout / 1000 - 1;

            while (!cancellationToken.IsCancellationRequested)
            {
                Message mqMsg = null;
                ISpan   span  = null;
                try
                {
                    // 异步阻塞消费
                    mqMsg = await queue.TakeMessageAsync(timeout, cancellationToken);

                    if (mqMsg != null)
                    {
                        // 埋点
                        span = tracer?.NewSpan($"redismq:{topic}", mqMsg);
                        log?.Info($"[{topic}]消息内容为:{mqMsg}");

                        var bodys = mqMsg.Body;
                        for (var i = 0; i < bodys.Length; i++)
                        {
                            if (bodys[i].EqualIgnoreCase("traceParent") && i + 1 < bodys.Length)
                            {
                                span.Detach(bodys[i + 1]);
                            }
                        }

                        // 解码
                        var msg = mqMsg.GetBody <T>();

                        // 处理消息
                        await onMessage(msg, mqMsg, cancellationToken);

                        // 确认消息
                        queue.Acknowledge(mqMsg.Id);
                    }
                    else
                    {
                        // 没有消息,歇一会
                        await Task.Delay(1000, cancellationToken);
                    }
                }
                catch (ThreadAbortException) { break; }
                catch (ThreadInterruptedException) { break; }
                catch (Exception ex)
                {
                    span?.SetError(ex, null);
                    errLog?.Error("[{0}/{1}]消息处理异常:{2} {3}", topic, mqMsg?.Id, mqMsg?.ToJson(), ex);
                }
                finally
                {
                    span?.Dispose();
                }
            }
        }