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

时间:2018-09-08 15:16:20

标签: spring asynchronous error-handling spring-integration aggregate

我创建了一个使用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"/>

----更新------

这是处理所有错误消息的转换器

@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;
        }
    }

}

1 个答案:

答案 0 :(得分:0)

  

已收到答复消息,但接收线程已收到答复。

错误提示,您不能为单个请求发送多个回复(或错误);严格来说,每个请求只有一个回复。

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

中流网关应该没有service-interface,因此它使用RequestReplyExchanger