Group jdbc-related batch properties beneath spring.batch.jdbc
See gh-25316
This commit is contained in:
parent
9a3889baec
commit
d093807f95
|
@ -38,6 +38,7 @@ import org.springframework.transaction.PlatformTransactionManager;
|
||||||
* @author Andy Wilkinson
|
* @author Andy Wilkinson
|
||||||
* @author Kazuki Shimizu
|
* @author Kazuki Shimizu
|
||||||
* @author Stephane Nicoll
|
* @author Stephane Nicoll
|
||||||
|
* @author Mukul Kumar Chaundhyan
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
*/
|
*/
|
||||||
public class BasicBatchConfigurer implements BatchConfigurer, InitializingBean {
|
public class BasicBatchConfigurer implements BatchConfigurer, InitializingBean {
|
||||||
|
@ -111,7 +112,7 @@ public class BasicBatchConfigurer implements BatchConfigurer, InitializingBean {
|
||||||
PropertyMapper map = PropertyMapper.get();
|
PropertyMapper map = PropertyMapper.get();
|
||||||
JobExplorerFactoryBean factory = new JobExplorerFactoryBean();
|
JobExplorerFactoryBean factory = new JobExplorerFactoryBean();
|
||||||
factory.setDataSource(this.dataSource);
|
factory.setDataSource(this.dataSource);
|
||||||
map.from(this.properties::getTablePrefix).whenHasText().to(factory::setTablePrefix);
|
map.from(this.properties.getJdbc()::getTablePrefix).whenHasText().to(factory::setTablePrefix);
|
||||||
factory.afterPropertiesSet();
|
factory.afterPropertiesSet();
|
||||||
return factory.getObject();
|
return factory.getObject();
|
||||||
}
|
}
|
||||||
|
@ -128,7 +129,7 @@ public class BasicBatchConfigurer implements BatchConfigurer, InitializingBean {
|
||||||
PropertyMapper map = PropertyMapper.get();
|
PropertyMapper map = PropertyMapper.get();
|
||||||
map.from(this.dataSource).to(factory::setDataSource);
|
map.from(this.dataSource).to(factory::setDataSource);
|
||||||
map.from(this::determineIsolationLevel).whenNonNull().to(factory::setIsolationLevelForCreate);
|
map.from(this::determineIsolationLevel).whenNonNull().to(factory::setIsolationLevelForCreate);
|
||||||
map.from(this.properties::getTablePrefix).whenHasText().to(factory::setTablePrefix);
|
map.from(this.properties.getJdbc()::getTablePrefix).whenHasText().to(factory::setTablePrefix);
|
||||||
map.from(this::getTransactionManager).to(factory::setTransactionManager);
|
map.from(this::getTransactionManager).to(factory::setTransactionManager);
|
||||||
factory.afterPropertiesSet();
|
factory.afterPropertiesSet();
|
||||||
return factory.getObject();
|
return factory.getObject();
|
||||||
|
|
|
@ -57,6 +57,7 @@ import org.springframework.util.StringUtils;
|
||||||
* @author Eddú Meléndez
|
* @author Eddú Meléndez
|
||||||
* @author Kazuki Shimizu
|
* @author Kazuki Shimizu
|
||||||
* @author Mahmoud Ben Hassine
|
* @author Mahmoud Ben Hassine
|
||||||
|
* @author Mukul Kumar Chaundhyan
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
*/
|
*/
|
||||||
@Configuration(proxyBeanMethods = false)
|
@Configuration(proxyBeanMethods = false)
|
||||||
|
@ -107,11 +108,13 @@ public class BatchAutoConfiguration {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean
|
@ConditionalOnMissingBean
|
||||||
|
@ConditionalOnProperty(prefix = "spring.batch.jdbc", name = "enabled", havingValue = "true",
|
||||||
|
matchIfMissing = true)
|
||||||
BatchDataSourceInitializer batchDataSourceInitializer(DataSource dataSource,
|
BatchDataSourceInitializer batchDataSourceInitializer(DataSource dataSource,
|
||||||
@BatchDataSource ObjectProvider<DataSource> batchDataSource, ResourceLoader resourceLoader,
|
@BatchDataSource ObjectProvider<DataSource> batchDataSource, ResourceLoader resourceLoader,
|
||||||
BatchProperties properties) {
|
BatchProperties properties) {
|
||||||
return new BatchDataSourceInitializer(batchDataSource.getIfAvailable(() -> dataSource), resourceLoader,
|
return new BatchDataSourceInitializer(batchDataSource.getIfAvailable(() -> dataSource), resourceLoader,
|
||||||
properties);
|
properties.getJdbc());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,7 @@ package org.springframework.boot.autoconfigure.batch;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.batch.BatchProperties.Jdbc;
|
||||||
import org.springframework.boot.jdbc.AbstractDataSourceInitializer;
|
import org.springframework.boot.jdbc.AbstractDataSourceInitializer;
|
||||||
import org.springframework.boot.jdbc.DataSourceInitializationMode;
|
import org.springframework.boot.jdbc.DataSourceInitializationMode;
|
||||||
import org.springframework.core.io.ResourceLoader;
|
import org.springframework.core.io.ResourceLoader;
|
||||||
|
@ -28,27 +29,27 @@ import org.springframework.util.Assert;
|
||||||
*
|
*
|
||||||
* @author Dave Syer
|
* @author Dave Syer
|
||||||
* @author Vedran Pavic
|
* @author Vedran Pavic
|
||||||
|
* @author Mukul Kumar Chaundhyan
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
*/
|
*/
|
||||||
public class BatchDataSourceInitializer extends AbstractDataSourceInitializer {
|
public class BatchDataSourceInitializer extends AbstractDataSourceInitializer {
|
||||||
|
|
||||||
private final BatchProperties properties;
|
private final Jdbc jdbcProperties;
|
||||||
|
|
||||||
public BatchDataSourceInitializer(DataSource dataSource, ResourceLoader resourceLoader,
|
public BatchDataSourceInitializer(DataSource dataSource, ResourceLoader resourceLoader, Jdbc jdbcProperties) {
|
||||||
BatchProperties properties) {
|
|
||||||
super(dataSource, resourceLoader);
|
super(dataSource, resourceLoader);
|
||||||
Assert.notNull(properties, "BatchProperties must not be null");
|
Assert.notNull(jdbcProperties, "Jdbc Batch Properties must not be null");
|
||||||
this.properties = properties;
|
this.jdbcProperties = jdbcProperties;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected DataSourceInitializationMode getMode() {
|
protected DataSourceInitializationMode getMode() {
|
||||||
return this.properties.getInitializeSchema();
|
return this.jdbcProperties.getInitializeSchema();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getSchemaLocation() {
|
protected String getSchemaLocation() {
|
||||||
return this.properties.getSchema();
|
return this.jdbcProperties.getSchema();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
package org.springframework.boot.autoconfigure.batch;
|
package org.springframework.boot.autoconfigure.batch;
|
||||||
|
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.boot.context.properties.DeprecatedConfigurationProperty;
|
||||||
import org.springframework.boot.jdbc.DataSourceInitializationMode;
|
import org.springframework.boot.jdbc.DataSourceInitializationMode;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -25,11 +26,77 @@ import org.springframework.boot.jdbc.DataSourceInitializationMode;
|
||||||
* @author Stephane Nicoll
|
* @author Stephane Nicoll
|
||||||
* @author Eddú Meléndez
|
* @author Eddú Meléndez
|
||||||
* @author Vedran Pavic
|
* @author Vedran Pavic
|
||||||
|
* @author Mukul Kumar Chaundhyan
|
||||||
* @since 1.2.0
|
* @since 1.2.0
|
||||||
*/
|
*/
|
||||||
@ConfigurationProperties(prefix = "spring.batch")
|
@ConfigurationProperties(prefix = "spring.batch")
|
||||||
public class BatchProperties {
|
public class BatchProperties {
|
||||||
|
|
||||||
|
private final Job job = new Job();
|
||||||
|
|
||||||
|
private final Jdbc jdbc = new Jdbc();
|
||||||
|
|
||||||
|
@DeprecatedConfigurationProperty(replacement = "spring.batch.jdbc")
|
||||||
|
public String getSchema() {
|
||||||
|
return this.jdbc.getSchema();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSchema(String schema) {
|
||||||
|
this.jdbc.setSchema(schema);
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeprecatedConfigurationProperty(replacement = "spring.batch.jdbc")
|
||||||
|
public String getTablePrefix() {
|
||||||
|
return this.jdbc.getTablePrefix();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTablePrefix(String tablePrefix) {
|
||||||
|
this.jdbc.setTablePrefix(tablePrefix);
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeprecatedConfigurationProperty(replacement = "spring.batch.jdbc")
|
||||||
|
public DataSourceInitializationMode getInitializeSchema() {
|
||||||
|
return this.jdbc.getInitializeSchema();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setInitializeSchema(DataSourceInitializationMode initializeSchema) {
|
||||||
|
this.jdbc.setInitializeSchema(initializeSchema);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Job getJob() {
|
||||||
|
return this.job;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Jdbc getJdbc() {
|
||||||
|
return this.jdbc;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Job {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Comma-separated list of job names to execute on startup (for instance,
|
||||||
|
* `job1,job2`). By default, all Jobs found in the context are executed.
|
||||||
|
*/
|
||||||
|
private String names = "";
|
||||||
|
|
||||||
|
public String getNames() {
|
||||||
|
return this.names;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNames(String names) {
|
||||||
|
this.names = names;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* JDBC configuration properties for Spring Batch.
|
||||||
|
*
|
||||||
|
* @author Mukul Kumar Chaundhyan
|
||||||
|
* @since 2.5.0
|
||||||
|
*/
|
||||||
|
public static class Jdbc {
|
||||||
|
|
||||||
private static final String DEFAULT_SCHEMA_LOCATION = "classpath:org/springframework/"
|
private static final String DEFAULT_SCHEMA_LOCATION = "classpath:org/springframework/"
|
||||||
+ "batch/core/schema-@@platform@@.sql";
|
+ "batch/core/schema-@@platform@@.sql";
|
||||||
|
|
||||||
|
@ -48,8 +115,6 @@ public class BatchProperties {
|
||||||
*/
|
*/
|
||||||
private DataSourceInitializationMode initializeSchema = DataSourceInitializationMode.EMBEDDED;
|
private DataSourceInitializationMode initializeSchema = DataSourceInitializationMode.EMBEDDED;
|
||||||
|
|
||||||
private final Job job = new Job();
|
|
||||||
|
|
||||||
public String getSchema() {
|
public String getSchema() {
|
||||||
return this.schema;
|
return this.schema;
|
||||||
}
|
}
|
||||||
|
@ -74,26 +139,6 @@ public class BatchProperties {
|
||||||
this.initializeSchema = initializeSchema;
|
this.initializeSchema = initializeSchema;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Job getJob() {
|
|
||||||
return this.job;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class Job {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Comma-separated list of job names to execute on startup (for instance,
|
|
||||||
* `job1,job2`). By default, all Jobs found in the context are executed.
|
|
||||||
*/
|
|
||||||
private String names = "";
|
|
||||||
|
|
||||||
public String getNames() {
|
|
||||||
return this.names;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setNames(String names) {
|
|
||||||
this.names = names;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -187,6 +187,21 @@ class BatchAutoConfigurationTests {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testDisableSchemaLoaderWithNewJdbcProperties() {
|
||||||
|
this.contextRunner.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class)
|
||||||
|
.withPropertyValues("spring.datasource.generate-unique-name=true",
|
||||||
|
"spring.batch.jdbc.initialize-schema:never")
|
||||||
|
.run((context) -> {
|
||||||
|
assertThat(context).hasSingleBean(JobLauncher.class);
|
||||||
|
assertThat(context.getBean(BatchProperties.class).getInitializeSchema())
|
||||||
|
.isEqualTo(DataSourceInitializationMode.NEVER);
|
||||||
|
assertThatExceptionOfType(BadSqlGrammarException.class)
|
||||||
|
.isThrownBy(() -> new JdbcTemplate(context.getBean(DataSource.class))
|
||||||
|
.queryForList("select * from BATCH_JOB_EXECUTION"));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testUsingJpa() {
|
void testUsingJpa() {
|
||||||
this.contextRunner.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class,
|
this.contextRunner.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class,
|
||||||
|
@ -224,6 +239,27 @@ class BatchAutoConfigurationTests {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testRenamePrefixWithNewJdbcProperties() {
|
||||||
|
this.contextRunner
|
||||||
|
.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class,
|
||||||
|
HibernateJpaAutoConfiguration.class)
|
||||||
|
.withPropertyValues("spring.datasource.generate-unique-name=true",
|
||||||
|
"spring.batch.jdbc.schema:classpath:batch/custom-schema-hsql.sql",
|
||||||
|
"spring.batch.jdbc.tablePrefix:PREFIX_")
|
||||||
|
.run((context) -> {
|
||||||
|
assertThat(context).hasSingleBean(JobLauncher.class);
|
||||||
|
assertThat(context.getBean(BatchProperties.class).getInitializeSchema())
|
||||||
|
.isEqualTo(DataSourceInitializationMode.EMBEDDED);
|
||||||
|
assertThat(new JdbcTemplate(context.getBean(DataSource.class))
|
||||||
|
.queryForList("select * from PREFIX_JOB_EXECUTION")).isEmpty();
|
||||||
|
JobExplorer jobExplorer = context.getBean(JobExplorer.class);
|
||||||
|
assertThat(jobExplorer.findRunningJobExecutions("test")).isEmpty();
|
||||||
|
JobRepository jobRepository = context.getBean(JobRepository.class);
|
||||||
|
assertThat(jobRepository.getLastJobExecution("test", new JobParameters())).isNull();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testCustomizeJpaTransactionManagerUsingProperties() {
|
void testCustomizeJpaTransactionManagerUsingProperties() {
|
||||||
this.contextRunner
|
this.contextRunner
|
||||||
|
|
|
@ -84,6 +84,21 @@ class BatchAutoConfigurationWithoutJpaTests {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void jdbcWithCustomPrefixWithNewJdbcProperties() {
|
||||||
|
this.contextRunner.withUserConfiguration(DefaultConfiguration.class, EmbeddedDataSourceConfiguration.class)
|
||||||
|
.withPropertyValues("spring.datasource.generate-unique-name=true",
|
||||||
|
"spring.batch.jdbc.schema:classpath:batch/custom-schema-hsql.sql",
|
||||||
|
"spring.batch.jdbc.tablePrefix:PREFIX_")
|
||||||
|
.run((context) -> {
|
||||||
|
assertThat(new JdbcTemplate(context.getBean(DataSource.class))
|
||||||
|
.queryForList("select * from PREFIX_JOB_EXECUTION")).isEmpty();
|
||||||
|
assertThat(context.getBean(JobExplorer.class).findRunningJobExecutions("test")).isEmpty();
|
||||||
|
assertThat(context.getBean(JobRepository.class).getLastJobExecution("test", new JobParameters()))
|
||||||
|
.isNull();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@EnableBatchProcessing
|
@EnableBatchProcessing
|
||||||
@TestAutoConfigurationPackage(City.class)
|
@TestAutoConfigurationPackage(City.class)
|
||||||
static class DefaultConfiguration {
|
static class DefaultConfiguration {
|
||||||
|
|
|
@ -228,7 +228,7 @@ class JobLauncherApplicationRunnerTests {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
BatchDataSourceInitializer batchDataSourceInitializer(ResourceLoader resourceLoader) {
|
BatchDataSourceInitializer batchDataSourceInitializer(ResourceLoader resourceLoader) {
|
||||||
return new BatchDataSourceInitializer(this.dataSource, resourceLoader, new BatchProperties());
|
return new BatchDataSourceInitializer(this.dataSource, resourceLoader, new BatchProperties().getJdbc());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue