private async Task InvokeConsumerMethodAsync(CapReceivedMessage receivedMessage) { if (!_selector.TryGetTopicExector(receivedMessage.Name, receivedMessage.Group, out var executor)) { var error = $"message can not be found subscriber, Message:{receivedMessage},\r\n see: https://github.com/dotnetcore/CAP/issues/63"; throw new SubscriberNotFoundException(error); } var startTime = DateTimeOffset.UtcNow; var stopwatch = Stopwatch.StartNew(); var operationId = Guid.Empty; var consumerContext = new ConsumerContext(executor, receivedMessage.ToMessageContext()); try { operationId = s_diagnosticListener.WriteSubscriberInvokeBefore(consumerContext); var ret = await Invoker.InvokeAsync(consumerContext); s_diagnosticListener.WriteSubscriberInvokeAfter(operationId, consumerContext, startTime, stopwatch.Elapsed); if (!string.IsNullOrEmpty(ret.CallbackName)) { await _callbackMessageSender.SendAsync(ret.MessageId, ret.CallbackName, ret.Result); } } catch (Exception ex) { s_diagnosticListener.WriteSubscriberInvokeError(operationId, consumerContext, ex, startTime, stopwatch.Elapsed); throw new SubscriberExecutionFailedException(ex.Message, ex); } }
public async Task <OperateResult> ExecuteAsync(CapReceivedMessage receivedMessage) { try { var executeDescriptorGroup = _selector.GetTopicExector(receivedMessage.Name); if (!executeDescriptorGroup.ContainsKey(receivedMessage.Group)) { var error = $"Topic:{receivedMessage.Name}, can not be found subscriber method."; throw new SubscriberNotFoundException(error); } // If there are multiple consumers in the same group, we will take the first var executeDescriptor = executeDescriptorGroup[receivedMessage.Group][0]; var consumerContext = new ConsumerContext(executeDescriptor, receivedMessage.ToMessageContext()); var ret = await Invoker.InvokeAsync(consumerContext); if (!string.IsNullOrEmpty(ret.CallbackName)) { await _callbackMessageSender.SendAsync(ret.MessageId, ret.CallbackName, ret.Result); } return(OperateResult.Success); } catch (Exception ex) { _logger.ConsumerMethodExecutingFailed($"Group:{receivedMessage.Group}, Topic:{receivedMessage.Name}", ex); return(OperateResult.Failed(ex)); } }
public async Task <OperateResult> ExecuteAsync(CapReceivedMessage receivedMessage) { if (!_selector.TryGetTopicExector(receivedMessage.Name, receivedMessage.Group, out var executor)) { var error = "message can not be found subscriber. Message:" + receivedMessage; error += "\r\n see: https://github.com/dotnetcore/CAP/issues/63"; throw new SubscriberNotFoundException(error); } var consumerContext = new ConsumerContext(executor, receivedMessage.ToMessageContext()); try { var ret = await Invoker.InvokeAsync(consumerContext); if (!string.IsNullOrEmpty(ret.CallbackName)) { await _callbackMessageSender.SendAsync(ret.MessageId, ret.CallbackName, ret.Result); } return(OperateResult.Success); } catch (Exception ex) { _logger.ConsumerMethodExecutingFailed($"Group:{receivedMessage.Group}, Topic:{receivedMessage.Name}", ex); return(OperateResult.Failed(ex)); } }