Spring 检索与筛选器匹配的所有文件一次
我正在尝试从入站ftp apdater流式处理程序中使用筛选器获取文件计数,因此在处理完所有文件后,我想启动一个远程shell,或者是否有其他方法可以知道适配器已完成发送消息 我已经尝试过使用CompositeFileListFilter覆盖公共列表过滤器文件(F[]文件)方法,但它从未被调用 现在我使用的是固定的文件计数,但它应该是动态的 我在CompositeFileListFilter上重写了此方法Spring 检索与筛选器匹配的所有文件一次,spring,spring-integration,spring-integration-dsl,Spring,Spring Integration,Spring Integration Dsl,我正在尝试从入站ftp apdater流式处理程序中使用筛选器获取文件计数,因此在处理完所有文件后,我想启动一个远程shell,或者是否有其他方法可以知道适配器已完成发送消息 我已经尝试过使用CompositeFileListFilter覆盖公共列表过滤器文件(F[]文件)方法,但它从未被调用 现在我使用的是固定的文件计数,但它应该是动态的 我在CompositeFileListFilter上重写了此方法 @覆盖 公共列表筛选器文件(F[]文件){ log.info(“收到的{}个文件”,fil
@覆盖
公共列表筛选器文件(F[]文件){
log.info(“收到的{}个文件”,files.length);
返回super.filterFiles(文件);
}
我有以下积分流,使用原子计数器直到3,它应该是3:
AtomicInteger messageCounter=新的AtomicInteger(0);
返回IntegrationFlows.from(Ftp.inboundStreamingAdapter(goldv5template())
.remoteDirectory(“/inputFolder”)
.filter(新的CompositeFileListFilterWithCount(){{
addFilter(新的FtpSimplePatternFileListFilter(“pattern1.*));
addFilter(新的FtpSimplePatternFileListFilter(“pattern2.*));
addFilter(新的FtpSimplePatternFileListFilter(“pattern3.*));
}})
,pollerConfiguration)
.transform(Transformers.fromStream(StandardCharsets.UTF_8.toString())
.log(message->“进程文件”+message.getHeaders().get(FileHeaders.REMOTE_文件))
.handle(消息->{
int numericValue=messageCounter.incrementAndGet();
log.info(“数值:{}”,numericValue);
如果(数值==3){
messageCounter.set(0);
log.info(“现在在这里启动远程shell”);
}
},e->e.advice(在()之后)
.get();
如果我不使用计数器,我会为每个文件得到一个远程shell调用,我只需要调用一次,只有在流完成时,它是基于cronjob进行调度的,所以我只想在最后调用它一次
我使用1s的固定延迟进行测试,但它一天只运行三次,我必须在每个时钟获取所有时间
这是我的测试轮询配置:
sourcePollingChannelAdapterSpec->sourcePollingChannelAdapterSpec.poller(pollerFactory->pollerFactory.fixedRate(1000L))
更新
我尝试了Artem的建议,但我有一个奇怪的行为,我试图在一次投票中获取某个ftp文件夹中的所有文件,因此阅读文档:
如果将每次轮询的最大消息数设置为1(默认值),则它一次仅处理一个文件,间隔由触发器定义,基本上是“一次轮询===一个文件”
对于典型的文件传输用例,您很可能想要相反的行为:处理每次轮询所能处理的所有文件,然后等待下一次轮询。如果是这种情况,请将每次轮询的最大消息数设置为-1。然后,在每次轮询时,适配器会尝试生成尽可能多的消息
因此,我已将每次轮询的最大消息数设置为-1,因此每次轮询都会提供每个文件。
我只在take.xml文件中添加了一个过滤器,为了防止重复,我还添加了一个acceptOnceFilter,但是ftp流适配器给了我无限次的相同文件,这是没有意义的,我在这个测试中使用了10秒的固定延迟
2019-07-23 10:32:04.308 INFO 9008 --- [ scheduling-1] o.s.integration.handler.LoggingHandler : process2 file sample1.xml
2019-07-23 10:32:04.312 INFO 9008 --- [ scheduling-1] o.s.integration.ftp.session.FtpSession : File has been successfully transferred to: /output/sample1.xml
2019-07-23 10:32:04.313 INFO 9008 --- [ scheduling-1] i.d.e.v.job.factory.TestFlowFactory : Advice after handle.
2019-07-23 10:32:04.313 INFO 9008 --- [ scheduling-1] i.d.e.v.job.factory.TestFlowFactory : ________________________________
2019-07-23 10:32:04.315 INFO 9008 --- [ scheduling-1] o.s.integration.handler.LoggingHandler : process file sample2.xml
2019-07-23 10:32:04.324 INFO 9008 --- [ scheduling-1] o.s.integration.ftp.session.FtpSession : File has been successfully transferred to: /output/sample2.xml
2019-07-23 10:32:04.324 INFO 9008 --- [ scheduling-1] i.d.e.v.job.factory.TestFlowFactory : Advice after handle.
2019-07-23 10:32:04.324 INFO 9008 --- [ scheduling-1] i.d.e.v.job.factory.TestFlowFactory : ________________________________
2019-07-23 10:32:04.326 INFO 9008 --- [ scheduling-1] o.s.integration.handler.LoggingHandler : process file sample3.xml
2019-07-23 10:32:04.330 INFO 9008 --- [ scheduling-1] o.s.integration.ftp.session.FtpSession : File has been successfully transferred to: /output/sample3.xml
2019-07-23 10:32:04.331 INFO 9008 --- [ scheduling-1] i.d.e.v.job.factory.TestFlowFactory : Advice after handle.
2019-07-23 10:32:04.331 INFO 9008 --- [ scheduling-1] i.d.e.v.job.factory.TestFlowFactory : ________________________________
2019-07-23 10:32:04.333 INFO 9008 --- [ scheduling-1] o.s.integration.handler.LoggingHandler : process file sample4.xml
2019-07-23 10:32:04.337 INFO 9008 --- [ scheduling-1] o.s.integration.ftp.session.FtpSession : File has been successfully transferred to: /output/sample4.xml
2019-07-23 10:32:04.338 INFO 9008 --- [ scheduling-1] i.d.e.v.job.factory.TestFlowFactory : Advice after handle.
2019-07-23 10:32:04.338 INFO 9008 --- [ scheduling-1] i.d.e.v.job.factory.TestFlowFactory : ________________________________
2019-07-23 10:32:04.341 INFO 9008 --- [ scheduling-1] o.s.integration.handler.LoggingHandler : process file sample1.xml
2019-07-23 10:32:04.345 INFO 9008 --- [ scheduling-1] o.s.integration.ftp.session.FtpSession : File has been successfully transferred to: /output/sample1.xml
2019-07-23 10:32:04.346 INFO 9008 --- [ scheduling-1] i.d.e.v.job.factory.TestFlowFactory : Advice after handle.
2019-07-23 10:32:04.346 INFO 9008 --- [ scheduling-1] i.d.e.v.job.factory.TestFlowFactory : ________________________________
2019-07-23 10:32:04.347 INFO 9008 --- [ scheduling-1] o.s.integration.handler.LoggingHandler : process file sample2.xml
2019-07-23 10:32:04.351 INFO 9008 --- [ scheduling-1] o.s.integration.ftp.session.FtpSession : File has been successfully transferred to: /output/sample2.xml
2019-07-23 10:32:04.351 INFO 9008 --- [ scheduling-1] i.d.e.v.job.factory.TestFlowFactory : Advice after handle.
2019-07-23 10:32:04.351 INFO 9008 --- [ scheduling-1] i.d.e.v.job.factory.TestFlowFactory : ________________________________
2019-07-23 10:32:04.353 INFO 9008 --- [ scheduling-1] o.s.integration.handler.LoggingHandler : process file sample3.xml
2019-07-23 10:32:04.356 INFO 9008 --- [ scheduling-1] o.s.integration.ftp.session.FtpSession : File has been successfully transferred to: /output/sample3.xml
2019-07-23 10:32:04.356 INFO 9008 --- [ scheduling-1] i.d.e.v.job.factory.TestFlowFactory : Advice after handle.
2019-07-23 10:32:04.357 INFO 9008 --- [ scheduling-1] i.d.e.v.job.factory.TestFlowFactory : ________________________________
2019-07-23 10:32:04.358 INFO 9008 --- [ scheduling-1] o.s.integration.handler.LoggingHandler : process file sample4.xml
...............................
返回集成流
.from(Ftp.inboundStreamingAdapter(testFlowTemplate())
.remoteDirectory(“/inputTestFlow”)
.filter(新的CompositeFileListFilter(){{
addFilter(新的AcceptOnceFileListFilter());
addFilter(新的FtpSimplePatternFileListFilter(“*.xml”);
}})
,sourcePollingChannelAdapterSpec->sourcePollingChannelAdapterSpec.poller(pollerConfiguration.maxMessagesPerPoll(-1)))
.transform(Transformers.fromStream(StandardCharsets.UTF_8.toString())
.log(消息->{
execution.setStartDate(新日期());
返回“进程文件”+message.getHeaders().get(FileHeaders.REMOTE_文件);
})
.handle(Ftp.outboundAdapter(ftpserver.PC_LOCAL.getFactory(),FileExistsMode.REPLACE)
.useTemporaryFileName(false)
.fileNameExpression(“headers['”+FileHeaders.REMOTE_FILE+“']”)
.remoteDirectory(“/output/”)
,e->e.advice(testFlowAfter())
)
.get();
更新2
我实现了创建此自定义过滤器所需的功能:
.filter(新的FileListFilter(){
私有最终集seenSet=newhashset();
非公开执行日期;
@凌驾
公共列表筛选器文件(FTPFile[]文件){
返回数组.stream(文件).filter(ftpFile->{
if(lastExecution!=null&&TimeUnit.millizes.toSeconds(new Date().getTime()-lastExecution.getTime())>=10L){
this.seenSet.clear();
}
lastExecution=新日期();
if(ftpFile.getName().endsWith(“.xml”)){
返回此.seenSet.add(ftpFile.getRawListing());
}
返回false;
}).collect(Collectors.toList());
}
})
但是我使用了一个手工制作的10秒间隔,这对于我的需要来说是可以的,还有其他聪明的方法可以根据触发器来改进代码吗?我认为cron触发器不是一个正确的解决方案
@Override
public List<F> filterFiles(F[] files) {
List<F> filteredFiles = super.filterFiles(files);
log.info("received {} files", filteredFiles.size());
return filteredFiles;
}
/**
* Indicates that this filter supports filtering a single file.
* Filters that return true <b>must</b> override {@link #accept(Object)}.
* Default false.
* @return true to allow external calls to {@link #accept(Object)}.
* @since 5.2
* @see #accept(Object)
*/
default boolean supportsSingleFileFiltering() {
return false;
}