【问题标题】:Spring batch job should be executed only once after the spring integration pollerSpring 批处理作业应仅在 Spring 集成轮询器之后执行一次
【发布时间】:2021-10-27 14:18:22
【问题描述】:
我只想在使用 spring 集成轮询器的目录中可用的文件时触发一次 spring 批处理。
假设在给定的时间点,我在监视目录中有 5 个文件触发 spring 批处理,所有 5 个文件只有一次。不是每个单独的文件。请帮帮我。
【问题讨论】:
标签:
spring-boot
spring-batch
spring-integration
【解决方案1】:
我怀疑 Spring Batch 中有一个开箱即用的 ItemProcessor 来处理文件列表。所以,听起来你有一些自定义的或者在你的工作中做一些事情来拆分那个列表等等。然而,在 Spring Integration 中没有这样的源来获取提供的目录中的整个文件列表。如果您不喜欢使用FileReadingMessageSource 来生成单个文件,您可能仍然需要自定义一个。
我目前看到的一种简单方法是为轮询器设置一个虚拟 MessageSource,例如只生成一个静态目录作为下一个流端点的有效负载:
@InboundChannelAdapter("fileListChannel")
@Bean
Supplier<String> dirSource() {
return () -> "myDir";
}
fileListChannel 消费者也是一个简单的服务激活器,用于从该目录获取文件列表:
@ServiceActivator(inputChannel = "fileListChannel", outputChannel = "startJobChannel")
File[] obtainFileList(File dir) {
return dir.listFiles();
}
这对你有意义吗?