处理Spring Integration File的预期返回 [英] Handle expected return from Spring Integration File

查看:160
本文介绍了处理Spring Integration File的预期返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下Spring Integration文件的配置文件:

I have the following configuration file for Spring Integration File:

@Configuration
@EnableIntegration
public class MyIntegrationConfiguration {

    private static final String FILE_CHANNEL_PROCESSING = "processingfileChannel";
    private static final String INTERVAL_PROCESSING = "5000";
    private static final String FILE_PATTERN = "*.txt";

    @Value("${import.path.source}")
    private String sourceDir;

    @Value("${import.path.output}")
    private String outputDir;

    @Bean
    @InboundChannelAdapter(value = FILE_CHANNEL_PROCESSING, poller = @Poller(fixedDelay = INTERVAL_PROCESSING))
    public MessageSource<File> sourceFiles() {
        FileReadingMessageSource source = new FileReadingMessageSource();
        source.setAutoCreateDirectory(true);
        source.setDirectory(new File(sourceDir));
        source.setFilter(new SimplePatternFileListFilter(FILE_PATTERN));
        return source;
    }

    @Bean
    @ServiceActivator(inputChannel = FILE_CHANNEL_PROCESSING)
    public MessageHandler processedFiles() {
        FileWritingMessageHandler handler = new FileWritingMessageHandler(new File(outputDir));
        handler.setFileExistsMode(FileExistsMode.REPLACE);
        handler.setDeleteSourceFiles(true);
        handler.setExpectReply(true);
        return handler;
    }

    @Bean
    public IntegrationFlow processFileFlow() {
        return IntegrationFlows
            .from(FILE_CHANNEL_PROCESSING)
            .transform(fileToStringTransformer())
            .handle("fileProcessor", "processFile").get();
    }

    @Bean
    public MessageChannel fileChannel() {
        return new DirectChannel();
    }

    @Bean
    public FileProcessor fileProcessor() {
        return new FileProcessor();
    }

    @Bean
    public FileToStringTransformer fileToStringTransformer() {
        return new FileToStringTransformer();
    }
}

对于 FileWritingMessageHandler 来自这个文档它说如果 setExpectReply(true)被设置:

For FileWritingMessageHandler from this documentation it says that if setExpectReply(true) is set:


指定是否需要回复消息。如果没有,此处理程序将仅为成功响应返回null,或者为非成功响应抛出异常。

Specify whether a reply Message is expected. If not, this handler will simply return null for a successful response or throw an Exception for a non-successful response.

我的问题是:我在哪里可以捕获这些异常或在哪里可以检索此消息/响应?

My question is: where can I catch these exceptions or where can I retrieve this message/response?

推荐答案

@ ServiceActivator 有一个 outputChannel 属性:

/**
 * Specify the channel to which this service activator will send any replies.
 * @return The channel name.
 */
String outputChannel() default "";

这是成功回复。

任何只会抛出异常(独立于 setExpectReply())给调用者。在你的情况下,故事是关于 @InboundChannelAdapter 。在这种情况下,异常被捕获并包装到 ErrorMessage ,以发送到<$ c $上的 errorChannel C> @Poller 。默认情况下,它是全局 errorChannel https://docs.spring.io/spring-integration/docs/5.0.4.RELEASE/reference/html/configuration.html#namespace-errorhandler

Any exceptions (independently of the setExpectReply()) are just thrown to the caller. In your case the story is about an @InboundChannelAdapter. In this case the exception is caught and wrapped to the ErrorMessage to be sent to the errorChannel on the @Poller. It is a global errorChannel by default: https://docs.spring.io/spring-integration/docs/5.0.4.RELEASE/reference/html/configuration.html#namespace-errorhandler

但是您的代码中还有其他一些问题。我看到第二个订阅者 FILE_CHANNEL_PROCESSING 。如果它不是 PublishSubscribeChannel ,您将对发送到此频道的邮件进行循环分发。但那已经是另一回事了。请不要在这里问这个问题!

However you have some other problem in your code. I see the second subscriber to the FILE_CHANNEL_PROCESSING. If it's not a PublishSubscribeChannel, you are going to have a round-robin distribution for messages sent to this channel. But that's already a different story. Just don't ask that question here, please!

这篇关于处理Spring Integration File的预期返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