Exemplo n.º 1
0
        /// <summary>
        /// Доставляет сообщение до обработчика.
        /// </summary>
        /// <param name="delivery">
        /// Входящее сообщение.
        /// </param>
        protected void Deliver(RabbitDelivery delivery)
        {
            this.logger.Trace(m => m("Received delivery labeled [{0}] from [{1}] with consumer [{2}].", delivery.Label, delivery.Args.Exchange, delivery.Args.ConsumerTag));

            if (delivery.Headers.ContainsKey(Headers.OriginalMessageId))
            {
                this.logger.Trace(m => m("Сквозной идентификатор сообщения [{0}].", Headers.GetString(delivery.Headers, Headers.OriginalMessageId)));
            }

            if (delivery.Headers.ContainsKey(Headers.Breadcrumbs))
            {
                this.logger.Trace(m => m("Сообщение было обработано в конечных точках: [{0}].", Headers.GetString(delivery.Headers, Headers.Breadcrumbs)));
            }

            var tags = delivery.IsResponse
                           ? new[]
            {
                "deliveryEndpoint:" + delivery.Channel.Bus.Endpoint.Address
            }
                           : new[]
            {
                "deliveryEndpoint:" + delivery.Channel.Bus.Endpoint.Address,
                "deliveryLabel:" + delivery.Label,
                "deliveryExchange:" + delivery.Args.Exchange
            };

            Metric.Increment("gemsb.rmq.consuming.count", 1D, tags);
            Stopwatch stopwatch = Stopwatch.StartNew();

            try
            {
                // TODO: refactor
                bool processed = this.TryHandleAsResponse(delivery);

                if (!processed)
                {
                    processed = this.TryHandleAsSubscription(delivery);
                }

                if (!processed)
                {
                    Metric.Increment("gemsb.rmq.unhandleddelivery.count", 1D, tags);
                    this.OnUnhandled(delivery);
                }
            }
            catch (Exception ex)
            {
                Metric.Increment("gemsb.rmq.faileddelivery.count", 1D, tags);
                this.OnFailure(delivery, ex);
            }

            stopwatch.Stop();
            Metric.Increment("gemsb.rmq.consumed.count", 1D, tags);
            Metric.Decrement("gemsb.rmq.consuming.count", 1D, tags);
            Metric.Histogram("gemsb.rmq.consuming.duration", stopwatch.ElapsedMilliseconds, 1D, tags);
            this.logger.Trace(m => m("Message labeled [{0}] processed in {1} ms.", delivery.Label, stopwatch.ElapsedMilliseconds));
        }