Skip to content
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

Add possibility to configure a custom ExecutionContextSerializer in BatchAutoConfiguration #38328

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,7 +24,9 @@
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.batch.core.repository.dao.DefaultExecutionContextSerializer;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.ExitCodeGenerator;
import org.springframework.boot.autoconfigure.AutoConfiguration;
Expand Down Expand Up @@ -62,6 +64,7 @@
* @author Eddú Meléndez
* @author Kazuki Shimizu
* @author Mahmoud Ben Hassine
* @author Lars Uffmann
* @since 1.0.0
*/
@AutoConfiguration(after = { HibernateJpaAutoConfiguration.class, TransactionAutoConfiguration.class })
Expand Down Expand Up @@ -102,13 +105,18 @@ static class SpringBootBatchConfiguration extends DefaultBatchConfiguration {

private final List<BatchConversionServiceCustomizer> batchConversionServiceCustomizers;

private final ExecutionContextSerializer executionContextSerializer;

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.executionContextSerializer = executionContextSerializer
.getIfAvailable(DefaultExecutionContextSerializer::new);
}

@Override
Expand Down Expand Up @@ -142,6 +150,11 @@ protected ConfigurableConversionService getConversionService() {
return conversionService;
}

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

}

@Configuration(proxyBeanMethods = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@
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.batch.core.repository.dao.DefaultExecutionContextSerializer;
import org.springframework.batch.core.repository.dao.Jackson2ExecutionContextStringSerializer;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
Expand Down Expand Up @@ -98,6 +101,7 @@
* @author Vedran Pavic
* @author Kazuki Shimizu
* @author Mahmoud Ben Hassine
* @author Lars Uffmann
*/
@ExtendWith(OutputCaptureExtension.class)
class BatchAutoConfigurationTests {
Expand Down Expand Up @@ -462,6 +466,27 @@ void whenTheUserDefinesAJobNameThatDoesNotExistWithRegisteredJobFailsFast() {
.withMessage("No job found with name 'three'");
}

@Test
void customExecutionContextSerializerIsUsed() {
this.contextRunner.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class)
.withUserConfiguration(CustomExecutionContextConfiguration.class)
.run((context) -> {
assertThat(context).hasSingleBean(Jackson2ExecutionContextStringSerializer.class);
assertThat(context.getBean(SpringBootBatchConfiguration.class).getExecutionContextSerializer())
.isInstanceOf(Jackson2ExecutionContextStringSerializer.class);
});
}

@Test
void defaultExecutionContextSerializerIsUsed() {
this.contextRunner.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class)
.run((context) -> {
assertThat(context).doesNotHaveBean(ExecutionContextSerializer.class);
assertThat(context.getBean(SpringBootBatchConfiguration.class).getExecutionContextSerializer())
.isInstanceOf(DefaultExecutionContextSerializer.class);
});
}

private JobLauncherApplicationRunner createInstance(String... registeredJobNames) {
JobLauncherApplicationRunner runner = new JobLauncherApplicationRunner(mock(JobLauncher.class),
mock(JobExplorer.class), mock(JobRepository.class));
Expand Down Expand Up @@ -777,4 +802,14 @@ BatchConversionServiceCustomizer anotherBatchConversionServiceCustomizer() {

}

@Configuration(proxyBeanMethods = false)
static class CustomExecutionContextConfiguration {

@Bean
ExecutionContextSerializer executionContextSerializer() {
return new Jackson2ExecutionContextStringSerializer();
}

}

}