启动Spring Batch启动失败的文件

时间:2018-06-14 15:01:29

标签: spring-integration spring-batch

我的用例如下:Spring集成正在监听一些文件夹。添加文件时,它运行spring batch,文件名为param。如果ExecutionListener.afterJob上的作业失败,则fileInputAdapter将被停止(fileInputAdapter.stop());。之后将发送coll fileInputAdapter.start();下一个文件。但对于我的情况是nasser启动批处理具有相同的失败文件。                                                     

    <int:channel id="fileInputChannel"></int:channel>
    <int:inbound-channel-adapter id="fileInputAdapter" ref="fileReadingMessageAdapter" method="receive" channel="fileInputChannel" auto-startup="true">
        <int:poller fixed-rate="1000"/>
    </int:inbound-channel-adapter>



@Configuration
@IntegrationComponentScan
@EnableIntegration
public class IntegrationConfig {
    @Bean
    @ServiceActivator(inputChannel = "errorChannel", outputChannel = "nullChanel")
    protected ErrorLogger errorLogger(JobLauncher jobLauncher) {
        return new ErrorLogger();
    }


    @Bean
    @ServiceActivator(inputChannel = "jobChannel", outputChannel = "errorChannel")
    protected JobLaunchingMessageHandler launcher(JobLauncher jobLauncher) {
        return new JobLaunchingMessageHandler(jobLauncher);
    }
    @Transformer(inputChannel = "fileInputChannel", outputChannel = "jobChannel")
public JobLaunchRequest transform(File aFile) {
    String fileName = aFile.getAbsolutePath();
    JobParameters jobParameters = new JobParametersBuilder().addString("input.file.name", fileName)
            .toJobParameters();
    logger.warn("JobLaunchRequest for file :" + fileName);
    JobLaunchRequest request = new JobLaunchRequest(job, jobParameters);
    return request;
    }
}

@Component
public class FileReadingMessageAdapter extends FileReadingMessageSource {

    @Autowired
    public FileReadingMessageAdapter(ConcurrentMetadataStore metadataStore) {

        super(Comparator.comparing(File::getName));

        final CompositeFileListFilter<File> compositeFileListFilter = new CompositeFileListFilter<>();
        FileSystemPersistentAcceptOnceFileListFilter perstFilter = new FileSystemPersistentAcceptOnceFileListFilter(
                metadataStore, "");
        compositeFileListFilter.addFilter(perstFilter);
        compositeFileListFilter.addFilter(new SimplePatternFileListFilter("*.txt"));
        compositeFileListFilter.addFilter(new LastModifiedFileListFilter(3));
        this.setFilter(compositeFileListFilter);
        this.setDirectory(new File("tempDropFolder"));
        //this.setDirectory(new File("C:/Temp/myfiles/"));
        this.setScanEachPoll(true);
        this.setUseWatchService(true);
    }
}


@Component
public class ExecutionListener implements JobExecutionListener {
    private @Autowired SourcePollingChannelAdapter fileInputAdapter;
    @Override
    public void afterJob(JobExecution jobExecution) {
        List<Throwable> allFailureExceptions = jobExecution.getAllFailureExceptions();
        ExitStatus es = jobExecution.getExitStatus();
        BatchStatus status = jobExecution.getStatus();
        if (jobExecution.getStatus().equals(BatchStatus.FAILED)) {
                fileInputAdapter.stop();
}

1 个答案:

答案 0 :(得分:0)

不确定什么是“nasser”,但如果您在问题中编辑和修复语法,那就太棒了。

您使用FileSystemPersistentAcceptOnceFileListFilter后不应该发生什么。

另一方面,你有几个问题:

  1. 为什么在出现错误时停止通道适配器?
  2. 为什么要延长FileReadingMessageSource?标准版有什么问题?
  3. 另外:如果您使用LastModifiedFileListFilter,则应考虑将其放在合并的开头,然后切换到ChainFileListFilterhttps://docs.spring.io/spring-integration/docs/5.0.6.RELEASE/reference/html/files.html#file-reading