错误通道在拆分器/聚合器中不起作用(异步调用)

问题描述 投票:1回答:1

我创建了一个使用Splitter / Aggregator异步调用网关的应用程序。在我的配置文件中,我通过InvestmentMessagingGateway调用该过程,该过程在调用拆分器时继续。每个拆分的消息并行调用服务激活器并将其传递给聚合器。我在InvestmentMessagingGateway中放置了一个错误通道,并将每个失败的消息转换为也传递给聚合器。

我在聚合器中收集每个成功和失败的消息作为响应的汇编。但是当我尝试在一个或多个消息中放置异常时,我的聚合器中出现错误,

收到回复消息但接收线程已收到回复。

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="......."">

    <context:component-scan base-package="com.api.investments"/>


    <!--The gateway to be called in parallel-->
    <gateway id="InvestmentGateway" service-interface="com.api.investments.gateways.InvestmentGateway"/>

    <channel id="investmentDetailChannel"/> 
    <service-activator input-channel="investmentDetailChannel" ref="investmentService" method="getAccountPortfolio"/>


    <!--Inbound gateway to invoke Splitter / Aggregator-->
    <gateway id="InvestmentMessageGateway" service-interface="com.api.investments.gateways.InvestmentMessageGateway"
  default-reply-channel="investmentAsyncReceiver" error-channel="investmentAsyncException"/>

    <channel id="investmentAsyncSender"/>
    <channel id="investmentAsyncReceiver"/>

    <!-- Splitter for Invesment Details-->
    <splitter input-channel="investmentAsyncSender" output-channel="investmentSplitChannel" id="investmentDetailsSplitter" ref="investmentComponentsSplitter" />

    <channel id="investmentSplitChannel">
        <queue />
    </channel>

  <!--Calls the Investment Gateway asynchronously using split messages ad send the response in aggregator-->
    <service-activator input-channel="investmentSplitChannel" output-channel="investmentAggregateChannel" ref="investmentAsyncActivator" method="retrieveInvestmentDetailsAsync" requires-reply="true">
        <poller receive-timeout="5000" task-executor="investmentExecutor" fixed-rate="50"/>
    </service-activator>



    <channel id="investmentAsyncException"/>

  <!--Handles failed messages and pass it in aggregator-->
    <transformer input-channel="investmentAsyncException" output-channel="investmentAggregateChannel" ref="invesmentErrorLogger" method="logError"/>

  <!--Aggreggates successfull and failed messaged-->
    <publish-subscribe-channel id="investmentAggregateChannel"/>
    <aggregator input-channel="investmentAggregateChannel" output-channel="investmentAsyncReceiver" id="investmentAggregator"
    ref="investmentComponentsAggregator" correlation-strategy="investmentComponentsCorrelationStrategy"
    expire-groups-upon-completion="true"
    send-partial-result-on-expiry="true" />


    <task:executor id="investmentExecutor" pool-size="10-1000"
                   queue-capacity="5000"/>

</beans:beans>

我尝试将我的错误通道放在服务激活器的轮询器中,但错误仍然是相同的,但这次它没有进入聚合器。我也试过像这样为服务激活器设置一个中间网关,但错误变为空。

<gateway id="InvestmentAsyncActivatorGateway" service-interface="com.api.investments.gateways.InvestmentAsyncActivatorGateway"
default-reply-channel="investmentAggregateChannel" error-channel="investmentAsyncException"/>

---- ------ UPDATE

这是处理每个错误消息的转换器

@Component("invesmentErrorLogger")
public class InvesmentErrorLoggerImpl implements InvestmentErrorLogger {

    private final Logger logger = LoggerFactory.getLogger(Application.class.getName());

    /**
     * handles all error messages in InvestmentMessageGateway
     * Creates an error message and pass it in the aggregator channel
     * @param invesmentMessageError
     * @return errorMessage
     */
    @Override
    public Message<ErrorDetails> logError(Message<?> invesmentMessageError) {
        if(invesmentMessageError.getPayload().getClass().equals(MessagingException.class)) {
            MessagingException messageException = (MessagingException) invesmentMessageError.getPayload();
            AccountPortfolioRequest failedMsgPayload = (AccountPortfolioRequest) messageException.getFailedMessage().getPayload();
            String logError = "Exception occured in Account Number: " + failedMsgPayload.getiAccNo();
            logger.error(logError);
            ErrorDetails productErrorDetail = new ErrorDetails();
            productErrorDetail.setCode(InvestmentAPIErrorMessages.SVC_ERR_INQACCNTPORTFOLIO);
            productErrorDetail.setMessage(InvestmentAPIErrorMessages.SVC_ERR_INQACCNTPORTFOLIO_DESC + ". Problem occured in Account Number: " + failedMsgPayload.getiAccNo());

            Message<ErrorDetails> errorMessage = MessageBuilder.withPayload(productErrorDetail)
                    .setHeaderIfAbsent(InvestmentInquiryConstants.INV_CORRELATION_STRATEGY, InvestmentInquiryConstants.INV_CORRELATION_STRATEGY_VALUE)
                    .build();

            return errorMessage;
        }
        else if(invesmentMessageError.getPayload().getClass().equals(MessageDeliveryException.class)) {
            MessageDeliveryException messageException = (MessageDeliveryException) invesmentMessageError.getPayload();
            AccountPortfolioRequest failedMsgPayload = (AccountPortfolioRequest) messageException.getFailedMessage().getPayload();
            String logError = "Exception occured in Account Number: " + failedMsgPayload.getiAccNo();
            logger.error(logError);
            ErrorDetails productErrorDetail = new ErrorDetails();
            productErrorDetail.setCode(InvestmentAPIErrorMessages.SVC_ERR_INQACCNTPORTFOLIO);
            productErrorDetail.setMessage(InvestmentAPIErrorMessages.SVC_ERR_INQACCNTPORTFOLIO_DESC + ". Problem occured in Account Number: " + failedMsgPayload.getiAccNo());

            Message<ErrorDetails> errorMessage = MessageBuilder.withPayload(productErrorDetail)
                    .setHeaderIfAbsent(InvestmentInquiryConstants.INV_CORRELATION_STRATEGY, InvestmentInquiryConstants.INV_CORRELATION_STRATEGY_VALUE)
                    .build();

            return errorMessage;
        }
        else {
            Exception messageException = (Exception) invesmentMessageError.getPayload();
            String logError = "Exception occured in Investment Gateway ";
            logger.error(logError);
            logger.equals(messageException.getMessage());
            ErrorDetails productErrorDetail = new ErrorDetails();
            productErrorDetail.setCode(InvestmentAPIErrorMessages.SVC_ERR_INQACCNTPORTFOLIO);
            productErrorDetail.setMessage(InvestmentAPIErrorMessages.SVC_ERR_INQACCNTPORTFOLIO_DESC + " " + messageException.getMessage());

            Message<ErrorDetails> errorMessage = MessageBuilder.withPayload(productErrorDetail)
                    .setHeaderIfAbsent(InvestmentInquiryConstants.INV_CORRELATION_STRATEGY, InvestmentInquiryConstants.INV_CORRELATION_STRATEGY_VALUE)
                    .build();

            return errorMessage;
        }
    }

}
spring asynchronous error-handling spring-integration aggregate
1个回答
0
投票

收到回复消息但接收线程已收到回复。

如错误所示,您无法为单个请求发送多个回复(或错误);每个请求严格一个回复。

您需要在拆分器和服务之间使用另一个网关。

中流网关应该没有service-interface所以它使用RequestReplyExchanger

© www.soinside.com 2019 - 2024. All rights reserved.