弹簧批处理停止来自外部类的作业执行

我有一个现有的春季批处理项目,该项目有多个步骤。我想修改一个步骤,以便我可以停止作业:。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


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


@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;

        }

    }


我试图实现,但没有运气,我认为是因为方法期望从方法中得到一个,它不关心类的其余部分。StepExecutionListenerStepReaderreaderStepBuilderFactoryItemReaderreader.read()


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


爪哇岛春天弹簧批次


qq_笑_17
浏览 73回答 1
1回答

呼啦一阵风

我正在寻找一些想法或解决方案,以便在捕获到无输入文件异常时能够停止整个作业(而不是失败)。这是一种常见的模式,在参考文档的“找不到输入时处理步骤完成”部分中进行了详细介绍。该部分中的示例显示了如何在找不到输入文件时使作业失败,但由于您希望停止作业而不是失败,因此可以在侦听器中使用,并且您的作业将以状态 结束。在您的示例中,您将向该步骤添加该侦听器。StepExecution#setTerminateOnly();STOPPEDMyStep但是,我建议添加一个预验证步骤,如果没有文件,则停止作业。下面是一个快速示例: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@EnableBatchProcessingpublic class MyJob {&nbsp; &nbsp; @Autowired&nbsp; &nbsp; private JobBuilderFactory jobs;&nbsp; &nbsp; @Autowired&nbsp; &nbsp; private StepBuilderFactory steps;&nbsp; &nbsp; @Bean&nbsp; &nbsp; public Step fileValidationStep() {&nbsp; &nbsp; &nbsp; &nbsp; return steps.get("fileValidationStep")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .tasklet((contribution, chunkContext) -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // TODO add code to check if the file exists&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("file not found");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chunkContext.getStepContext().getStepExecution().setTerminateOnly();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return RepeatStatus.FINISHED;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .build();&nbsp; &nbsp; }&nbsp; &nbsp; @Bean&nbsp; &nbsp; public Step fileProcessingStep() {&nbsp; &nbsp; &nbsp; &nbsp; return steps.get("fileProcessingStep")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .tasklet((contribution, chunkContext) -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("processing file");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return RepeatStatus.FINISHED;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .build();&nbsp; &nbsp; }&nbsp; &nbsp; @Bean&nbsp; &nbsp; public Job job() {&nbsp; &nbsp; &nbsp; &nbsp; return jobs.get("job")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .start(fileValidationStep())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .next(fileProcessingStep())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .build();&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(String[] args) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; ApplicationContext context = new AnnotationConfigApplicationContext(MyJob.class);&nbsp; &nbsp; &nbsp; &nbsp; JobLauncher jobLauncher = context.getBean(JobLauncher.class);&nbsp; &nbsp; &nbsp; &nbsp; Job job = context.getBean(Job.class);&nbsp; &nbsp; &nbsp; &nbsp; JobExecution jobExecution = jobLauncher.run(job, new JobParameters());&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Job status: " + jobExecution.getExitStatus().getExitCode());&nbsp; &nbsp; }}该示例打印:file not foundJob status: STOPPED希望这有帮助。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java