Skip to content

Support user-defined ExecutionContextSerializer in BatchAutoConfiguation #38631

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.springframework.batch.core.configuration.support.DefaultBatchConfiguration;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.ExecutionContextSerializer;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.ExitCodeGenerator;
Expand Down Expand Up @@ -62,6 +63,7 @@
* @author Eddú Meléndez
* @author Kazuki Shimizu
* @author Mahmoud Ben Hassine
* @author Sanghyuk Jung
* @since 1.0.0
*/
@AutoConfiguration(after = { HibernateJpaAutoConfiguration.class, TransactionAutoConfiguration.class })
Expand Down Expand Up @@ -102,13 +104,17 @@ static class SpringBootBatchConfiguration extends DefaultBatchConfiguration {

private final List<BatchConversionServiceCustomizer> batchConversionServiceCustomizers;

private final ExecutionContextSerializer serializer;

SpringBootBatchConfiguration(DataSource dataSource, @BatchDataSource ObjectProvider<DataSource> batchDataSource,
PlatformTransactionManager transactionManager, BatchProperties properties,
ObjectProvider<BatchConversionServiceCustomizer> batchConversionServiceCustomizers) {
ObjectProvider<BatchConversionServiceCustomizer> batchConversionServiceCustomizers,
ObjectProvider<ExecutionContextSerializer> executionContextSerializer) {
this.dataSource = batchDataSource.getIfAvailable(() -> dataSource);
this.transactionManager = transactionManager;
this.properties = properties;
this.batchConversionServiceCustomizers = batchConversionServiceCustomizers.orderedStream().toList();
this.serializer = executionContextSerializer.getIfAvailable(super::getExecutionContextSerializer);
}

@Override
Expand All @@ -133,6 +139,11 @@ protected Isolation getIsolationLevelForCreate() {
return (isolation != null) ? isolation : super.getIsolationLevelForCreate();
}

@Override
protected ExecutionContextSerializer getExecutionContextSerializer() {
return this.serializer;
}

@Override
protected ConfigurableConversionService getConversionService() {
ConfigurableConversionService conversionService = super.getConversionService();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.springframework.batch.core.job.AbstractJob;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.launch.JobOperator;
import org.springframework.batch.core.repository.ExecutionContextSerializer;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -430,6 +431,17 @@ void conversionServiceCustomizersAreCalled() {
});
}

@Test
void whenTheUserDefinesCustomExecutionContextSerializer() {
this.contextRunner.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class)
.withUserConfiguration(ExecutionContextSerializerConfiguration.class)
.run((context) -> {
SpringBootBatchConfiguration batchConfiguration = context.getBean(SpringBootBatchConfiguration.class);
ExecutionContextSerializer serializer = context.getBean(ExecutionContextSerializer.class);
assertThat(batchConfiguration.getExecutionContextSerializer()).isSameAs(serializer);
});
}

@Test
void whenTheUserDefinesAJobNameAsJobInstanceValidates() {
JobLauncherApplicationRunner runner = createInstance("another");
Expand Down Expand Up @@ -777,4 +789,14 @@ BatchConversionServiceCustomizer anotherBatchConversionServiceCustomizer() {

}

@Configuration(proxyBeanMethods = false)
static class ExecutionContextSerializerConfiguration {

@Bean
ExecutionContextSerializer executionContextSerializer() {
return mock(ExecutionContextSerializer.class);
}

}

}