繁体   English   中英

Spring Batch从外部类停止作业执行

[英]Spring Batch stop job execution from external class

我有一个现有的Spring Batch项目,该项目具有多个步骤。 我想修改一个步骤,以便停止工作: jobExecution.getStatus() == STOPPED

我的步骤:

@Autowired
public StepBuilderFactory stepBuilderFactory;
@Autowired
private StepReader reader;
@Autowired
private StepProcessor processor;
@Autowired
private StepWriter writer;
@Autowired
public GenericListener listener;
@Bean
@JobScope
@Qualifier("mystep")
public Step MyStep() throws ReaderException {
    return stepBuilderFactory.get("mystep")
            .reader(reader.read())
            .listener(listener)
            .processor(processor)
            .writer(writer)
            .build();
}

GenericListener实现ItemReadListener, ItemProcessListener, ItemWriteListener ,并在基本上写入日志的方法之前和之后重写。

这里的重点是StepReader类及其返回FlatFileItemReader read()方法:

@Component
public class StepReader {
    public static final String DELIMITER = "|";
    @Autowired
    private ClassToAccessProperties classToAccessProperties;
    private Logger log = Logger.create(StepReader.class);
    @Autowired
    private FlatFileItemReaderFactory<MyObject> flatFileItemReaderFactory;

    public ItemReader<MyObject> read() throws ReaderException {
        try {
            String csv = classToAccessProperties.getInputCsv();
            FlatFileItemReader<MyObject> reader = flatFileItemReaderFactory.create(csv, getLineMapper());
            return reader;
        } catch (ReaderException | EmptyInputfileException | IOException e) {
            throw new ReaderException(e);
        } catch (NoInputFileException e) {
            log.info("Oh no !! No input file");
            // Here I want to stop the job
            return null;
        }
    }
    private LineMapper<MyObject> getLineMapper () {
        DefaultLineMapper<MyObject> mapper = new DefaultLineMapper<>();
        DelimitedLineTokenizer delimitedLineTokenizer = new DelimitedLineTokenizer();
        delimitedLineTokenizer.setDelimiter(DELIMITER);
        mapper.setLineTokenizer(delimitedLineTokenizer);
        mapper.setFieldSetMapper(new MyObjectFieldSetMapper());
        return mapper;
    }
}

我试图执行StepExecutionListenerStepReader ,但没有运气,我想是因为reader在方法StepBuilderFactory期待一个ItemReaderreader.read()方法,它不关心班上的其他同学。

我正在寻找想法或解决方案,以便能够在NoInputFileException时停止整个工作(而不是使其失败)。

我正在寻找想法或解决方案,以便能够在捕获到NoInputFileException时停止整个工作(而不是使其失败)。

这是一种常见的模式,在参考文档的“ 找不到输入时处理步骤完成”部分中有详细说明。 该部分中的示例显示了在找不到输入文件时如何使作业失败,但是由于要停止作业而不是使其失败,因此可以使用StepExecution#setTerminateOnly(); 在监听器中,您的工作将以STOPPED状态结束。 在您的示例中,您可以将该侦听器添加到MyStep步骤。

但是,我建议添加一个预验证步骤,如果没有文件,则停止该作业。 这是一个简单的示例:

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableBatchProcessing
public class MyJob {

    @Autowired
    private JobBuilderFactory jobs;

    @Autowired
    private StepBuilderFactory steps;

    @Bean
    public Step fileValidationStep() {
        return steps.get("fileValidationStep")
                .tasklet((contribution, chunkContext) -> {
                    // TODO add code to check if the file exists
                    System.out.println("file not found");
                    chunkContext.getStepContext().getStepExecution().setTerminateOnly();
                    return RepeatStatus.FINISHED;
                })
                .build();
    }

    @Bean
    public Step fileProcessingStep() {
        return steps.get("fileProcessingStep")
                .tasklet((contribution, chunkContext) -> {
                    System.out.println("processing file");
                    return RepeatStatus.FINISHED;
                })
                .build();
    }

    @Bean
    public Job job() {
        return jobs.get("job")
                .start(fileValidationStep())
                .next(fileProcessingStep())
                .build();
    }

    public static void main(String[] args) throws Exception {
        ApplicationContext context = new AnnotationConfigApplicationContext(MyJob.class);
        JobLauncher jobLauncher = context.getBean(JobLauncher.class);
        Job job = context.getBean(Job.class);
        JobExecution jobExecution = jobLauncher.run(job, new JobParameters());
        System.out.println("Job status: " + jobExecution.getExitStatus().getExitCode());
    }

}

该示例打印:

file not found
Job status: STOPPED

希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM