protected void SendResponse(RC.IModel channel, Address replyTo, IMessage <byte[]> messageIn)
        {
            var message = messageIn;

            if (BeforeSendReplyPostProcessors != null)
            {
                var      processors    = BeforeSendReplyPostProcessors;
                IMessage postProcessed = message;
                foreach (var postProcessor in processors)
                {
                    postProcessed = postProcessor.PostProcessMessage(postProcessed);
                }

                message = postProcessed as IMessage <byte[]>;
                if (message == null)
                {
                    throw new InvalidOperationException("A BeforeSendReplyPostProcessors failed to return IMessage<byte[]>");
                }
            }

            PostProcessChannel(channel, message);

            try
            {
                _logger?.LogDebug("Publishing response to exchange = [{exchange}], routingKey = [{routingKey}]", replyTo.ExchangeName, replyTo.RoutingKey);
                if (RetryTemplate == null)
                {
                    DoPublish(channel, replyTo, message);
                }
                else
                {
                    var messageToSend = message;
                    RetryTemplate.Execute <object>(
                        ctx =>
                    {
                        DoPublish(channel, replyTo, messageToSend);
                        return(null);
                    },
                        ctx =>
                    {
                        if (RecoveryCallback != null)
                        {
                            ctx.SetAttribute(SendRetryContextAccessor.MESSAGE, messageToSend);
                            ctx.SetAttribute(SendRetryContextAccessor.ADDRESS, replyTo);
                            RecoveryCallback.Recover(ctx);
                            return(null);
                        }
                        else
                        {
                            throw RabbitExceptionTranslator.ConvertRabbitAccessException(ctx.LastException);
                        }
                    });
                }
            }
            catch (Exception ex)
            {
                throw RabbitExceptionTranslator.ConvertRabbitAccessException(ex);
            }
        }
Ejemplo n.º 2
0
        protected void SendResponse(IModel channel, Address replyTo, Message messageIn)
        {
            var message = messageIn;

            if (BeforeSendReplyPostProcessors != null)
            {
                var processors = BeforeSendReplyPostProcessors;
                foreach (var postProcessor in processors)
                {
                    message = postProcessor.PostProcessMessage(message);
                }
            }

            PostProcessChannel(channel, message);

            try
            {
                _logger?.LogDebug("Publishing response to exchange = [" + replyTo.ExchangeName + "], routingKey = [" + replyTo.RoutingKey + "]");
                if (RetryTemplate == null)
                {
                    DoPublish(channel, replyTo, message);
                }
                else
                {
                    var messageToSend = message;
                    RetryTemplate.Execute <object>(
                        ctx =>
                    {
                        DoPublish(channel, replyTo, messageToSend);
                        return(null);
                    },
                        ctx =>
                    {
                        if (RecoveryCallback != null)
                        {
                            ctx.SetAttribute(SendRetryContextAccessor.MESSAGE, messageToSend);
                            ctx.SetAttribute(SendRetryContextAccessor.ADDRESS, replyTo);
                            RecoveryCallback.Recover(ctx);
                            return(null);
                        }
                        else
                        {
                            throw RabbitExceptionTranslator.ConvertRabbitAccessException(ctx.LastException);
                        }
                    });
                }
            }
            catch (Exception ex)
            {
                throw RabbitExceptionTranslator.ConvertRabbitAccessException(ex);
            }
        }