Skip to content

Commit 4c86f33

Browse files
committed
Add support to configure JobKeyGenerator in MongoJobRepository
1 parent 58ef32e commit 4c86f33

File tree

4 files changed

+30
-38
lines changed

4 files changed

+30
-38
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/repository/support/AbstractJobRepositoryFactoryBean.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import org.springframework.aop.framework.ProxyFactory;
2323
import org.springframework.aop.support.DefaultPointcutAdvisor;
2424
import org.springframework.aop.support.NameMatchMethodPointcut;
25+
import org.springframework.batch.core.DefaultJobKeyGenerator;
26+
import org.springframework.batch.core.JobKeyGenerator;
2527
import org.springframework.batch.core.repository.JobRepository;
2628
import org.springframework.batch.core.repository.dao.ExecutionContextDao;
2729
import org.springframework.batch.core.repository.dao.JobExecutionDao;
@@ -71,6 +73,8 @@ public abstract class AbstractJobRepositoryFactoryBean implements FactoryBean<Jo
7173
*/
7274
private static final String DEFAULT_ISOLATION_LEVEL = TRANSACTION_ISOLATION_LEVEL_PREFIX + "SERIALIZABLE";
7375

76+
protected JobKeyGenerator jobKeyGenerator;
77+
7478
/**
7579
* @return fully configured {@link JobInstanceDao} implementation.
7680
* @throws Exception thrown if error occurs creating JobInstanceDao.
@@ -176,9 +180,22 @@ public void setTransactionAttributeSource(TransactionAttributeSource transaction
176180
this.transactionAttributeSource = transactionAttributeSource;
177181
}
178182

183+
/**
184+
* * Sets the generator for creating the key used in identifying unique {link
185+
* JobInstance} objects
186+
* @param jobKeyGenerator a {@link JobKeyGenerator}
187+
* @since 5.1
188+
*/
189+
public void setJobKeyGenerator(JobKeyGenerator jobKeyGenerator) {
190+
this.jobKeyGenerator = jobKeyGenerator;
191+
}
192+
179193
@Override
180194
public void afterPropertiesSet() throws Exception {
181195
Assert.state(transactionManager != null, "TransactionManager must not be null.");
196+
if (jobKeyGenerator == null) {
197+
jobKeyGenerator = new DefaultJobKeyGenerator();
198+
}
182199
if (this.transactionAttributeSource == null) {
183200
Properties transactionAttributes = new Properties();
184201
transactionAttributes.setProperty("create*",

spring-batch-core/src/main/java/org/springframework/batch/core/repository/support/JdbcJobRepositoryFactoryBean.java

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package org.springframework.batch.core.repository.support;
1717

18-
import org.springframework.batch.core.JobKeyGenerator;
1918
import org.springframework.batch.core.repository.ExecutionContextSerializer;
2019
import org.springframework.batch.core.repository.dao.AbstractJdbcBatchMetadataDao;
2120
import org.springframework.batch.core.repository.dao.DefaultExecutionContextSerializer;
@@ -147,26 +146,16 @@ public void setIncrementerFactory(DataFieldMaxValueIncrementerFactory incremente
147146
super.setIncrementerFactory(incrementerFactory);
148147
}
149148

150-
/**
151-
* * Sets the generator for creating the key used in identifying unique {link
152-
* JobInstance} objects
153-
* @param jobKeyGenerator a {@link JobKeyGenerator}
154-
* @since 5.1
155-
*/
156-
public void setJobKeyGenerator(JobKeyGenerator jobKeyGenerator) {
157-
super.setJobKeyGenerator(jobKeyGenerator);
158-
}
159-
160-
/**
161-
* Set the {@link Charset} to use when serializing/deserializing the execution
162-
* context. Defaults to "UTF-8". Must not be {@code null}.
163-
* @param charset to use when serializing/deserializing the execution context.
164-
* @see JdbcExecutionContextDao#setCharset(Charset)
165-
* @since 5.0
166-
*/
167-
public void setCharset(@NonNull Charset charset) {
168-
super.setCharset(charset);
169-
}
149+
/**
150+
* Set the {@link Charset} to use when serializing/deserializing the execution
151+
* context. Defaults to "UTF-8". Must not be {@code null}.
152+
* @param charset to use when serializing/deserializing the execution context.
153+
* @see JdbcExecutionContextDao#setCharset(Charset)
154+
* @since 5.0
155+
*/
156+
public void setCharset(@NonNull Charset charset) {
157+
super.setCharset(charset);
158+
}
170159

171160
/**
172161
* Set the conversion service to use in the job repository. This service is used to

spring-batch-core/src/main/java/org/springframework/batch/core/repository/support/JobRepositoryFactoryBean.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,6 @@ public class JobRepositoryFactoryBean extends AbstractJobRepositoryFactoryBean i
9090

9191
protected DataFieldMaxValueIncrementerFactory incrementerFactory;
9292

93-
protected JobKeyGenerator jobKeyGenerator;
94-
9593
protected int maxVarCharLengthForExitMessage = AbstractJdbcBatchMetadataDao.DEFAULT_EXIT_MESSAGE_LENGTH;
9694

9795
protected int maxVarCharLengthForShortContext = AbstractJdbcBatchMetadataDao.DEFAULT_SHORT_CONTEXT_LENGTH;
@@ -205,16 +203,6 @@ public void setIncrementerFactory(DataFieldMaxValueIncrementerFactory incremente
205203
this.incrementerFactory = incrementerFactory;
206204
}
207205

208-
/**
209-
* * Sets the generator for creating the key used in identifying unique {link
210-
* JobInstance} objects
211-
* @param jobKeyGenerator a {@link JobKeyGenerator}
212-
* @since 5.1
213-
*/
214-
public void setJobKeyGenerator(JobKeyGenerator jobKeyGenerator) {
215-
this.jobKeyGenerator = jobKeyGenerator;
216-
}
217-
218206
/**
219207
* Set the {@link Charset} to use when serializing/deserializing the execution
220208
* context. Defaults to "UTF-8". Must not be {@code null}.
@@ -251,10 +239,6 @@ public void afterPropertiesSet() throws Exception {
251239
incrementerFactory = new DefaultDataFieldMaxValueIncrementerFactory(dataSource);
252240
}
253241

254-
if (jobKeyGenerator == null) {
255-
jobKeyGenerator = new DefaultJobKeyGenerator();
256-
}
257-
258242
if (databaseType == null) {
259243
databaseType = DatabaseType.fromMetaData(dataSource).name();
260244
if (logger.isInfoEnabled()) {

spring-batch-core/src/main/java/org/springframework/batch/core/repository/support/MongoJobRepositoryFactoryBean.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ public void setMongoOperations(MongoOperations mongoOperations) {
4949

5050
@Override
5151
protected JobInstanceDao createJobInstanceDao() {
52-
return new MongoJobInstanceDao(this.mongoOperations);
52+
MongoJobInstanceDao mongoJobInstanceDao = new MongoJobInstanceDao(this.mongoOperations);
53+
mongoJobInstanceDao.setJobKeyGenerator(this.jobKeyGenerator);
54+
return mongoJobInstanceDao;
5355
}
5456

5557
@Override

0 commit comments

Comments
 (0)