JobRestartException:JobInstance 已存在且不可重新启动

Nik*_*eek 5 java spring spring-batch spring-cloud-task

我正在将 Spring Batch 与 Spring 云任务结合使用。我的工作中有以下配置:

@Bean
    public Job jobDemo(
            @Value("${jobname}")String jobName,
            JobBuilderFactory jobBuilderFactory,
            JobCompletionNotificationListener listener
        ) {
        return jobBuilderFactory.get(jobName)
                .incrementer(new RunIdIncrementer())
                .preventRestart()
                .listener(listener)
                .flow(stepA())
                .end()
                .build();
    }
Run Code Online (Sandbox Code Playgroud)

我不想在作业中使用重新启动功能,这就是我将.preventRestart(). 我想在每次任务运行时启动一个新作业,即即使上次作业失败或停止或发生任何其他情况,也要运行作业的新实例。但我收到以下错误:

org.springframework.batch.core.repository.JobRestartException: JobInstance already exists and is not restartable
Run Code Online (Sandbox Code Playgroud)

仅在作业未成功完成的情况下才会发生这种情况。关于解决方案有什么想法吗?

Mic*_*lla 2

AJobInstance只能成功完成一次。JobParameter当您通过 Spring Boot 启动 Spring Batch 作业时,如果存在提供(如您所拥有) ,Spring Batch 会处理增加 a 的逻辑JobParametersIncrementer。然而......当 Spring Batch 执行增量操作时,它仅在前一个作业成功时才增量。就您而言,您希望它始终递增。因此,您将需要编写自己的CommandLineRunner始终递增JobParameters.

Spring BootJobLauncherCommandLineRunner是启动作业的代码所在的地方。您可能想要扩展它并重写它的execute方法,以确保作业参数始终递增。