diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BasicBatchConfigurer.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BasicBatchConfigurer.java index 6622eb57199..19b1df83483 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BasicBatchConfigurer.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BasicBatchConfigurer.java @@ -29,10 +29,12 @@ import org.springframework.batch.core.launch.JobLauncher; import org.springframework.batch.core.launch.support.SimpleJobLauncher; import org.springframework.batch.core.repository.JobRepository; import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.stereotype.Component; import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.util.StringUtils; /** * Basic {@link BatchConfigurer} implementation. @@ -57,6 +59,9 @@ public class BasicBatchConfigurer implements BatchConfigurer { private JobExplorer jobExplorer; + @Autowired + private BatchProperties properties; + /** * Create a new {@link BasicBatchConfigurer} instance. * @param dataSource the underlying data source @@ -112,6 +117,10 @@ public class BasicBatchConfigurer implements BatchConfigurer { private JobExplorer createJobExplorer() throws Exception { JobExplorerFactoryBean jobExplorerFactoryBean = new JobExplorerFactoryBean(); jobExplorerFactoryBean.setDataSource(this.dataSource); + String tablePrefix = this.properties.getTablePrefix(); + if (StringUtils.hasText(tablePrefix)) { + jobExplorerFactoryBean.setTablePrefix(tablePrefix); + } jobExplorerFactoryBean.afterPropertiesSet(); return jobExplorerFactoryBean.getObject(); } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration.java index 5dc30d5d3ba..c47af310f56 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration.java @@ -102,6 +102,10 @@ public class BatchAutoConfiguration { public JobExplorer jobExplorer(DataSource dataSource) throws Exception { JobExplorerFactoryBean factory = new JobExplorerFactoryBean(); factory.setDataSource(dataSource); + String tablePrefix = this.properties.getTablePrefix(); + if (StringUtils.hasText(tablePrefix)) { + factory.setTablePrefix(tablePrefix); + } factory.afterPropertiesSet(); return factory.getObject(); } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchProperties.java index 65f8aa70548..796d516aa37 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchProperties.java @@ -35,6 +35,8 @@ public class BatchProperties { */ private String schema = DEFAULT_SCHEMA_LOCATION; + private String tablePrefix; + private final Initializer initializer = new Initializer(); private final Job job = new Job(); @@ -55,6 +57,14 @@ public class BatchProperties { return this.job; } + public void setTablePrefix(String tablePrefix) { + this.tablePrefix = tablePrefix; + } + + public String getTablePrefix() { + return this.tablePrefix; + } + public static class Initializer { /** diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java index 02c8e92eb2f..a5f8897c19b 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java @@ -211,6 +211,24 @@ public class BatchAutoConfigurationTests { new JobParameters())); } + @Test + public void testRenamePrefix() throws Exception { + this.context = new AnnotationConfigApplicationContext(); + EnvironmentTestUtils.addEnvironment(this.context, + "spring.datasource.name:batchtest", + "spring.batch.schema:classpath:batch/custom-schema-hsql.sql", + "spring.batch.tablePrefix:PREFIX_"); + this.context.register(TestConfiguration.class, + EmbeddedDataSourceConfiguration.class, + HibernateJpaAutoConfiguration.class, BatchAutoConfiguration.class, + PropertyPlaceholderAutoConfiguration.class); + this.context.refresh(); + assertNotNull(this.context.getBean(JobLauncher.class)); + assertNotNull(this.context.getBean(JobExplorer.class)); + assertEquals(0, new JdbcTemplate(this.context.getBean(DataSource.class)) + .queryForList("select * from PREFIX_JOB_EXECUTION").size()); + } + @Configuration protected static class EmptyConfiguration { } diff --git a/spring-boot-autoconfigure/src/test/resources/batch/custom-schema-hsql.sql b/spring-boot-autoconfigure/src/test/resources/batch/custom-schema-hsql.sql new file mode 100644 index 00000000000..4ce9dc78abf --- /dev/null +++ b/spring-boot-autoconfigure/src/test/resources/batch/custom-schema-hsql.sql @@ -0,0 +1,87 @@ +-- Autogenerated: do not edit this file + +CREATE TABLE PREFIX_JOB_INSTANCE ( + JOB_INSTANCE_ID BIGINT IDENTITY NOT NULL PRIMARY KEY , + VERSION BIGINT , + JOB_NAME VARCHAR(100) NOT NULL, + JOB_KEY VARCHAR(32) NOT NULL, + constraint JOB_INST_UN unique (JOB_NAME, JOB_KEY) +) ; + +CREATE TABLE PREFIX_JOB_EXECUTION ( + JOB_EXECUTION_ID BIGINT IDENTITY NOT NULL PRIMARY KEY , + VERSION BIGINT , + JOB_INSTANCE_ID BIGINT NOT NULL, + CREATE_TIME TIMESTAMP NOT NULL, + START_TIME TIMESTAMP DEFAULT NULL , + END_TIME TIMESTAMP DEFAULT NULL , + STATUS VARCHAR(10) , + EXIT_CODE VARCHAR(2500) , + EXIT_MESSAGE VARCHAR(2500) , + LAST_UPDATED TIMESTAMP, + JOB_CONFIGURATION_LOCATION VARCHAR(2500) NULL, + constraint JOB_INST_EXEC_FK foreign key (JOB_INSTANCE_ID) + references PREFIX_JOB_INSTANCE(JOB_INSTANCE_ID) +) ; + +CREATE TABLE PREFIX_JOB_EXECUTION_PARAMS ( + JOB_EXECUTION_ID BIGINT NOT NULL , + TYPE_CD VARCHAR(6) NOT NULL , + KEY_NAME VARCHAR(100) NOT NULL , + STRING_VAL VARCHAR(250) , + DATE_VAL TIMESTAMP DEFAULT NULL , + LONG_VAL BIGINT , + DOUBLE_VAL DOUBLE PRECISION , + IDENTIFYING CHAR(1) NOT NULL , + constraint JOB_EXEC_PARAMS_FK foreign key (JOB_EXECUTION_ID) + references PREFIX_JOB_EXECUTION(JOB_EXECUTION_ID) +) ; + +CREATE TABLE PREFIX_STEP_EXECUTION ( + STEP_EXECUTION_ID BIGINT IDENTITY NOT NULL PRIMARY KEY , + VERSION BIGINT NOT NULL, + STEP_NAME VARCHAR(100) NOT NULL, + JOB_EXECUTION_ID BIGINT NOT NULL, + START_TIME TIMESTAMP NOT NULL , + END_TIME TIMESTAMP DEFAULT NULL , + STATUS VARCHAR(10) , + COMMIT_COUNT BIGINT , + READ_COUNT BIGINT , + FILTER_COUNT BIGINT , + WRITE_COUNT BIGINT , + READ_SKIP_COUNT BIGINT , + WRITE_SKIP_COUNT BIGINT , + PROCESS_SKIP_COUNT BIGINT , + ROLLBACK_COUNT BIGINT , + EXIT_CODE VARCHAR(2500) , + EXIT_MESSAGE VARCHAR(2500) , + LAST_UPDATED TIMESTAMP, + constraint JOB_EXEC_STEP_FK foreign key (JOB_EXECUTION_ID) + references PREFIX_JOB_EXECUTION(JOB_EXECUTION_ID) +) ; + +CREATE TABLE PREFIX_STEP_EXECUTION_CONTEXT ( + STEP_EXECUTION_ID BIGINT NOT NULL PRIMARY KEY, + SHORT_CONTEXT VARCHAR(2500) NOT NULL, + SERIALIZED_CONTEXT LONGVARCHAR , + constraint STEP_EXEC_CTX_FK foreign key (STEP_EXECUTION_ID) + references PREFIX_STEP_EXECUTION(STEP_EXECUTION_ID) +) ; + +CREATE TABLE PREFIX_JOB_EXECUTION_CONTEXT ( + JOB_EXECUTION_ID BIGINT NOT NULL PRIMARY KEY, + SHORT_CONTEXT VARCHAR(2500) NOT NULL, + SERIALIZED_CONTEXT LONGVARCHAR , + constraint JOB_EXEC_CTX_FK foreign key (JOB_EXECUTION_ID) + references PREFIX_JOB_EXECUTION(JOB_EXECUTION_ID) +) ; + +CREATE TABLE PREFIX_STEP_EXECUTION_SEQ ( + ID BIGINT IDENTITY +); +CREATE TABLE PREFIX_JOB_EXECUTION_SEQ ( + ID BIGINT IDENTITY +); +CREATE TABLE PREFIX_JOB_SEQ ( + ID BIGINT IDENTITY +);