Пример #1
0
        private async Task <bool> HandleMessage(MethodInfo method, MessageHandlerBuilder messageHandlers, object actualMessage, ulong deliveryTag)
        {
            var retryCount = 0;

Retry:
            try
            {
                method.Invoke(messageHandlers, new[] { actualMessage });

                _channel.BasicAck(deliveryTag, false);

                Console.WriteLine("Ack");

                return(true);
            }
            catch (Exception ex)
            {
                if (retryCount < MaxRetryCount)
                {
                    await Task.Delay(RetryDelay);

                    retryCount++;

                    //todo log retrying
                    goto Retry;
                }

                //send to dead.letter exchange
                _channel.BasicNack(deliveryTag, false, false);

                //todo log error
                return(false);
            }
        }
Пример #2
0
        private void DiscoverHandlers()
        {
            var serviceProvider = GetServiceProvider();
            var messageHandlers = new MessageHandlerBuilder(serviceProvider);
            var consumer        = new EventingBasicConsumer(_channel);

            consumer.Received += async(sender, message) =>
            {
                var props       = message.BasicProperties;
                var deliveryTag = message.DeliveryTag;
                var headers     = props.Headers;

                if (!_typeMap.ContainsKey(props.Type) || !_typeMap.TryGetValue(props.Type, out Type actualMessageType))
                {
                    return;
                }

                var actualMessage = GetSerializer().Deserialize(message.Body, actualMessageType);

                var method = typeof(MessageHandlerBuilder).GetMethod("HandleMessagesAsync").MakeGenericMethod(actualMessage.GetType());

                var(canHandle, errorMessage) = await HandleMessage(method, messageHandlers, headers, actualMessage, deliveryTag);

                if (!canHandle)
                {
                    SendToDeadLetterQueue(message.BasicProperties, message.Body);
                }
            };

            lock (_channel)
            {
                _consumerTag = _channel.BasicConsume(consumer, _serviceQueue, false);
            }
        }
Пример #3
0
        private async Task <(bool, string)> HandleMessage(MethodInfo method, MessageHandlerBuilder messageHandlers, IDictionary <string, object> headers, object actualMessage, ulong deliveryTag)
        {
            var retryCount = 0;

Retry:
            try
            {
                await(Task) method.Invoke(messageHandlers, new[] { headers, actualMessage });

                lock (_channel)
                {
                    _channel.BasicAck(deliveryTag, false);
                }

                //Console.WriteLine("Ack");

                return(true, null);
            }
            catch (Exception ex)
            {
                var body = JsonConvert.SerializeObject(actualMessage);

                var message = $"Error while handle message: {actualMessage.GetType().FullName}\n{ex.Message}\n{ex.StackTrace}";

                Console.WriteLine(message);

                if (retryCount < MaxRetryCount)
                {
                    await Task.Delay(RetryDelay);

                    retryCount++;

                    //todo log retrying
                    goto Retry;
                }

                lock (_channel)
                {
                    //send to dead.letter exchange
                    _channel.BasicNack(deliveryTag, false, false);
                }

                //todo log error
                return(false, message);
            }
        }
Пример #4
0
        private void DiscoverHandlers()
        {
            var messageHandlers = new MessageHandlerBuilder(_serviceProvider);
            var consumer        = new EventingBasicConsumer(_channel);

            consumer.Received += (sender, message) =>
            {
                var props             = message.BasicProperties;
                var actualMessageType = _typeMap[props.Type];
                var actualMessage     = _serializer.Deserialize(message.Body, actualMessageType);

                typeof(MessageHandlerBuilder).GetMethod("HandleMessages")
                .MakeGenericMethod(actualMessage.GetType()).Invoke(messageHandlers, new[] { actualMessage });
            };

            _consumerTag = _channel.BasicConsume(consumer, _serviceQueue, true);
        }
Пример #5
0
        private void DiscoverHandlers()
        {
            var messageHandlers = new MessageHandlerBuilder(GetServiceProvider());
            var consumer        = new EventingBasicConsumer(_channel);

            consumer.Received += async(sender, message) =>
            {
                var props             = message.BasicProperties;
                var deliveryTag       = message.DeliveryTag;
                var actualMessageType = _typeMap[props.Type];
                var actualMessage     = GetSerializer().Deserialize(message.Body, actualMessageType);

                var method = typeof(MessageHandlerBuilder).GetMethod("HandleMessages").MakeGenericMethod(actualMessage.GetType());

                var success = await HandleMessage(method, messageHandlers, actualMessage, deliveryTag);

                if (!success)
                {
                    SendToDeadLetterQueue(message.BasicProperties, message.Body);
                }
            };

            _consumerTag = _channel.BasicConsume(consumer, _serviceQueue, false);
        }