gpt4 book ai didi

java - 处理 Spring 集成文件的预期返回

转载 作者:太空宇宙 更新时间:2023-11-04 10:32:10 26 4
gpt4 key购买 nike

我有以下 Spring 集成文件配置文件:

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

对于来自this documentationFileWritingMessageHandler它表示如果设置了 setExpectReply(true):

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.

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

最佳答案

@ServiceActivator有一个 outputChannel属性:

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

这是成功回复的结果。

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

但是您的代码中还存在其他问题。我看到 FILE_CHANNEL_PROCESSING 的第二个订阅者 。如果不是PublishSubscribeChannel ,您将对发送到此 channel 的消息进行循环分配。但这已经是一个不同的故事了。请不要在这里问这个问题!

关于java - 处理 Spring 集成文件的预期返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49880845/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com