Пример #1
0
        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));
            }
        }
Пример #2
0
        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);
            }
        }
Пример #3
0
        private void ProcessMessage(IServiceScope serviceScope, CapReceivedMessage receivedMessage)
        {
            var provider     = serviceScope.ServiceProvider;
            var messageStore = provider.GetRequiredService <ICapMessageStore>();

            try
            {
                var executeDescriptorGroup = _selector.GetTopicExector(receivedMessage.KeyName);

                if (executeDescriptorGroup.ContainsKey(receivedMessage.Group))
                {
                    messageStore.ChangeReceivedMessageStateAsync(receivedMessage, StatusName.Processing).Wait();

                    // 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());

                    _consumerInvokerFactory.CreateInvoker(consumerContext).InvokeAsync();

                    messageStore.ChangeReceivedMessageStateAsync(receivedMessage, StatusName.Succeeded).Wait();
                }
            }
            catch (Exception ex)
            {
                _logger.ConsumerMethodExecutingFailed($"Group:{receivedMessage.Group}, Topic:{receivedMessage.KeyName}", ex);
            }
        }
Пример #4
0
        protected virtual async Task <OperateResult> ExecuteSubscribeAsync(CapReceivedMessage receivedMessage)
        {
            try
            {
                var executeDescriptorGroup = _selector.GetTopicExector(receivedMessage.Name);

                if (!executeDescriptorGroup.ContainsKey(receivedMessage.Group))
                {
                    throw new SubscriberNotFoundException(receivedMessage.Name + " has not been found.");
                }

                // 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());

                await _consumerInvokerFactory.CreateInvoker(consumerContext).InvokeAsync();

                return(OperateResult.Success);
            }
            catch (SubscriberNotFoundException ex)
            {
                _logger.LogError("Can not be found subscribe method of name: " + receivedMessage.Name);
                return(OperateResult.Failed(ex));
            }
            catch (Exception ex)
            {
                _logger.ConsumerMethodExecutingFailed($"Group:{receivedMessage.Group}, Topic:{receivedMessage.Name}", ex);
                return(OperateResult.Failed(ex));
            }
        }
Пример #5
0
        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));
            }
        }