Skip to content

Commit 98f72e7

Browse files
committed
WIP: Use ObjectProviders to customize default batch configuration
Issue #4962
1 parent 86ddfb9 commit 98f72e7

File tree

3 files changed

+148
-184
lines changed

3 files changed

+148
-184
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/DefaultBatchConfiguration.java

Lines changed: 39 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616
package org.springframework.batch.core.configuration.support;
1717

1818
import org.springframework.batch.core.configuration.DuplicateJobException;
19-
import org.springframework.batch.core.job.DefaultJobKeyGenerator;
2019
import org.springframework.batch.core.job.Job;
21-
import org.springframework.batch.core.job.JobInstance;
22-
import org.springframework.batch.core.job.JobKeyGenerator;
2320
import org.springframework.batch.core.configuration.BatchConfigurationException;
2421
import org.springframework.batch.core.configuration.JobRegistry;
2522
import org.springframework.batch.core.converter.DefaultJobParametersConverter;
@@ -31,6 +28,8 @@
3128
import org.springframework.batch.core.repository.support.ResourcelessJobRepository;
3229
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
3330
import org.springframework.beans.BeansException;
31+
import org.springframework.beans.factory.ObjectProvider;
32+
import org.springframework.beans.factory.annotation.Autowired;
3433
import org.springframework.context.ApplicationContext;
3534
import org.springframework.context.ApplicationContextAware;
3635
import org.springframework.context.annotation.Bean;
@@ -39,7 +38,6 @@
3938
import org.springframework.core.task.SyncTaskExecutor;
4039
import org.springframework.core.task.TaskExecutor;
4140
import org.springframework.transaction.PlatformTransactionManager;
42-
import org.springframework.transaction.annotation.Isolation;
4341

