|
| 1 | +package io.oasp.module.batch.common.impl; |
| 2 | + |
| 3 | +import org.springframework.batch.core.BatchStatus; |
| 4 | +import org.springframework.batch.core.Job; |
| 5 | +import org.springframework.batch.core.JobExecution; |
| 6 | +import org.springframework.batch.core.JobParameters; |
| 7 | +import org.springframework.batch.core.JobParametersIncrementer; |
| 8 | +import org.springframework.batch.core.JobParametersInvalidException; |
| 9 | +import org.springframework.batch.core.launch.JobLauncher; |
| 10 | +import org.springframework.batch.core.launch.support.SimpleJobLauncher; |
| 11 | +import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException; |
| 12 | +import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException; |
| 13 | +import org.springframework.batch.core.repository.JobRepository; |
| 14 | +import org.springframework.batch.core.repository.JobRestartException; |
| 15 | + |
| 16 | +/** |
| 17 | + * {@link JobLauncher} that extends the functionality provided by the standard {@link SimpleJobLauncher}: |
| 18 | + * <p> |
| 19 | + * For batches, which always restart from scratch (i.e. those marked with restartable="false"), the parameter's are |
| 20 | + * 'incremented' automatically using the {@link JobParametersIncrementer} from the job (usually by adding or modifying |
| 21 | + * the 'run.id' parameter). It is actually just a convenience functionality so that the one starting batches does not |
| 22 | + * have to change the parameters manually. |
| 23 | + * |
| 24 | + * @author Ludger Overbeck |
| 25 | + */ |
| 26 | +public class JobLauncherWithAdditionalRestartCapabilities extends SimpleJobLauncher { |
| 27 | + |
| 28 | + private JobRepository jobRepository; |
| 29 | + |
| 30 | + @Override |
| 31 | + public JobExecution run(final Job job, JobParameters jobParameters) throws JobExecutionAlreadyRunningException, |
| 32 | + JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException { |
| 33 | + |
| 34 | + if (!job.isRestartable()) { |
| 35 | + |
| 36 | + JobParameters originalParameters = jobParameters; |
| 37 | + while (this.jobRepository.isJobInstanceExists(job.getName(), jobParameters)) { |
| 38 | + |
| 39 | + // check if batch job is still running or was completed already |
| 40 | + // analogous to SimpleJobRepository#createJobExecution |
| 41 | + JobExecution jobExecution = this.jobRepository.getLastJobExecution(job.getName(), jobParameters); |
| 42 | + if (jobExecution.isRunning()) { |
| 43 | + throw new JobExecutionAlreadyRunningException("A job execution for this job is already running: " |
| 44 | + + jobExecution.getJobInstance()); |
| 45 | + } |
| 46 | + BatchStatus status = jobExecution.getStatus(); |
| 47 | + if (status == BatchStatus.COMPLETED || status == BatchStatus.ABANDONED) { |
| 48 | + throw new JobInstanceAlreadyCompleteException("A job instance already exists and is complete for parameters=" |
| 49 | + + originalParameters + ". If you want to run this job again, change the parameters."); |
| 50 | + } |
| 51 | + |
| 52 | + // if there is a NullPointerException executing the following statement |
| 53 | + // there has not been a JobParametersIncrementer set for the job |
| 54 | + jobParameters = job.getJobParametersIncrementer().getNext(jobParameters); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + return super.run(job, jobParameters); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public void setJobRepository(JobRepository jobRepository) { |
| 63 | + |
| 64 | + super.setJobRepository(jobRepository); |
| 65 | + this.jobRepository = jobRepository; |
| 66 | + } |
| 67 | + |
| 68 | +} |
0 commit comments