Skip to content

Commit cb1d6b1

Browse files
fmbenhassinesnicoll
authored andcommitted
Migrate to Spring Batch 6
See gh-46216
1 parent 53b66d0 commit cb1d6b1

File tree

11 files changed

+75
-171
lines changed

11 files changed

+75
-171
lines changed

spring-boot-project/spring-boot-batch/src/main/java/org/springframework/boot/batch/autoconfigure/BatchAutoConfiguration.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222

2323
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
2424
import org.springframework.batch.core.configuration.support.DefaultBatchConfiguration;
25+
import org.springframework.batch.core.configuration.support.JdbcDefaultBatchConfiguration;
2526
import org.springframework.batch.core.converter.JobParametersConverter;
26-
import org.springframework.batch.core.explore.JobExplorer;
27-
import org.springframework.batch.core.launch.JobLauncher;
27+
import org.springframework.batch.core.launch.JobOperator;
2828
import org.springframework.batch.core.repository.ExecutionContextSerializer;
2929
import org.springframework.batch.core.repository.JobRepository;
3030
import org.springframework.beans.factory.ObjectProvider;
@@ -72,7 +72,7 @@
7272
*/
7373
@AutoConfiguration(after = { DataSourceAutoConfiguration.class, TransactionAutoConfiguration.class },
7474
afterName = "org.springframework.boot.hibernate.autoconfigure.HibernateJpaAutoConfiguration")
75-
@ConditionalOnClass({ JobLauncher.class, DataSource.class, DatabasePopulator.class })
75+
@ConditionalOnClass({ JobOperator.class, DataSource.class, DatabasePopulator.class })
7676
@ConditionalOnBean({ DataSource.class, PlatformTransactionManager.class })
7777
@ConditionalOnMissingBean(value = DefaultBatchConfiguration.class, annotation = EnableBatchProcessing.class)
7878
@EnableConfigurationProperties(BatchProperties.class)
@@ -82,9 +82,9 @@ public class BatchAutoConfiguration {
8282
@Bean
8383
@ConditionalOnMissingBean
8484
@ConditionalOnBooleanProperty(name = "spring.batch.job.enabled", matchIfMissing = true)
85-
public JobLauncherApplicationRunner jobLauncherApplicationRunner(JobLauncher jobLauncher, JobExplorer jobExplorer,
85+
public JobLauncherApplicationRunner jobLauncherApplicationRunner(JobOperator jobOperator,
8686
JobRepository jobRepository, BatchProperties properties) {
87-
JobLauncherApplicationRunner runner = new JobLauncherApplicationRunner(jobLauncher, jobExplorer, jobRepository);
87+
JobLauncherApplicationRunner runner = new JobLauncherApplicationRunner(jobOperator, jobRepository);
8888
String jobName = properties.getJob().getName();
8989
if (StringUtils.hasText(jobName)) {
9090
runner.setJobName(jobName);
@@ -99,7 +99,7 @@ public JobExecutionExitCodeGenerator jobExecutionExitCodeGenerator() {
9999
}
100100

101101
@Configuration(proxyBeanMethods = false)
102-
static class SpringBootBatchConfiguration extends DefaultBatchConfiguration {
102+
static class SpringBootBatchConfiguration extends JdbcDefaultBatchConfiguration {
103103

104104
private final DataSource dataSource;
105105

@@ -173,6 +173,7 @@ protected ExecutionContextSerializer getExecutionContextSerializer() {
173173
: super.getExecutionContextSerializer();
174174
}
175175

176+
@SuppressWarnings("removal")
176177
@Override
177178
protected JobParametersConverter getJobParametersConverter() {
178179
return (this.jobParametersConverter != null) ? this.jobParametersConverter

spring-boot-project/spring-boot-batch/src/main/java/org/springframework/boot/batch/autoconfigure/BatchConversionServiceCustomizer.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,12 @@
1616

1717
package org.springframework.boot.batch.autoconfigure;
1818

19-
import org.springframework.batch.core.configuration.support.DefaultBatchConfiguration;
2019
import org.springframework.core.convert.support.ConfigurableConversionService;
2120

2221
/**
2322
* Callback interface that can be implemented by beans wishing to customize the
24-
* {@link ConfigurableConversionService} that is
25-
* {@link DefaultBatchConfiguration#getConversionService provided by
26-
* DefaultBatchConfiguration} while retaining its default auto-configuration.
23+
* {@link ConfigurableConversionService} that is used by the batch infrastructure
24+
* while retaining its default auto-configuration.
2725
*
2826
* @author Claudio Nave
2927
* @since 4.0.0

spring-boot-project/spring-boot-batch/src/main/java/org/springframework/boot/batch/autoconfigure/JobExecutionEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package org.springframework.boot.batch.autoconfigure;
1818

19-
import org.springframework.batch.core.JobExecution;
19+
import org.springframework.batch.core.job.JobExecution;
2020
import org.springframework.context.ApplicationEvent;
2121

2222
/**

spring-boot-project/spring-boot-batch/src/main/java/org/springframework/boot/batch/autoconfigure/JobExecutionExitCodeGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import java.util.List;
2020
import java.util.concurrent.CopyOnWriteArrayList;
2121

22-
import org.springframework.batch.core.JobExecution;
22+
import org.springframework.batch.core.job.JobExecution;
2323
import org.springframework.boot.ExitCodeGenerator;
2424
import org.springframework.context.ApplicationListener;
2525

spring-boot-project/spring-boot-batch/src/main/java/org/springframework/boot/batch/autoconfigure/JobLauncherApplicationRunner.java

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,18 @@
2727
import org.apache.commons.logging.LogFactory;
2828

2929
import org.springframework.batch.core.BatchStatus;
30-
import org.springframework.batch.core.Job;
31-
import org.springframework.batch.core.JobExecution;
32-
import org.springframework.batch.core.JobExecutionException;
33-
import org.springframework.batch.core.JobParameter;
34-
import org.springframework.batch.core.JobParameters;
35-
import org.springframework.batch.core.JobParametersBuilder;
36-
import org.springframework.batch.core.JobParametersInvalidException;
3730
import org.springframework.batch.core.configuration.JobRegistry;
3831
import org.springframework.batch.core.converter.DefaultJobParametersConverter;
3932
import org.springframework.batch.core.converter.JobParametersConverter;
40-
import org.springframework.batch.core.explore.JobExplorer;
41-
import org.springframework.batch.core.launch.JobLauncher;
33+
import org.springframework.batch.core.job.Job;
34+
import org.springframework.batch.core.job.JobExecution;
35+
import org.springframework.batch.core.job.JobExecutionException;
36+
import org.springframework.batch.core.job.parameters.JobParameter;
37+
import org.springframework.batch.core.job.parameters.JobParameters;
38+
import org.springframework.batch.core.job.parameters.JobParametersBuilder;
39+
import org.springframework.batch.core.job.parameters.JobParametersInvalidException;
40+
import org.springframework.batch.core.launch.JobOperator;
41+
import org.springframework.batch.core.launch.NoSuchJobException;
4242
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
4343
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
4444
import org.springframework.batch.core.repository.JobRepository;
@@ -55,7 +55,7 @@
5555
import org.springframework.util.StringUtils;
5656

5757
/**
58-
* {@link ApplicationRunner} to {@link JobLauncher launch} Spring Batch jobs. If a single
58+
* {@link ApplicationRunner} to {@link JobOperator launch} Spring Batch jobs. If a single
5959
* job is found in the context, it will be executed by default. If multiple jobs are
6060
* found, launch a specific job by providing a jobName.
6161
*
@@ -78,9 +78,7 @@ public class JobLauncherApplicationRunner
7878

7979
private JobParametersConverter converter = new DefaultJobParametersConverter();
8080

81-
private final JobLauncher jobLauncher;
82-
83-
private final JobExplorer jobExplorer;
81+
private final JobOperator jobOperator;
8482

8583
private final JobRepository jobRepository;
8684

@@ -96,17 +94,14 @@ public class JobLauncherApplicationRunner
9694

9795
/**
9896
* Create a new {@link JobLauncherApplicationRunner}.
99-
* @param jobLauncher to launch jobs
100-
* @param jobExplorer to check the job repository for previous executions
97+
* @param jobOperator to launch jobs
10198
* @param jobRepository to check if a job instance exists with the given parameters
10299
* when running a job
103100
*/
104-
public JobLauncherApplicationRunner(JobLauncher jobLauncher, JobExplorer jobExplorer, JobRepository jobRepository) {
105-
Assert.notNull(jobLauncher, "'jobLauncher' must not be null");
106-
Assert.notNull(jobExplorer, "'jobExplorer' must not be null");
101+
public JobLauncherApplicationRunner(JobOperator jobOperator, JobRepository jobRepository) {
102+
Assert.notNull(jobOperator, "'jobOperator' must not be null");
107103
Assert.notNull(jobRepository, "'jobRepository' must not be null");
108-
this.jobLauncher = jobLauncher;
109-
this.jobExplorer = jobExplorer;
104+
this.jobOperator = jobOperator;
110105
this.jobRepository = jobRepository;
111106
}
112107

@@ -199,23 +194,24 @@ private void executeRegisteredJobs(JobParameters jobParameters) throws JobExecut
199194
}
200195
}
201196

202-
protected void execute(Job job, JobParameters jobParameters) throws JobExecutionAlreadyRunningException,
203-
JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException {
197+
protected void execute(Job job, JobParameters jobParameters)
198+
throws JobExecutionAlreadyRunningException, NoSuchJobException, JobRestartException,
199+
JobInstanceAlreadyCompleteException, JobParametersInvalidException {
204200
JobParameters parameters = getNextJobParameters(job, jobParameters);
205-
JobExecution execution = this.jobLauncher.run(job, parameters);
201+
JobExecution execution = this.jobOperator.start(job, parameters);
206202
if (this.publisher != null) {
207203
this.publisher.publishEvent(new JobExecutionEvent(execution));
208204
}
209205
}
210206

211207
private JobParameters getNextJobParameters(Job job, JobParameters jobParameters) {
212-
if (this.jobRepository != null && this.jobRepository.isJobInstanceExists(job.getName(), jobParameters)) {
208+
if (this.jobRepository != null && this.jobRepository.getJobInstance(job.getName(), jobParameters) != null) {
213209
return getNextJobParametersForExisting(job, jobParameters);
214210
}
215211
if (job.getJobParametersIncrementer() == null) {
216212
return jobParameters;
217213
}
218-
JobParameters nextParameters = new JobParametersBuilder(jobParameters, this.jobExplorer)
214+
JobParameters nextParameters = new JobParametersBuilder(jobParameters, this.jobRepository)
219215
.getNextJobParameters(job)
220216
.toJobParameters();
221217
return merge(nextParameters, jobParameters);

0 commit comments

Comments
 (0)