public void OnEvent(DisruptorTransportEvent data, long sequence, bool endOfBatch) { ILogger logger = null; try { var serviceProvider = data.ServiceProvider; logger = serviceProvider.GetService <ILogger <DisruptorTransportEventHandler> >(); using (var scope = data.ServiceProvider.CreateScope()) { var scopeServiceProvider = scope.ServiceProvider; var messageType = data.Message.GetType(); var handlerProxyTypeDefinition = typeof(IMessageHandlerProxy <>); var asyncHandlerProxyTypeDefinition = typeof(IAsyncMessageHandlerProxy <>); var hasHandler = false; var handlerProxies = scopeServiceProvider.GetServices(handlerProxyTypeDefinition.MakeGenericType(messageType)); if (handlerProxies.IsNotEmpty()) { hasHandler = true; MessageHandlerUtils.DynamicInvokeHandle(handlerProxies, data.Message, logger); } var asyncHandlerProxies = scopeServiceProvider.GetServices(asyncHandlerProxyTypeDefinition.MakeGenericType(messageType)); if (asyncHandlerProxies.IsNotEmpty()) { hasHandler = true; MessageHandlerUtils.DynamicInvokeAsyncHandle(asyncHandlerProxies, data.Message, logger).SyncRun(); } if (!hasHandler) { logger.LogWarning($"No message handler found: {messageType.FullName}."); } } } catch (Exception e) { var message = $"Handling disruptor transport event has a unknown exception: {e}."; if (logger == null) { Console.WriteLine(message); return; } logger.LogError(message); } }
private async void Consumer_Received(object sender, ConsumeResult <string, byte[]> eventArgs) { var kafkaMessage = eventArgs.Message; var messageTypeFullName = string.Empty; foreach (var header in kafkaMessage.Headers) { if (header.Key != "MessageType") { continue; } messageTypeFullName = Encoding.UTF8.GetString(header.GetValueBytes()); break; } var typeResolver = _serviceProvider.GetService <ITypeResolver>(); if (!typeResolver.TryResolveType(messageTypeFullName, out var messageType)) { _logger.LogError($"The Consume Group: {_consumer.Group} received kafka message from topic: {eventArgs.Topic}, but cannot resolve type: {messageTypeFullName}."); return; } ; var payload = _serializer.Deserialize(eventArgs.Message.Value, messageType); if (!(payload is IMessage message)) { _logger.LogError($"The Consume Group: {_consumer.Group} deserialized kafka message from topic: {eventArgs.Topic}, but incorrect message type, expected: {messageTypeFullName}, actual: {payload.GetType().FullName}."); return; } ; var handlerProxyTypeDefinition = typeof(IMessageHandlerProxy <>); var asyncHandlerProxyTypeDefinition = typeof(IAsyncMessageHandlerProxy <>); using (var scope = _serviceProvider.CreateScope()) { var hasHandler = false; var scopeServiceProvider = scope.ServiceProvider; var handlerProxies = scopeServiceProvider.GetServices(handlerProxyTypeDefinition.MakeGenericType(messageType)); if (handlerProxies.IsNotEmpty()) { hasHandler = true; MessageHandlerUtils.DynamicInvokeHandle(handlerProxies, message, _logger); } var asyncHandlerProxies = scopeServiceProvider.GetServices(asyncHandlerProxyTypeDefinition.MakeGenericType(messageType)); if (asyncHandlerProxies.IsNotEmpty()) { hasHandler = true; await MessageHandlerUtils.DynamicInvokeAsyncHandle(asyncHandlerProxies, message, _logger); } if (!hasHandler) { _logger.LogWarning($"No message handler found for {messageTypeFullName}, the consume group: {_consumer.Group}, topic: {eventArgs.Topic}."); } } }