Пример #1
0
        public void After_One_Subscriber_Was_Added_The_Message_Handler_Should_Be_One()
        {
            _subscriberManager.Subscribe <TestMessage, TestMessageHandler>();
            _subscriberManager.Subscribe <TestDynamicMessageHandler>("TestDynamicMessageHandler");

            var subscribers        = _subscriberManager.GetSubscribers <TestMessage>();
            var dynamicSubscribers = _subscriberManager.GetSubscribers("TestDynamicMessageHandler");

            Assert.Single(subscribers);
            Assert.Single(dynamicSubscribers);
        }
Пример #2
0
        private async Task DoPublishAsync(string messageName, dynamic message)
        {
            Assert.NotNullOrEmpty(messageName, nameof(messageName));
            Assert.NotNull(message, nameof(message));

            var subscribers = _subscriberManager.GetSubscribers(messageName);

            if (subscribers != null && subscribers.Any())
            {
                using (var scope = _serviceScopeFactory.CreateScope())
                {
                    foreach (var subcriber in subscribers)
                    {
                        dynamic handler = scope.ServiceProvider.GetService(subcriber.MessageHandlerType);

                        if (handler != null)
                        {
                            if (_options.MonitorSlowMessageHandler)
                            {
                                var start = DateTime.UtcNow;

                                await handler.HandleAsync(message);

                                var elapsed = DateTime.UtcNow - start;
                                if (elapsed > _options.SlowMessageHandlerThreshold)
                                {
                                    _logger.LogTrace("SLOW BUS MSG [{0}]: {1} - {2}ms. Handler: {3}.", Name, messageName, elapsed.TotalMilliseconds, (string)handler.GetType().Name);

                                    OnSlowMessageHandled.Invoke(this, new SlowMessageHandlerEventArgs(messageName, subcriber.MessageHandlerType.Name, elapsed));
                                }
                            }
                            else
                            {
                                await handler.HandleAsync(message);
                            }

                            _logger.LogInformation($"MESSAGE BUS[{Name}]: the message[{messageName}] had been hanlded[{(string)handler.GetType().Name}].");
                        }
                        else
                        {
                            _logger.LogError($"MESSAGE BUS[{Name}]: the message[{messageName}] handler could not be found.");
                        }
                    }
                }
            }
        }
Пример #3
0
 public IEnumerable <MessageSubscriber> GetSubscribers(Type messageType)
 => _subscriber.GetSubscribers(messageType);