Esempio n. 1
0
        public override async Task SendAsync <T>(T message)
        {
            var messageHandlers = _messageHandlerProvider
                                  .GetHandlers <T>()
                                  .ToList();

            if (messageHandlers.Any())
            {
                foreach (var messageHandler in messageHandlers)
                {
                    var concreteType = typeof(IMessageHandler <>).MakeGenericType(typeof(T));
                    var method       = concreteType.GetMethod("HandAsync");
                    if (method != null)
                    {
                        await(Task) method.Invoke(messageHandler, new object[] { message });
                    }
                }
            }
            else
            {
                var messageName = MessageNameAttribute.GetNameOrDefault(message.GetType());
                _logger.LogWarning("No subscription for local memory message: {eventName}", messageName);
            }
            await Task.CompletedTask;
        }
Esempio n. 2
0
        private HandlerFindResult GetMessageHandler(string subscriber, ProcessingMessage processingMessage, IMessageHandlerProvider messageHandlerProvider, out IDictionary <IMessage, IEnumerable <IMessageHandlerProxy> > messageHandlerProxyDict)
        {
            messageHandlerProxyDict = new Dictionary <IMessage, IEnumerable <IMessageHandlerProxy> >();
            var messageGroup = processingMessage.Message as MessageGroup;

            if (messageGroup != null)
            {
                if (messageGroup.Messages == null || !messageGroup.Messages.Any())
                {
                    return(HandlerFindResult.NoMessage);
                }
                foreach (var message in messageGroup.Messages)
                {
                    var messageType      = message.GetType();
                    var handlerProxyList = messageHandlerProvider.GetHandlers(messageType, subscriber);
                    if (handlerProxyList != null && handlerProxyList.Any())
                    {
                        messageHandlerProxyDict.Add(message, handlerProxyList);
                    }
                    else if (!messageGroup.NoHandlerAllowed)
                    {
                        messageHandlerProxyDict.Clear();
                        return(HandlerFindResult.NotFound);
                    }
                }
                return(HandlerFindResult.Found);
            }
            else
            {
                var messageType      = processingMessage.Message.GetType();
                var handlerProxyList = messageHandlerProvider.GetHandlers(messageType, subscriber);
                if (handlerProxyList == null || handlerProxyList.Count() == 0)
                {
                    return(HandlerFindResult.NotFound);
                }
                else
                {
                    messageHandlerProxyDict.Add(processingMessage.Message, handlerProxyList);
                    return(HandlerFindResult.Found);
                }
            }
        }
        private async Task ProcessEvent(string eventName, string message)
        {
            _logger.LogTrace("Processing RabbitMQ event: {eventName}", eventName);

            var messageType = _messageHandlerManager.MessageHandlerWrappers
                              .FirstOrDefault(p => p.MessageName == eventName)
                              ?.MessageType;

            if (messageType != null)
            {
                var integrationEvent = (IMessage)JsonConvert.DeserializeObject(message, messageType);

                _logger.LogTrace("Enable diagnostic listeners before consume,name is {name}", DiagnosticListenerConstants.BeforeConsume);
                EventBusDiagnosticListener.TracingConsumeBefore(integrationEvent);

                var messageHandlers = _messageHandlerProvider.GetHandlers(messageType);
                foreach (var messageHandler in messageHandlers)
                {
                    var concreteType = typeof(IMessageHandler <>).MakeGenericType(messageType);
                    var method       = concreteType.GetMethod("HandAsync");
                    if (method == null)
                    {
                        continue;
                    }
                    try
                    {
                        await(Task) method.Invoke(messageHandler, new object[] { integrationEvent });
                    }
                    catch (Exception e)
                    {
                        var handlerType = messageHandler.GetType();
                        _logger.LogError("Message processing failure,message type is {messageType},handler type is {handlerType},error message is {errorMessage}",
                                         messageType, handlerType, e.Message);

                        _logger.LogTrace("Enable diagnostic listeners incorrect consume,name is {name}", DiagnosticListenerConstants.ErrorConsume);
                        EventBusDiagnosticListener.TracingConsumeError(integrationEvent, handlerType, e.Message);
                    }
                }

                _logger.LogTrace("Enable diagnostic listeners after consume,name is {name}", DiagnosticListenerConstants.AfterConsume);
                EventBusDiagnosticListener.TracingConsumeAfter(integrationEvent);
            }
            else
            {
                _logger.LogWarning("No subscription for RabbitMQ event: {eventName}", eventName);

                _logger.LogTrace("Not subscribed to enable diagnostic listener,name is {name}", DiagnosticListenerConstants.NotSubscribed);
                EventBusDiagnosticListener.TracingNotSubscribed(message);
            }
        }
Esempio n. 4
0
        public override async Task SendAsync <T>(T message)
        {
            var messageHandlers = _messageHandlerProvider
                                  .GetHandlers <T>()
                                  .ToList();

            if (messageHandlers.Any())
            {
                _logger.LogTrace("Enable diagnostic listeners before consume,name is {name}", DiagnosticListenerConstants.BeforeConsume);
                EventBusDiagnosticListener.TracingConsumeBefore(message);

                foreach (var messageHandler in messageHandlers)
                {
                    var concreteType = typeof(IMessageHandler <>).MakeGenericType(typeof(T));
                    var method       = concreteType.GetMethod("HandAsync");
                    if (method == null)
                    {
                        continue;
                    }
                    try
                    {
                        await(Task) method.Invoke(messageHandler, new object[] { message });
                    }
                    catch (Exception e)
                    {
                        var handlerType = messageHandler.GetType();
                        var messageType = message.GetType();
                        _logger.LogError("Message processing failure,message type is {messageType},handler type is {handlerType},error message is {errorMessage}",
                                         messageType, handlerType, e.Message);

                        _logger.LogTrace("Enable diagnostic listeners incorrect consume,name is {name}", DiagnosticListenerConstants.ErrorConsume);
                        EventBusDiagnosticListener.TracingConsumeError(message, handlerType, e.Message);
                    }
                }

                _logger.LogTrace("Enable diagnostic listeners after consume,name is {name}", DiagnosticListenerConstants.AfterConsume);
                EventBusDiagnosticListener.TracingConsumeAfter(message);
            }
            else
            {
                var messageName = MessageNameAttribute.GetNameOrDefault(message.GetType());
                _logger.LogWarning("No subscription for local memory message: {eventName}", messageName);

                _logger.LogTrace("Not subscribed to enable diagnostic listener,name is {name}", DiagnosticListenerConstants.NotSubscribed);
                EventBusDiagnosticListener.TracingNotSubscribed(message);
            }
            await Task.CompletedTask;
        }
        private async Task ProcessEvent(string eventName, string message)
        {
            _logger.LogTrace("Processing RabbitMQ event: {eventName}", eventName);

            if (_messageHandlerManager.MessageTypeMappingDict.TryGetValue(eventName, out var messageType))
            {
                var integrationEvent = (IMessage)JsonConvert.DeserializeObject(message, messageType);
                var messageHandlers  = _messageHandlerProvider.GetHandlers(messageType);
                foreach (var messageHandler in messageHandlers)
                {
                    var concreteType = typeof(IMessageHandler <>).MakeGenericType(messageType);
                    var method       = concreteType.GetMethod("HandAsync");
                    if (method != null)
                    {
                        await(Task) method.Invoke(messageHandler, new object[] { integrationEvent });
                    }
                }
            }
            else
            {
                _logger.LogWarning("No subscription for RabbitMQ event: {eventName}", eventName);
            }
        }