Add support to configure JobKeyGenerator in MongoJobRepository

This commit is contained in:
Mahmoud Ben Hassine
2025-05-22 09:55:26 +02:00
parent 58ef32e7a0
commit 4c86f33da9
4 changed files with 30 additions and 38 deletions

View File

@@ -22,6 +22,8 @@ import org.aopalliance.intercept.MethodInterceptor;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.NameMatchMethodPointcut;
import org.springframework.batch.core.DefaultJobKeyGenerator;
import org.springframework.batch.core.JobKeyGenerator;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.dao.ExecutionContextDao;
import org.springframework.batch.core.repository.dao.JobExecutionDao;
@@ -71,6 +73,8 @@ public abstract class AbstractJobRepositoryFactoryBean implements FactoryBean<Jo
*/
private static final String DEFAULT_ISOLATION_LEVEL = TRANSACTION_ISOLATION_LEVEL_PREFIX + "SERIALIZABLE";
protected JobKeyGenerator jobKeyGenerator;
/**
* @return fully configured {@link JobInstanceDao} implementation.
* @throws Exception thrown if error occurs creating JobInstanceDao.
@@ -176,9 +180,22 @@ public abstract class AbstractJobRepositoryFactoryBean implements FactoryBean<Jo
this.transactionAttributeSource = transactionAttributeSource;
}
/**
* * Sets the generator for creating the key used in identifying unique {link
* JobInstance} objects
* @param jobKeyGenerator a {@link JobKeyGenerator}
* @since 5.1
*/
public void setJobKeyGenerator(JobKeyGenerator jobKeyGenerator) {
this.jobKeyGenerator = jobKeyGenerator;
}
@Override
public void afterPropertiesSet() throws Exception {
Assert.state(transactionManager != null, "TransactionManager must not be null.");
if (jobKeyGenerator == null) {
jobKeyGenerator = new DefaultJobKeyGenerator();
}
if (this.transactionAttributeSource == null) {
Properties transactionAttributes = new Properties();
transactionAttributes.setProperty("create*",

View File

@@ -15,7 +15,6 @@
*/
package org.springframework.batch.core.repository.support;
import org.springframework.batch.core.JobKeyGenerator;
import org.springframework.batch.core.repository.ExecutionContextSerializer;
import org.springframework.batch.core.repository.dao.AbstractJdbcBatchMetadataDao;
import org.springframework.batch.core.repository.dao.DefaultExecutionContextSerializer;
@@ -147,26 +146,16 @@ public class JdbcJobRepositoryFactoryBean extends JobRepositoryFactoryBean {
super.setIncrementerFactory(incrementerFactory);
}
/**
* * Sets the generator for creating the key used in identifying unique {link
* JobInstance} objects
* @param jobKeyGenerator a {@link JobKeyGenerator}
* @since 5.1
*/
public void setJobKeyGenerator(JobKeyGenerator jobKeyGenerator) {
super.setJobKeyGenerator(jobKeyGenerator);
}
/**
* Set the {@link Charset} to use when serializing/deserializing the execution
* context. Defaults to "UTF-8". Must not be {@code null}.
* @param charset to use when serializing/deserializing the execution context.
* @see JdbcExecutionContextDao#setCharset(Charset)
* @since 5.0
*/
public void setCharset(@NonNull Charset charset) {
super.setCharset(charset);
}
/**
* Set the {@link Charset} to use when serializing/deserializing the execution
* context. Defaults to "UTF-8". Must not be {@code null}.
* @param charset to use when serializing/deserializing the execution context.
* @see JdbcExecutionContextDao#setCharset(Charset)
* @since 5.0
*/
public void setCharset(@NonNull Charset charset) {
super.setCharset(charset);
}
/**
* Set the conversion service to use in the job repository. This service is used to

View File

@@ -90,8 +90,6 @@ public class JobRepositoryFactoryBean extends AbstractJobRepositoryFactoryBean i
protected DataFieldMaxValueIncrementerFactory incrementerFactory;
protected JobKeyGenerator jobKeyGenerator;
protected int maxVarCharLengthForExitMessage = AbstractJdbcBatchMetadataDao.DEFAULT_EXIT_MESSAGE_LENGTH;
protected int maxVarCharLengthForShortContext = AbstractJdbcBatchMetadataDao.DEFAULT_SHORT_CONTEXT_LENGTH;
@@ -205,16 +203,6 @@ public class JobRepositoryFactoryBean extends AbstractJobRepositoryFactoryBean i
this.incrementerFactory = incrementerFactory;
}
/**
* * Sets the generator for creating the key used in identifying unique {link
* JobInstance} objects
* @param jobKeyGenerator a {@link JobKeyGenerator}
* @since 5.1
*/
public void setJobKeyGenerator(JobKeyGenerator jobKeyGenerator) {
this.jobKeyGenerator = jobKeyGenerator;
}
/**
* Set the {@link Charset} to use when serializing/deserializing the execution
* context. Defaults to "UTF-8". Must not be {@code null}.
@@ -251,10 +239,6 @@ public class JobRepositoryFactoryBean extends AbstractJobRepositoryFactoryBean i
incrementerFactory = new DefaultDataFieldMaxValueIncrementerFactory(dataSource);
}
if (jobKeyGenerator == null) {
jobKeyGenerator = new DefaultJobKeyGenerator();
}
if (databaseType == null) {
databaseType = DatabaseType.fromMetaData(dataSource).name();
if (logger.isInfoEnabled()) {

View File

@@ -49,7 +49,9 @@ public class MongoJobRepositoryFactoryBean extends AbstractJobRepositoryFactoryB
@Override
protected JobInstanceDao createJobInstanceDao() {
return new MongoJobInstanceDao(this.mongoOperations);
MongoJobInstanceDao mongoJobInstanceDao = new MongoJobInstanceDao(this.mongoOperations);
mongoJobInstanceDao.setJobKeyGenerator(this.jobKeyGenerator);
return mongoJobInstanceDao;
}
@Override