Skip to content

Commit

Permalink
Merge pull request #38328 from cachescrubber
Browse files Browse the repository at this point in the history
* pr/38328-2:
  Add possibility to configure a custom ExecutionContextSerializer in BatchAutoConfiguration

Closes gh-38328
  • Loading branch information
mhalbritter committed Jan 10, 2024
2 parents f3ec5da + e5b2ad9 commit 18f1221
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
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();
}

}

}

0 comments on commit 18f1221

Please sign in to comment.