Commit 2de791f5 authored by Stephane Nicoll's avatar Stephane Nicoll

Merge pull request #3002 from eddumelendez/gh-2132

* gh-2132:
  polish batch table prefix customization
  Customize table prefix of batch meta-data tables
parents a0c08222 e0f59d8a
/* /*
* Copyright 2012-2014 the original author or authors. * Copyright 2012-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -33,6 +33,7 @@ import org.springframework.jdbc.datasource.DataSourceTransactionManager; ...@@ -33,6 +33,7 @@ import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.util.StringUtils;
/** /**
* Basic {@link BatchConfigurer} implementation. * Basic {@link BatchConfigurer} implementation.
...@@ -41,10 +42,12 @@ import org.springframework.transaction.PlatformTransactionManager; ...@@ -41,10 +42,12 @@ import org.springframework.transaction.PlatformTransactionManager;
* @author Andy Wilkinson * @author Andy Wilkinson
*/ */
@Component @Component
public class BasicBatchConfigurer implements BatchConfigurer { class BasicBatchConfigurer implements BatchConfigurer {
private static Log logger = LogFactory.getLog(BasicBatchConfigurer.class); private static Log logger = LogFactory.getLog(BasicBatchConfigurer.class);
private final BatchProperties properties;
private final DataSource dataSource; private final DataSource dataSource;
private final EntityManagerFactory entityManagerFactory; private final EntityManagerFactory entityManagerFactory;
...@@ -61,8 +64,8 @@ public class BasicBatchConfigurer implements BatchConfigurer { ...@@ -61,8 +64,8 @@ public class BasicBatchConfigurer implements BatchConfigurer {
* Create a new {@link BasicBatchConfigurer} instance. * Create a new {@link BasicBatchConfigurer} instance.
* @param dataSource the underlying data source * @param dataSource the underlying data source
*/ */
public BasicBatchConfigurer(DataSource dataSource) { public BasicBatchConfigurer(BatchProperties properties, DataSource dataSource) {
this(dataSource, null); this(properties, dataSource, null);
} }
/** /**
...@@ -70,8 +73,9 @@ public class BasicBatchConfigurer implements BatchConfigurer { ...@@ -70,8 +73,9 @@ public class BasicBatchConfigurer implements BatchConfigurer {
* @param dataSource the underlying data source * @param dataSource the underlying data source
* @param entityManagerFactory the entity manager factory (or {@code null}) * @param entityManagerFactory the entity manager factory (or {@code null})
*/ */
public BasicBatchConfigurer(DataSource dataSource, public BasicBatchConfigurer(BatchProperties properties, DataSource dataSource,
EntityManagerFactory entityManagerFactory) { EntityManagerFactory entityManagerFactory) {
this.properties = properties;
this.entityManagerFactory = entityManagerFactory; this.entityManagerFactory = entityManagerFactory;
this.dataSource = dataSource; this.dataSource = dataSource;
} }
...@@ -112,6 +116,10 @@ public class BasicBatchConfigurer implements BatchConfigurer { ...@@ -112,6 +116,10 @@ public class BasicBatchConfigurer implements BatchConfigurer {
private JobExplorer createJobExplorer() throws Exception { private JobExplorer createJobExplorer() throws Exception {
JobExplorerFactoryBean jobExplorerFactoryBean = new JobExplorerFactoryBean(); JobExplorerFactoryBean jobExplorerFactoryBean = new JobExplorerFactoryBean();
jobExplorerFactoryBean.setDataSource(this.dataSource); jobExplorerFactoryBean.setDataSource(this.dataSource);
String tablePrefix = this.properties.getTablePrefix();
if (StringUtils.hasText(tablePrefix)) {
jobExplorerFactoryBean.setTablePrefix(tablePrefix);
}
jobExplorerFactoryBean.afterPropertiesSet(); jobExplorerFactoryBean.afterPropertiesSet();
return jobExplorerFactoryBean.getObject(); return jobExplorerFactoryBean.getObject();
} }
......
/* /*
* Copyright 2012-2014 the original author or authors. * Copyright 2012-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -55,6 +55,7 @@ import org.springframework.util.StringUtils; ...@@ -55,6 +55,7 @@ import org.springframework.util.StringUtils;
* JobRegistry. * JobRegistry.
* *
* @author Dave Syer * @author Dave Syer
* @author Eddú Meléndez
*/ */
@Configuration @Configuration
@ConditionalOnClass({ JobLauncher.class, DataSource.class, JdbcOperations.class }) @ConditionalOnClass({ JobLauncher.class, DataSource.class, JdbcOperations.class })
...@@ -102,6 +103,10 @@ public class BatchAutoConfiguration { ...@@ -102,6 +103,10 @@ public class BatchAutoConfiguration {
public JobExplorer jobExplorer(DataSource dataSource) throws Exception { public JobExplorer jobExplorer(DataSource dataSource) throws Exception {
JobExplorerFactoryBean factory = new JobExplorerFactoryBean(); JobExplorerFactoryBean factory = new JobExplorerFactoryBean();
factory.setDataSource(dataSource); factory.setDataSource(dataSource);
String tablePrefix = this.properties.getTablePrefix();
if (StringUtils.hasText(tablePrefix)) {
factory.setTablePrefix(tablePrefix);
}
factory.afterPropertiesSet(); factory.afterPropertiesSet();
return factory.getObject(); return factory.getObject();
} }
...@@ -126,6 +131,9 @@ public class BatchAutoConfiguration { ...@@ -126,6 +131,9 @@ public class BatchAutoConfiguration {
@Configuration @Configuration
protected static class JpaBatchConfiguration { protected static class JpaBatchConfiguration {
@Autowired
private BatchProperties properties;
// The EntityManagerFactory may not be discoverable by type when this condition // The EntityManagerFactory may not be discoverable by type when this condition
// is evaluated, so we need a well-known bean name. This is the one used by Spring // is evaluated, so we need a well-known bean name. This is the one used by Spring
// Boot in the JPA auto configuration. // Boot in the JPA auto configuration.
...@@ -133,13 +141,13 @@ public class BatchAutoConfiguration { ...@@ -133,13 +141,13 @@ public class BatchAutoConfiguration {
@ConditionalOnBean(name = "entityManagerFactory") @ConditionalOnBean(name = "entityManagerFactory")
public BatchConfigurer jpaBatchConfigurer(DataSource dataSource, public BatchConfigurer jpaBatchConfigurer(DataSource dataSource,
EntityManagerFactory entityManagerFactory) { EntityManagerFactory entityManagerFactory) {
return new BasicBatchConfigurer(dataSource, entityManagerFactory); return new BasicBatchConfigurer(this.properties, dataSource, entityManagerFactory);
} }
@Bean @Bean
@ConditionalOnMissingBean(name = "entityManagerFactory") @ConditionalOnMissingBean(name = "entityManagerFactory")
public BatchConfigurer basicBatchConfigurer(DataSource dataSource) { public BatchConfigurer basicBatchConfigurer(DataSource dataSource) {
return new BasicBatchConfigurer(dataSource); return new BasicBatchConfigurer(this.properties, dataSource);
} }
} }
......
/* /*
* Copyright 2012-2014 the original author or authors. * Copyright 2012-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -22,6 +22,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties; ...@@ -22,6 +22,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
* Configuration properties for Spring Batch. * Configuration properties for Spring Batch.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Eddú Meléndez
* @since 1.2.0 * @since 1.2.0
*/ */
@ConfigurationProperties("spring.batch") @ConfigurationProperties("spring.batch")
...@@ -35,6 +36,11 @@ public class BatchProperties { ...@@ -35,6 +36,11 @@ public class BatchProperties {
*/ */
private String schema = DEFAULT_SCHEMA_LOCATION; private String schema = DEFAULT_SCHEMA_LOCATION;
/**
* Table prefix for all the batch meta-data tables.
*/
private String tablePrefix;
private final Initializer initializer = new Initializer(); private final Initializer initializer = new Initializer();
private final Job job = new Job(); private final Job job = new Job();
...@@ -55,6 +61,14 @@ public class BatchProperties { ...@@ -55,6 +61,14 @@ public class BatchProperties {
return this.job; return this.job;
} }
public void setTablePrefix(String tablePrefix) {
this.tablePrefix = tablePrefix;
}
public String getTablePrefix() {
return this.tablePrefix;
}
public static class Initializer { public static class Initializer {
/** /**
......
/* /*
* Copyright 2012-2014 the original author or authors. * Copyright 2012-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -211,6 +211,24 @@ public class BatchAutoConfigurationTests { ...@@ -211,6 +211,24 @@ public class BatchAutoConfigurationTests {
new JobParameters())); 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 @Configuration
protected static class EmptyConfiguration { protected static class EmptyConfiguration {
} }
......
-- 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
);
...@@ -450,11 +450,12 @@ content into your application; rather pick only the properties that you need. ...@@ -450,11 +450,12 @@ content into your application; rather pick only the properties that you need.
spring.mail.default-encoding=UTF-8 # encoding to use for MimeMessages spring.mail.default-encoding=UTF-8 # encoding to use for MimeMessages
spring.mail.properties.*= # properties to set on the JavaMail session spring.mail.properties.*= # properties to set on the JavaMail session
# SPRING BATCH ({sc-spring-boot-autoconfigure}/batch/BatchDatabaseInitializer.{sc-ext}[BatchDatabaseInitializer]) # SPRING BATCH ({sc-spring-boot-autoconfigure}/batch/BatchProperties.{sc-ext}[BatchProperties])
spring.batch.job.names=job1,job2 spring.batch.job.names=job1,job2
spring.batch.job.enabled=true spring.batch.job.enabled=true
spring.batch.initializer.enabled=true spring.batch.initializer.enabled=true
spring.batch.schema= # batch schema to load spring.batch.schema= # batch schema to load
spring.batch.table-prefix= # table prefix for all the batch meta-data tables
# SPRING CACHE ({sc-spring-boot-autoconfigure}/cache/CacheProperties.{sc-ext}[CacheProperties]) # SPRING CACHE ({sc-spring-boot-autoconfigure}/cache/CacheProperties.{sc-ext}[CacheProperties])
spring.cache.type= # generic, ehcache, hazelcast, jcache, redis, guava, simple, none spring.cache.type= # generic, ehcache, hazelcast, jcache, redis, guava, simple, none
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment