Skip to content

Commit 8dec85d

Browse files
committed
Autoconfigure more properties for Spring Batch
1. validateTransactionState 2. maxVarCharLength 3. jobParametersConverter Signed-off-by: Yanming Zhou <zhouyanming@gmail.com>
1 parent 4147943 commit 8dec85d

File tree

3 files changed

+93
-1
lines changed

3 files changed

+93
-1
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
2424
import org.springframework.batch.core.configuration.support.DefaultBatchConfiguration;
25+
import org.springframework.batch.core.converter.JobParametersConverter;
2526
import org.springframework.batch.core.explore.JobExplorer;
2627
import org.springframework.batch.core.launch.JobLauncher;
2728
import org.springframework.batch.core.repository.ExecutionContextSerializer;
@@ -66,6 +67,7 @@
6667
* @author Mahmoud Ben Hassine
6768
* @author Lars Uffmann
6869
* @author Lasse Wulff
70+
* @author Yanming Zhou
6971
* @since 1.0.0
7072
*/
7173
@AutoConfiguration(after = { HibernateJpaAutoConfiguration.class, TransactionAutoConfiguration.class })
@@ -110,18 +112,22 @@ static class SpringBootBatchConfiguration extends DefaultBatchConfiguration {
110112

111113
private final ExecutionContextSerializer executionContextSerializer;
112114

115+
private final JobParametersConverter jobParametersConverter;
116+
113117
SpringBootBatchConfiguration(DataSource dataSource, @BatchDataSource ObjectProvider<DataSource> batchDataSource,
114118
PlatformTransactionManager transactionManager,
115119
@BatchTransactionManager ObjectProvider<PlatformTransactionManager> batchTransactionManager,
116120
@BatchTaskExecutor ObjectProvider<TaskExecutor> batchTaskExecutor, BatchProperties properties,
117121
ObjectProvider<BatchConversionServiceCustomizer> batchConversionServiceCustomizers,
118-
ObjectProvider<ExecutionContextSerializer> executionContextSerializer) {
122+
ObjectProvider<ExecutionContextSerializer> executionContextSerializer,
123+
ObjectProvider<JobParametersConverter> jobParametersConverter) {
119124
this.dataSource = batchDataSource.getIfAvailable(() -> dataSource);
120125
this.transactionManager = batchTransactionManager.getIfAvailable(() -> transactionManager);
121126
this.taskExector = batchTaskExecutor.getIfAvailable();
122127
this.properties = properties;
123128
this.batchConversionServiceCustomizers = batchConversionServiceCustomizers.orderedStream().toList();
124129
this.executionContextSerializer = executionContextSerializer.getIfAvailable();
130+
this.jobParametersConverter = jobParametersConverter.getIfAvailable();
125131
}
126132

127133
@Override
@@ -134,12 +140,24 @@ protected PlatformTransactionManager getTransactionManager() {
134140
return this.transactionManager;
135141
}
136142

143+
@Override
144+
protected int getMaxVarCharLength() {
145+
Integer maxVarCharLength = this.properties.getJdbc().getMaxVarCharLength();
146+
return (maxVarCharLength != null) ? maxVarCharLength : super.getMaxVarCharLength();
147+
}
148+
137149
@Override
138150
protected String getTablePrefix() {
139151
String tablePrefix = this.properties.getJdbc().getTablePrefix();
140152
return (tablePrefix != null) ? tablePrefix : super.getTablePrefix();
141153
}
142154

155+
@Override
156+
protected boolean getValidateTransactionState() {
157+
Boolean validateTransactionState = this.properties.getJdbc().getValidateTransactionState();
158+
return (validateTransactionState != null) ? validateTransactionState : super.getValidateTransactionState();
159+
}
160+
143161
@Override
144162
protected Isolation getIsolationLevelForCreate() {
145163
Isolation isolation = this.properties.getJdbc().getIsolationLevelForCreate();
@@ -161,6 +179,12 @@ protected ExecutionContextSerializer getExecutionContextSerializer() {
161179
: super.getExecutionContextSerializer();
162180
}
163181

182+
@Override
183+
protected JobParametersConverter getJobParametersConverter() {
184+
return (this.jobParametersConverter != null) ? this.jobParametersConverter
185+
: super.getJobParametersConverter();
186+
}
187+
164188
@Override
165189
protected TaskExecutor getTaskExecutor() {
166190
return (this.taskExector != null) ? this.taskExector : super.getTaskExecutor();

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchProperties.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
* @author Eddú Meléndez
2828
* @author Vedran Pavic
2929
* @author Mukul Kumar Chaundhyan
30+
* @author Yanming Zhou
3031
* @since 1.2.0
3132
*/
3233
@ConfigurationProperties("spring.batch")
@@ -67,6 +68,11 @@ public static class Jdbc {
6768
private static final String DEFAULT_SCHEMA_LOCATION = "classpath:org/springframework/"
6869
+ "batch/core/schema-@@platform@@.sql";
6970

71+
/**
72+
* Whether to validate the transaction state.
73+
*/
74+
private Boolean validateTransactionState;
75+
7076
/**
7177
* Transaction isolation level to use when creating job meta-data for new jobs.
7278
*/
@@ -83,6 +89,12 @@ public static class Jdbc {
8389
*/
8490
private String platform;
8591

92+
/**
93+
* The length of long string columns in database. Do not override this if you
94+
* haven't modified the schema.
95+
*/
96+
private Integer maxVarCharLength;
97+
8698
/**
8799
* Table prefix for all the batch meta-data tables.
88100
*/
@@ -93,6 +105,14 @@ public static class Jdbc {
93105
*/
94106
private DatabaseInitializationMode initializeSchema = DatabaseInitializationMode.EMBEDDED;
95107

108+
public Boolean getValidateTransactionState() {
109+
return this.validateTransactionState;
110+
}
111+
112+
public void setValidateTransactionState(Boolean validateTransactionState) {
113+
this.validateTransactionState = validateTransactionState;
114+
}
115+
96116
public Isolation getIsolationLevelForCreate() {
97117
return this.isolationLevelForCreate;
98118
}
@@ -117,6 +137,14 @@ public void setPlatform(String platform) {
117137
this.platform = platform;
118138
}
119139

140+
public Integer getMaxVarCharLength() {
141+
return this.maxVarCharLength;
142+
}
143+
144+
public void setMaxVarCharLength(Integer maxVarCharLength) {
145+
this.maxVarCharLength = maxVarCharLength;
146+
}
147+
120148
public String getTablePrefix() {
121149
return this.tablePrefix;
122150
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
import org.springframework.batch.core.configuration.JobRegistry;
4040
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
4141
import org.springframework.batch.core.configuration.support.DefaultBatchConfiguration;
42+
import org.springframework.batch.core.converter.DefaultJobParametersConverter;
43+
import org.springframework.batch.core.converter.JobParametersConverter;
44+
import org.springframework.batch.core.converter.JsonJobParametersConverter;
4245
import org.springframework.batch.core.explore.JobExplorer;
4346
import org.springframework.batch.core.job.AbstractJob;
4447
import org.springframework.batch.core.launch.JobLauncher;
@@ -90,6 +93,7 @@
9093
import org.springframework.jdbc.datasource.init.DatabasePopulator;
9194
import org.springframework.orm.jpa.JpaTransactionManager;
9295
import org.springframework.transaction.PlatformTransactionManager;
96+
import org.springframework.transaction.annotation.Isolation;
9397

9498
import static org.assertj.core.api.Assertions.assertThat;
9599
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@@ -107,6 +111,7 @@
107111
* @author Mahmoud Ben Hassine
108112
* @author Lars Uffmann
109113
* @author Lasse Wulff
114+
* @author Yanming Zhou
110115
*/
111116
@ExtendWith(OutputCaptureExtension.class)
112117
class BatchAutoConfigurationTests {
@@ -520,6 +525,41 @@ void defaultExecutionContextSerializerIsUsed() {
520525
});
521526
}
522527

528+
@Test
529+
void customJdbcPropertiesIsUsed() {
530+
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)
531+
.withPropertyValues("spring.batch.jdbc.validate-transaction-state:false",
532+
"spring.batch.jdbc.isolation-level-for-create:READ_COMMITTED",
533+
"spring.batch.jdbc.max-var-char-length:4000")
534+
.run((context) -> {
535+
SpringBootBatchConfiguration configuration = context.getBean(SpringBootBatchConfiguration.class);
536+
assertThat(configuration.getValidateTransactionState()).isEqualTo(false);
537+
assertThat(configuration.getIsolationLevelForCreate()).isEqualTo(Isolation.READ_COMMITTED);
538+
assertThat(configuration.getMaxVarCharLength()).isEqualTo(4000);
539+
});
540+
}
541+
542+
@Test
543+
void customJobParametersConverterIsUsed() {
544+
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)
545+
.withBean(JobParametersConverter.class, JsonJobParametersConverter::new)
546+
.withPropertyValues("spring.datasource.generate-unique-name=true")
547+
.run((context) -> {
548+
assertThat(context).hasSingleBean(JsonJobParametersConverter.class);
549+
assertThat(context.getBean(SpringBootBatchConfiguration.class).getJobParametersConverter())
550+
.isInstanceOf(JsonJobParametersConverter.class);
551+
});
552+
}
553+
554+
@Test
555+
void defaultJobParametersConverterIsUsed() {
556+
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class).run((context) -> {
557+
assertThat(context).doesNotHaveBean(JobParametersConverter.class);
558+
assertThat(context.getBean(SpringBootBatchConfiguration.class).getJobParametersConverter())
559+
.isInstanceOf(DefaultJobParametersConverter.class);
560+
});
561+
}
562+
523563
private JobLauncherApplicationRunner createInstance(String... registeredJobNames) {
524564
JobLauncherApplicationRunner runner = new JobLauncherApplicationRunner(mock(JobLauncher.class),
525565
mock(JobExplorer.class), mock(JobRepository.class));

0 commit comments

Comments
 (0)