Spring Integration:MessageSource不支持errorChannel头

时间:2018-04-19 00:56:15

标签: spring-integration spring-integration-aws

我有以下流程:

@Resource(name = S3_CLIENT_BEAN)
private MessageSource<InputStream> messageSource;

public IntegrationFlow fileStreamingFlow() {
    return IntegrationFlows.from(s3Properties.getFileStreamingInputChannel())
        .enrichHeaders(spec -> spec.header(ERROR_CHANNEL, S3_ERROR_CHANNEL, true))
        .handle(String.class, (fileName, h) -> {
                    if (messageSource instanceof S3StreamingMessageSource) {
                        S3StreamingMessageSource s3StreamingMessageSource = (S3StreamingMessageSource) messageSource;

                        ChainFileListFilter<S3ObjectSummary> chainFileListFilter = new ChainFileListFilter<>();
                        chainFileListFilter.addFilters(...);
                        s3StreamingMessageSource.setFilter(chainFileListFilter);

                        return s3StreamingMessageSource.receive();
                    }                    
                    return messageSource.receive();
                }, spec -> spec
                        .requiresReply(false) // in case all messages got filtered out
        )
        .channel(s3Properties.getFileStreamingOutputChannel())
        .get();
}

我发现如果s3StreamingMessageSource.receive抛出异常,则错误最终会在为管道中的上一个流配置的错误通道中结束,而不是为此流配置的S3_ERROR_CHANNEL。不确定它是否与this问题有关。

1 个答案:

答案 0 :(得分:1)

s3StreamingMessageSource.receive()

调用SourcePollingChannelAdapter
protected Message<?> receiveMessage() {
    return this.source.receive();
}

这个来自AbstractPollingEndpoint

private boolean doPoll() {

        message = this.receiveMessage();
...

        this.handleMessage(message);
...
}

handleMessage()执行此操作:

this.messagingTemplate.send(getOutputChannel(), message);

所以,这绝对还远离上面提到的.enrichHeaders(spec -> spec.header(ERROR_CHANNEL, S3_ERROR_CHANNEL, true))下游。

但是你仍然可以在S3_ERROR_CHANNEL中捕获异常。注意IntegrationFlows.from()

的第二个参数
IntegrationFlows.from(s3Properties.getFileStreamingInputChannel(),
           e -> e.poller(Pollers.fixedDelay(...)
                       .errorChannel(S3_ERROR_CHANNEL)))

或者,根据您目前的情况,您有一个全局轮询器,因此在那里配置errorChannel