Skip to content

Commit

Permalink
Deprecate Job/Step builder factories
Browse files Browse the repository at this point in the history
This commit deprecates JobBuilderFactory and StepBuilderFactory
in favor of the respective builders they create.

It also removes the exposure of such utilities as beans in the
application context when using `@EnableBatchProcessing`.

Resolves #4188
  • Loading branch information
fmbenhassine committed Sep 13, 2022
1 parent 06c2dc3 commit 6e443cb
Show file tree
Hide file tree
Showing 45 changed files with 387 additions and 484 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,39 +49,15 @@
*/
@Configuration(proxyBeanMethods = false)
@Import(ScopeConfiguration.class)
public abstract class AbstractBatchConfiguration implements InitializingBean {
public abstract class AbstractBatchConfiguration {

private static final Log logger = LogFactory.getLog(AbstractBatchConfiguration.class);

@Autowired
protected ApplicationContext context;

private JobBuilderFactory jobBuilderFactory;

private StepBuilderFactory stepBuilderFactory;

private JobRegistry jobRegistry = new MapJobRegistry();

/**
* Establish the {@link JobBuilderFactory} for the batch execution.
* @return The instance of the {@link JobBuilderFactory}.
* @throws Exception The {@link Exception} thrown if an error occurs.
*/
@Bean
public JobBuilderFactory jobBuilders() throws Exception {
return this.jobBuilderFactory;
}

/**
* Establish the {@link StepBuilderFactory} for the batch execution.
* @return The instance of the {@link StepBuilderFactory}.
* @throws Exception The {@link Exception} thrown if an error occurs.
*/
@Bean
public StepBuilderFactory stepBuilders() throws Exception {
return this.stepBuilderFactory;
}

/**
* Establish the {@link JobRepository} for the batch execution.
* @return The instance of the {@link JobRepository}.
Expand Down Expand Up @@ -123,13 +99,6 @@ public JobRegistry jobRegistry() throws Exception {
*/
public abstract PlatformTransactionManager transactionManager() throws Exception;

@Override
public void afterPropertiesSet() throws Exception {
BatchConfigurer batchConfigurer = getOrCreateConfigurer();
this.jobBuilderFactory = new JobBuilderFactory(batchConfigurer.getJobRepository());
this.stepBuilderFactory = new StepBuilderFactory(batchConfigurer.getJobRepository());
}

/**
* If a {@link BatchConfigurer} exists, return it. Otherwise, create a
* {@link DefaultBatchConfigurer}. If more than one configurer is present, an
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@
* public class AppConfig {
*
* @Autowired
* private JobBuilderFactory jobs;
* private JobRepository jobRepository;
*
* @Bean
* public Job job() {
* return jobs.get("myJob").start(step1()).next(step2()).build();
* public Job job(JobRepository jobRepository) {
* return new JobBuilder("myJob").repository(jobRepository).start(step1()).next(step2()).build();
* }
*
* @Bean
Expand Down Expand Up @@ -108,12 +108,6 @@
* <li>a {@link org.springframework.batch.core.explore.JobExplorer} (bean name
* "jobExplorer" of type
* {@link org.springframework.batch.core.explore.support.SimpleJobExplorer})</li>
* <li>a {@link JobBuilderFactory} (bean name "jobBuilders") as a convenience to prevent
* you from having to inject the job repository into every job, as in the earlier
* examples</li>
* <li>a {@link StepBuilderFactory} (bean name "stepBuilders") as a convenience to prevent
* you from having to inject the job repository and transaction manager into every
* step</li>
* </ul>
*
* The transaction manager provided by this annotation is of type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@
*
* @author Dave Syer
* @author Mahmoud Ben Hassine
* @deprecated Deprecated as of v5.0 and scheduled for removal in v5.2 in favor of using
* the {@link JobBuilder}.
*
*/
@Deprecated(since = "5.0.0", forRemoval = true)
public class JobBuilderFactory {

private JobRepository jobRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@
*
* @author Dave Syer
* @author Mahmoud Ben Hassine
* @deprecated Deprecated as of v5.0 and scheduled for removal in v5.2 in favor of using
* the {@link StepBuilder}.
*
*/
@Deprecated(since = "5.0.0", forRemoval = true)
public class StepBuilderFactory {

private JobRepository jobRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
Expand Down Expand Up @@ -53,21 +56,13 @@ void testInlineDataSourceDefinition() throws Exception {
@EnableBatchProcessing
static class MyJobConfiguration {

private JobBuilderFactory jobs;

private StepBuilderFactory steps;

public MyJobConfiguration(JobBuilderFactory jobs, StepBuilderFactory steps) {
this.jobs = jobs;
this.steps = steps;
}

@Bean
public Job job() {
return jobs.get("job").start(steps.get("step").tasklet((contribution, chunkContext) -> {
System.out.println("hello world");
return RepeatStatus.FINISHED;
}).build()).build();
public Job job(JobRepository jobRepository) {
return new JobBuilder("job").repository(jobRepository)
.start(new StepBuilder("step").repository(jobRepository).tasklet((contribution, chunkContext) -> {
System.out.println("hello world");
return RepeatStatus.FINISHED;
}).build()).build();
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.job.builder.SimpleJobBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.AbstractStep;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -110,28 +113,28 @@ private void testJob(String jobName, BatchStatus status, int stepExecutionCount,
public static class TestConfiguration {

@Autowired
private JobBuilderFactory jobs;

@Autowired
private StepBuilderFactory steps;
private JobRepository jobRepository;

@Autowired
private JdbcTransactionManager transactionManager;

@Bean
public Job testJob() throws Exception {
SimpleJobBuilder builder = jobs.get("test").start(step1()).next(step2());
SimpleJobBuilder builder = new JobBuilder("test").repository(this.jobRepository).start(step1())
.next(step2());
return builder.build();
}

@Bean
protected Step step1() throws Exception {
return steps.get("step1").tasklet(tasklet()).transactionManager(this.transactionManager).build();
return new StepBuilder("step1").repository(jobRepository).tasklet(tasklet())
.transactionManager(this.transactionManager).build();
}

@Bean
protected Step step2() throws Exception {
return steps.get("step2").tasklet(tasklet()).transactionManager(this.transactionManager).build();
return new StepBuilder("step2").repository(jobRepository).tasklet(tasklet())
.transactionManager(this.transactionManager).build();
}

@Bean
Expand All @@ -155,27 +158,22 @@ public RepeatStatus execute(StepContribution contribution, ChunkContext context)
@Import(DataSourceConfiguration.class)
public static class AnotherConfiguration {

@Autowired
private JobBuilderFactory jobs;

@Autowired
private StepBuilderFactory steps;

@Autowired
private JdbcTransactionManager transactionManager;

@Autowired
private Tasklet tasklet;

@Bean
public Job anotherJob() throws Exception {
SimpleJobBuilder builder = jobs.get("another").start(step3());
public Job anotherJob(JobRepository jobRepository) throws Exception {
SimpleJobBuilder builder = new JobBuilder("another").repository(jobRepository).start(step3(jobRepository));
return builder.build();
}

@Bean
protected Step step3() throws Exception {
return steps.get("step3").tasklet(tasklet).transactionManager(this.transactionManager).build();
protected Step step3(JobRepository jobRepository) throws Exception {
return new StepBuilder("step3").repository(jobRepository).tasklet(tasklet)
.transactionManager(this.transactionManager).build();
}

}
Expand All @@ -185,16 +183,13 @@ protected Step step3() throws Exception {
@Import(DataSourceConfiguration.class)
public static class TestConfigurer extends DefaultBatchConfigurer {

@Autowired
private SimpleBatchConfiguration jobs;

public TestConfigurer(DataSource dataSource) {
super(dataSource);
}

@Bean
public Job testConfigurerJob() throws Exception {
SimpleJobBuilder builder = jobs.jobBuilders().get("configurer").start(step1());
public Job testConfigurerJob(JobRepository jobRepository) throws Exception {
SimpleJobBuilder builder = new JobBuilder("configurer").repository(jobRepository).start(step1());
return builder.build();
}

Expand All @@ -218,24 +213,18 @@ protected void doExecute(StepExecution stepExecution) throws Exception {
@Import(DataSourceConfiguration.class)
public static class BeansConfigurer {

@Autowired
private JobBuilderFactory jobs;

@Autowired
private StepBuilderFactory steps;

@Autowired
private JdbcTransactionManager transactionManager;

@Bean
public Job beansConfigurerJob() throws Exception {
SimpleJobBuilder builder = jobs.get("beans").start(step1());
public Job beansConfigurerJob(JobRepository jobRepository) throws Exception {
SimpleJobBuilder builder = new JobBuilder("beans").repository(jobRepository).start(step1(jobRepository));
return builder.build();
}

@Bean
protected Step step1() throws Exception {
return steps.get("step1").tasklet(new Tasklet() {
protected Step step1(JobRepository jobRepository) throws Exception {
return new StepBuilder("step1").repository(jobRepository).tasklet(new Tasklet() {

@Nullable
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@
import org.springframework.batch.core.configuration.support.AutomaticJobRegistrar;
import org.springframework.batch.core.configuration.support.GenericApplicationContextFactory;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.job.builder.SimpleJobBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
Expand Down Expand Up @@ -122,28 +125,23 @@ public ApplicationObjectSupport fakeApplicationObjectSupport() {
};
}

@Autowired
private JobBuilderFactory jobs;

@Autowired
private StepBuilderFactory steps;

@Bean
public Job testJob() throws Exception {
SimpleJobBuilder builder = jobs.get("test").start(step1()).next(step2());
public Job testJob(JobRepository jobRepository) throws Exception {
SimpleJobBuilder builder = new JobBuilder("test").repository(jobRepository).start(step1(jobRepository))
.next(step2(jobRepository));
return builder.build();
}

@Bean
protected Step step1() throws Exception {
return steps.get("step1").tasklet(tasklet()).transactionManager(new ResourcelessTransactionManager())
.build();
protected Step step1(JobRepository jobRepository) throws Exception {
return new StepBuilder("step1").repository(jobRepository).tasklet(tasklet())
.transactionManager(new ResourcelessTransactionManager()).build();
}

@Bean
protected Step step2() throws Exception {
return steps.get("step2").tasklet(tasklet()).transactionManager(new ResourcelessTransactionManager())
.build();
protected Step step2(JobRepository jobRepository) throws Exception {
return new StepBuilder("step2").repository(jobRepository).tasklet(tasklet())
.transactionManager(new ResourcelessTransactionManager()).build();
}

@Bean
Expand All @@ -162,21 +160,15 @@ public RepeatStatus execute(StepContribution contribution, ChunkContext context)
@Configuration
public static class VanillaConfiguration {

@Autowired
private JobBuilderFactory jobs;

@Autowired
private StepBuilderFactory steps;

@Bean
public Job vanillaJob() throws Exception {
SimpleJobBuilder builder = jobs.get("vanilla").start(step3());
public Job vanillaJob(JobRepository jobRepository) throws Exception {
SimpleJobBuilder builder = new JobBuilder("vanilla").repository(jobRepository).start(step3(jobRepository));
return builder.build();
}

@Bean
protected Step step3() throws Exception {
return steps.get("step3").tasklet(new Tasklet() {
protected Step step3(JobRepository jobRepository) throws Exception {
return new StepBuilder("step3").repository(jobRepository).tasklet(new Tasklet() {
@Nullable
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext context) throws Exception {
Expand Down
Loading

3 comments on commit 6e443cb

@benelog
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am leaving a small comment just in case it will be changed in the future.

It will be more convenient if the 'JobRepository' is added to constructors of JobBuilder and StepBuilder.

		public Job job(JobRepository jobRepository) {
			return new JobBuilder("job", jobRepository)
					.start(new StepBuilder("step", jobRepository)
							.tasklet((contribution, chunkContext) -> RepeatStatus.FINISHED)
							.transactionManager(transactionManager(dataSource())).build())
					.build();
		}

@fmbenhassine
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the suggestion. That was done here: #4192.

@benelog
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found that the commit below was already done before my comment.
Thank you.

f39f070

Please sign in to comment.