0
votes

I have deployed a Spring Batch project as a task on Spring Cloud Data Flow.

I have launched the task for the first time (It was going to run about 5 minutes which made the status of task is "running"). However, I have relaunched this task for the second time when it's status is running and it worked again.

1) How can I configure that the task could not be relaunched until it's completed status for the last time. 2) Besides, I found that when stopping the job, it will execute until it finishes the current step(My job has 2 steps and the second step was not executed ). However, are there any ways to shut down it (step 1) immediately when I stop the job?

2
For better help sooner, edit to add a minimal reproducible example or Short, Self Contained, Correct Example. - yur

2 Answers

1
votes

You can use JobExplorer class for checking running jobs, see jobExplorer.findRunningJobExecutions(jobName) method.

In your JobLauncher class you can something like that:

@Component
public class MyJobLauncher {

    private static final Logger LOG = LoggerFactory.getLogger(MyJobLauncher.class);

    private final Job job;

    private final JobLauncher jobLauncher;

    private final JobExplorer jobExplorer;

    @Autowired
    public MyJobLauncher(@Qualifier("myJob") Job job, JobLauncher jobLauncher, JobExplorer jobExplorer) {
        this.job = job;
        this.jobLauncher = jobLauncher;
        this.jobExplorer = jobExplorer;
    }

    public void launchJob() throws JobParametersInvalidException, JobExecutionAlreadyRunningException,
            JobRestartException, JobInstanceAlreadyCompleteException, IOException {

        if (jobExplorer.findRunningJobExecutions("myJob").isEmpty()) {
            LOG.info("Starting myJob");
            jobLauncher.run(job, new JobParameters());
            LOG.info("Stopping myJob");
        }
    }
}

For shutting down the job, check JobExecution.stop() method which can be fetched from jobExplorer.getJobExecution() method.

1
votes

How to avoid relaunching task while it's status is still running on Spring Cloud Data Flow

Spring Cloud Task 2.0.0 added a feature that allows you to prevent tasks from running concurrently. See https://spring.io/blog/2018/05/07/spring-cloud-task-2-0-0-release-is-now-available#restricting-concurrent-task-execution

You need to activate the spring.cloud.task.singleInstanceEnabled=true property.

You can find more details in the Restricting Spring Cloud Task Instances section.