示例#1
0
        /// <summary>
        /// Subscriber
        /// </summary>
        protected void Subscribe <TRequest>(CancellationToken ctx)
        {
            _eventingBasicConsumer.Received += async(sender, e) =>
            {
                object?attemptObj = null;
                e.BasicProperties.Headers?.TryGetValue("x-attempt", out attemptObj);
                int.TryParse(attemptObj?.ToString() ?? "0", out var attempt);

                try
                {
                    var request = _serializer.Deserialize(typeof(TRequest), e.Body);
                    var @event  = (TRequest)request;
                    await ProcessMessage(@event, ctx);
                }
                catch (Exception ex)
                {
                    _logger?.LogError(ex, ex.Message);

                    object?ttlObj = null;
                    e.BasicProperties.Headers?.TryGetValue("x-message-ttl", out ttlObj);
                    var ttl = ttlObj == null
                        ? _config.GetDelayInSeconds()
                        : TimeSpan.FromMilliseconds((int)ttlObj);

                    var properties = GetBasicProperties(++attempt, ttl);
                    Publish(attempt, ttl, properties, e.Body.ToArray());
                }
                finally
                {
                    TryAck(e);
                }
            };

            ConfigureSubscriber();
        }