如果文件输入资源不存在,请跳过步骤

问题描述

我在一项作业中有12个步骤,其中的步骤是从csv和txt文件读取的。如果要使用此https://docs.spring.io/spring-batch/docs/current/reference/html/step.html#programmaticFlowDecisions之后的JobExecutionDecider,如果目录中不存在与该步骤相对应的文件,我将尝试添加跳过该步骤的功能。当目录中不存在文件时,它工作正常。相反,如果文件存在,则从第一个开始停止。

jobBuilderFactory.get("importFileJob")
            .listener( new ImportFileJobListener() )
            .incrementer( new RunIdIncrementer() )
            .start( truncateAnagraficheTableStep( QUERY_TRUNCATE_INTERFACE_IN_ANAGRAFICHE ) )
            .next( new Decider( directory,file01Prefix ) ).on( "FAILED" ).to( truncateContrattiTableStep( QUERY_TRUNCATE_INTERFACE_IN_CONTRATTI ) )
            .from( new Decider( directory,file01Prefix ) ).on( "COMPLETED" ).to( anagraficheStep( QUERY_INSERT_INTERFACE_IN_ANAGRAFICHE ) )
            .next( anagraficheStep( QUERY_INSERT_INTERFACE_IN_ANAGRAFICHE ) )
            .next( truncateContrattiTableStep( QUERY_TRUNCATE_INTERFACE_IN_CONTRATTI ) )
            .next( new Decider( directory,file02Prefix ) ).on( "FAILED" ).to( truncateCapitalLimitTableStep( QUERY_TRUNCATE_INTERFACE_IN_CAPITAL_LIMIT ) )
            .from( new Decider( directory,file02Prefix ) ).on( "COMPLETED" ).to( contrattiStep( QUERY_INSERT_INTERFACE_IN_CONTRATTI ) )
            .next( contrattiStep( QUERY_INSERT_INTERFACE_IN_CONTRATTI ) )
            .next( truncateCapitalLimitTableStep( QUERY_TRUNCATE_INTERFACE_IN_CAPITAL_LIMIT ))
            .next( new Decider( directory,file03Prefix ) ).on( "FAILED" ).to( truncateEsitiPefTableStep( QUERY_TRUNCATE_INTERFACE_IN_ESITI_PEF ) )
            .from( new Decider( directory,file03Prefix ) ).on( "COMPLETED" ).to( capitalLimitStep( QUERY_INSERT_INTERFACE_IN_CAPITAL_LIMIT ) )
            .next( capitalLimitStep( QUERY_INSERT_INTERFACE_IN_CAPITAL_LIMIT ))
            .next( moveTextFilesStep() )
            .next( truncateEsitiPefTableStep( QUERY_TRUNCATE_INTERFACE_IN_ESITI_PEF ) )
            .next( new Decider( directory,file04Prefix ) ).on( "FAILED" ).to( truncateStatoOperazioniTableStep( QUERY_TRUNCATE_INTERFACE_IN_STATO_OPERAZIONI ) )
            .from( new Decider( directory,file04Prefix ) ).on( "COMPLETED" ).to( esitiPefStep() )
            .next(esitiPefStep())
            .next( truncateStatoOperazioniTableStep( QUERY_TRUNCATE_INTERFACE_IN_STATO_OPERAZIONI ) )
            .next( new Decider( directory,file05Prefix ) ).on( "FAILED" ).fail()
            .from( new Decider( directory,file05Prefix ) ).on( "COMPLETED" ).to( statoOperazioniStep() )
            .next( statoOperazioniStep() )
            .next( moveExcelFilesStep() )
            .end()
            .build();

这是决定者的代码

public class Decider implements JobExecutionDecider {

private static Logger logger = Logger.getLogger("techLog");
private File directory;
private String filePrefix;


public Decider(File directory,String filePrefix) {
    super();
    this.directory = directory;
    this.filePrefix = filePrefix;
}


@Override
public FlowExecutionStatus decide(JobExecution jobExecution,StepExecution stepExecution) {
    // TODO Auto-generated method stub
    String status;
    
    File[] inputFiles = directory.listFiles((dir,name) -> name.startsWith(filePrefix) );
    if ( inputFiles.length == 0 ) {
        logger.error( "Input resource does not exist file " + directory + "\\" + filePrefix );
        status = "FAILED";
    }
    else {
        status = "COMPLETED";
    }
    return new FlowExecutionStatus(status);
}

}

解决方法

这是您的决策程序实现的问题。您可以根据文件的有无来指定FlowExecutionStatus来返回。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...