4442
/**
4543
* Base {@link Configuration} class that provides common infrastructure beans for enabling
@@ -55,11 +53,11 @@
5553
* <li>a {@link org.springframework.batch.core.scope.JobScope} named "jobScope"</li>
5654
* </ul>
5755
*
58-
* Customization is possible by extending the class and overriding getters.
5956
* <p>
6057
* A typical usage of this class is as follows: <pre class="code">
6158
* &#064;Configuration
62-
* public class MyJobConfiguration extends DefaultBatchConfiguration {
59+
* &#064;Import(DefaultBatchConfiguration.class)
60+
* public class MyJobConfiguration {
6361
*
6462
* &#064;Bean
6563
* public Job job(JobRepository jobRepository) {
@@ -71,6 +69,10 @@
7169
* }
7270
* </pre>
7371
*
72+
* Customization is possible by defining configurable artefacts (transaction manager,
73+
* task executor, etc) as beans in the application context.
74+
*
75+
*
7476
* @author Dave Syer
7577
* @author Michael Minella
7678
* @author Mahmoud Ben Hassine
@@ -83,6 +85,18 @@ public class DefaultBatchConfiguration implements ApplicationContextAware {
8385

8486
protected ApplicationContext applicationContext;
8587

88+
@Autowired
89+
protected ObjectProvider<PlatformTransactionManager> transactionManagerObjectProvider;
90+
91+
@Autowired
92+
protected ObjectProvider<JobParametersConverter> jobParametersConverterObjectProvider;
93+
94+
@Autowired
95+
protected ObjectProvider<TaskExecutor> taskExecutorObjectProvider;
96+
97+
@Autowired
98+
protected ObjectProvider<JobRegistry> jobRegistryObjectProvider;
99+
86100
@Override
87101
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
88102
this.applicationContext = applicationContext;
@@ -97,10 +111,13 @@ public JobRepository jobRepository() {
97111
public JobOperator jobOperator(JobRepository jobRepository) throws BatchConfigurationException {
98112
JobOperatorFactoryBean jobOperatorFactoryBean = new JobOperatorFactoryBean();
99113
jobOperatorFactoryBean.setJobRepository(jobRepository);
100-
jobOperatorFactoryBean.setJobRegistry(getJobRegistry());
101-
jobOperatorFactoryBean.setTransactionManager(getTransactionManager());
102-
jobOperatorFactoryBean.setJobParametersConverter(getJobParametersConverter());
103-
jobOperatorFactoryBean.setTaskExecutor(getTaskExecutor());
114+
jobOperatorFactoryBean.setJobRegistry(jobRegistryObjectProvider.getIfAvailable(() -> getDefaultJobRegistry()));
115+
jobOperatorFactoryBean.setTransactionManager(
116+
transactionManagerObjectProvider.getIfAvailable(() -> getDefaultTransactionManager()));
117+
jobOperatorFactoryBean.setJobParametersConverter(
118+
jobParametersConverterObjectProvider.getIfAvailable(() -> getDefaultJobParametersConverter()));
119+
jobOperatorFactoryBean.setTaskExecutor(taskExecutorObjectProvider.getIfAvailable(() -> getDefaultTaskExecutor()));
120+
// TODO configure meter registry and transaction attribute source
104121
try {
105122
jobOperatorFactoryBean.afterPropertiesSet();
106123
return jobOperatorFactoryBean.getObject();
@@ -110,7 +127,12 @@ public JobOperator jobOperator(JobRepository jobRepository) throws BatchConfigur
110127
}
111128
}
112129

113-
protected JobRegistry getJobRegistry() {
130+
/**
131+
* Return the default {@link JobRegistry} to use for the job operator. By default, it
132+
* is populated with jobs from the application context.
133+
* @return The job registry to use for the job operator
134+
*/
135+
private JobRegistry getDefaultJobRegistry() {
114136
MapJobRegistry jobRegistry = new MapJobRegistry();
115137
this.applicationContext.getBeansOfType(Job.class).values().forEach(job -> {
116138
try {
@@ -124,62 +146,27 @@ protected JobRegistry getJobRegistry() {
124146
}
125147

126148
/**
127-
* Return the transaction manager to use for the job operator. Defaults to
128-
* {@link ResourcelessTransactionManager}.
149+
* Return the default {@link PlatformTransactionManager} to use for the job operator.
129150
* @return The transaction manager to use for the job operator
130151
*/
131-
protected PlatformTransactionManager getTransactionManager() {
152+
private PlatformTransactionManager getDefaultTransactionManager() {
132153
return new ResourcelessTransactionManager();
133154
}
134155

135156
/**
136-
* Return the {@link TaskExecutor} to use in the job operator. Defaults to
137-
* {@link SyncTaskExecutor}.
157+
* Return the default {@link TaskExecutor} to use in the job operator.
138158
* @return the {@link TaskExecutor} to use in the job operator.
139159
*/
140-
protected TaskExecutor getTaskExecutor() {
160+
private TaskExecutor getDefaultTaskExecutor() {
141161
return new SyncTaskExecutor();
142162
}
143163

144164
/**
145-
* Return the {@link JobParametersConverter} to use in the job operator. Defaults to
146-
* {@link DefaultJobParametersConverter}
165+
* Return the default {@link JobParametersConverter} to use in the job operator.
147166
* @return the {@link JobParametersConverter} to use in the job operator.
148-
* @deprecated since 6.0 with no replacement and scheduled for removal in 6.2 or
149-
* later.
150167
*/
151-
@Deprecated(since = "6.0", forRemoval = true)
152-
protected JobParametersConverter getJobParametersConverter() {
168+
private JobParametersConverter getDefaultJobParametersConverter() {
153169
return new DefaultJobParametersConverter();
154170
}
155171

156-
/**
157-
* Return the value of the {@code validateTransactionState} parameter. Defaults to
158-
* {@code true}.
159-
* @return true if the transaction state should be validated, false otherwise
160-
*/
161-
protected boolean getValidateTransactionState() {
162-
return true;
163-
}
164-
165-
/**
166-
* Return the transaction isolation level when creating job executions. Defaults to
167-
* {@link Isolation#SERIALIZABLE}.
168-
* @return the transaction isolation level when creating job executions
169-
*/
170-
protected Isolation getIsolationLevelForCreate() {
171-
return Isolation.SERIALIZABLE;
172-
}
173-
174-
/**
175-
* A custom implementation of the {@link JobKeyGenerator}. The default, if not
176-
* injected, is the {@link DefaultJobKeyGenerator}.
177-
* @return the generator that creates the key used in identifying {@link JobInstance}
178-
* objects
179-
* @since 5.1
180-
*/
181-
protected JobKeyGenerator getJobKeyGenerator() {
182-
return new DefaultJobKeyGenerator();
183-
}
184-
185172
}

0 commit comments

Comments
 (0)