Allows user to set table prefix via properties.

resolves #244
This commit is contained in:
Glenn Renfro
2016-12-15 16:01:30 -05:00
committed by Michael Minella
parent 2eb1c49e12
commit 10bb457a5a
11 changed files with 165 additions and 24 deletions

View File

@@ -24,8 +24,10 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.task.batch.listener.TaskBatchExecutionListener;
import org.springframework.cloud.task.configuration.TaskConfigurer;
import org.springframework.cloud.task.configuration.TaskProperties;
import org.springframework.cloud.task.repository.TaskExplorer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
@@ -50,14 +52,17 @@ public class TaskBatchAutoConfiguration {
@Configuration
@ConditionalOnMissingBean(name = "taskBatchExecutionListener")
@EnableConfigurationProperties(TaskProperties.class)
public static class TaskBatchExecutionListenerAutoconfiguration {
@Autowired
private ApplicationContext context;
@Autowired
private TaskProperties taskProperties;
@Bean
public TaskBatchExecutionListenerFactoryBean taskBatchExecutionListener(TaskExplorer taskExplorer) {
TaskConfigurer taskConfigurer = null;
if(!context.getBeansOfType(TaskConfigurer.class).isEmpty()) {
taskConfigurer = context.getBean(TaskConfigurer.class);

View File

@@ -24,9 +24,11 @@ import org.springframework.beans.factory.FactoryBean;
import org.springframework.cloud.task.batch.listener.TaskBatchExecutionListener;
import org.springframework.cloud.task.batch.listener.support.JdbcTaskBatchDao;
import org.springframework.cloud.task.batch.listener.support.MapTaskBatchDao;
import org.springframework.cloud.task.configuration.TaskProperties;
import org.springframework.cloud.task.repository.TaskExplorer;
import org.springframework.cloud.task.repository.dao.MapTaskExecutionDao;
import org.springframework.cloud.task.repository.support.SimpleTaskExplorer;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;
/**
@@ -44,11 +46,34 @@ public class TaskBatchExecutionListenerFactoryBean implements FactoryBean<TaskBa
private TaskExplorer taskExplorer;
private String tablePrefix = TaskProperties.DEFAULT_TABLE_PREFIX;
/**
* Initializes the TaskBatchExecutionListenerFactoryBean and defaults the
* tablePrefix to {@link TaskProperties#DEFAULT_TABLE_PREFIX}.
*
* @param dataSource the dataSource to use for the TaskBatchExecutionListener.
* @param taskExplorer the taskExplorer to use for the TaskBatchExecutionListener.
*/
public TaskBatchExecutionListenerFactoryBean(DataSource dataSource, TaskExplorer taskExplorer) {
this.dataSource = dataSource;
this.taskExplorer = taskExplorer;
}
/**
* Initializes the TaskBatchExecutionListenerFactoryBean.
*
* @param dataSource the dataSource to use for the TaskBatchExecutionListener.
* @param taskExplorer the taskExplorer to use for the TaskBatchExecutionListener.
* @param tablePrefix the prefix for the task tables accessed by the
* TaskBatchExecutionListener.
*/
public TaskBatchExecutionListenerFactoryBean(DataSource dataSource, TaskExplorer taskExplorer, String tablePrefix) {
this(dataSource,taskExplorer);
Assert.hasText(tablePrefix, "tablePrefix must not be null nor empty.");
this.tablePrefix = tablePrefix;
}
@Override
public TaskBatchExecutionListener getObject() throws Exception {
if(listener != null){
@@ -58,7 +83,8 @@ public class TaskBatchExecutionListenerFactoryBean implements FactoryBean<TaskBa
this.listener = new TaskBatchExecutionListener(getMapTaskBatchDao());
}
else {
this.listener = new TaskBatchExecutionListener(new JdbcTaskBatchDao(this.dataSource));
this.listener = new TaskBatchExecutionListener(
new JdbcTaskBatchDao(this.dataSource, tablePrefix));
}
return listener;

View File

@@ -19,6 +19,7 @@ import javax.sql.DataSource;
import org.springframework.batch.core.JobExecution;
import org.springframework.cloud.task.batch.listener.TaskBatchDao;
import org.springframework.cloud.task.configuration.TaskProperties;
import org.springframework.cloud.task.repository.TaskExecution;
import org.springframework.cloud.task.repository.dao.JdbcTaskExecutionDao;
import org.springframework.jdbc.core.JdbcOperations;
@@ -35,13 +36,26 @@ import org.springframework.util.StringUtils;
*/
public class JdbcTaskBatchDao implements TaskBatchDao {
private String tablePrefix = JdbcTaskExecutionDao.DEFAULT_TABLE_PREFIX;
private String tablePrefix = TaskProperties.DEFAULT_TABLE_PREFIX;
private static final String INSERT_STATEMENT = "INSERT INTO %PREFIX%TASK_BATCH VALUES(?, ?)";
private JdbcOperations jdbcTemplate;
/**
* Intializes the JdbcTaskBatchDao.
* @param dataSource {@link DataSource} where the task batch table resides.
* @param tablePrefix the table prefix to use for this dao.
*/
public JdbcTaskBatchDao(DataSource dataSource, String tablePrefix) {
this(dataSource);
Assert.hasText(tablePrefix, "tablePrefix must not be null nor empty.");
this.tablePrefix = tablePrefix;
}
/**
* Intializes the JdbcTaskBatchDao and defaults the table prefix to
* {@link TaskProperties#DEFAULT_TABLE_PREFIX}.
* @param dataSource {@link DataSource} where the task batch table resides.
*/
public JdbcTaskBatchDao(DataSource dataSource) {
@@ -60,9 +74,10 @@ public class JdbcTaskBatchDao implements TaskBatchDao {
/**
* The table prefix for the task batch table.
*
* @param tablePrefix defaults to {@link JdbcTaskExecutionDao#DEFAULT_TABLE_PREFIX}.
* @param tablePrefix defaults to {@link TaskProperties#DEFAULT_TABLE_PREFIX}.
*/
public void setTablePrefix(String tablePrefix) {
@Deprecated
public void setTablePrefix(String tablePrefix) {
Assert.notNull(tablePrefix, "Null is not allowed as a tablePrefix (use an empty string if you don't want a prefix at all).");
this.tablePrefix = tablePrefix;
}

View File

@@ -55,24 +55,45 @@ public class DefaultTaskConfigurer implements TaskConfigurer {
private DataSource dataSource;
public DefaultTaskConfigurer() {
this(null);
this(TaskProperties.DEFAULT_TABLE_PREFIX);
}
/**
* Initializes the DefaultTaskConfigurer and sets the default table prefix
* to {@link TaskProperties#DEFAULT_TABLE_PREFIX}.
* @param dataSource references the {@link DataSource} to be used as the Task
* repository. If none is provided, a Map will be used (not recommended for
* production use.
*/
public DefaultTaskConfigurer(DataSource dataSource) {
this(dataSource, TaskProperties.DEFAULT_TABLE_PREFIX);
}
/** Initializes the DefaultTaskConfigurer.
* @param tablePrefix the prefix to apply to the task table names used by
* task infrastructure.
*/
public DefaultTaskConfigurer(String tablePrefix) {
this(null, tablePrefix);
}
/** Initializes the DefaultTaskConfigurer.
* @param dataSource references the {@link DataSource} to be used as the Task
* repository. If none is provided, a Map will be used (not recommended for
* production use.
* @param tablePrefix the prefix to apply to the task table names used by
* task infrastructure.
*/
public DefaultTaskConfigurer(DataSource dataSource, String tablePrefix) {
this.dataSource = dataSource;
if(this.dataSource != null) {
this.taskExecutionDaoFactoryBean = new TaskExecutionDaoFactoryBean(this.dataSource);
this.taskExecutionDaoFactoryBean = new
TaskExecutionDaoFactoryBean(this.dataSource, tablePrefix);
}
else {
this.taskExecutionDaoFactoryBean = new TaskExecutionDaoFactoryBean();
}
this.taskRepository = new SimpleTaskRepository(this.taskExecutionDaoFactoryBean);
this.taskExplorer = new SimpleTaskExplorer(this.taskExecutionDaoFactoryBean);
}

View File

@@ -155,11 +155,13 @@ public class SimpleTaskConfiguration {
if (configurers < 1) {
TaskConfigurer taskConfigurer;
if(!CollectionUtils.isEmpty(this.dataSources) && this.dataSources.size() == 1) {
taskConfigurer = new DefaultTaskConfigurer(this.dataSources.iterator().next());
taskConfigurer = new DefaultTaskConfigurer(
this.dataSources.iterator().next(),
taskProperties.getTablePrefix());
}
else {
taskConfigurer = new DefaultTaskConfigurer();
taskConfigurer = new DefaultTaskConfigurer(
taskProperties.getTablePrefix());
}
this.context.getBeanFactory().registerSingleton("taskConfigurer", taskConfigurer);
return taskConfigurer;

View File

@@ -27,6 +27,8 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "spring.cloud.task")
public class TaskProperties {
public static final String DEFAULT_TABLE_PREFIX = "TASK_";
/**
* An id that can be associated with a task.
*/
@@ -37,6 +39,11 @@ public class TaskProperties {
*/
private Integer executionid;
/**
* The prefix to append to the table names created by Spring Cloud Task.
*/
private String tablePrefix = DEFAULT_TABLE_PREFIX;
/**
* When set to true the context is closed at the end of the task. Else
* the context remains open.
@@ -66,4 +73,12 @@ public class TaskProperties {
public void setClosecontextEnable(Boolean closecontextEnable) {
this.closecontextEnable = closecontextEnable;
}
public String getTablePrefix() {
return tablePrefix;
}
public void setTablePrefix(String tablePrefix) {
this.tablePrefix = tablePrefix;
}
}

View File

@@ -30,6 +30,7 @@ import java.util.TreeSet;
import javax.sql.DataSource;
import org.springframework.batch.item.database.Order;
import org.springframework.cloud.task.configuration.TaskProperties;
import org.springframework.cloud.task.repository.TaskExecution;
import org.springframework.cloud.task.repository.database.PagingQueryProvider;
import org.springframework.cloud.task.repository.database.support.SqlPagingQueryProviderFactoryBean;
@@ -111,9 +112,7 @@ public class JdbcTaskExecutionDao implements TaskExecutionDao {
private static final String FIND_TASK_EXECUTION_BY_JOB_EXECUTION_ID = "SELECT TASK_EXECUTION_ID FROM %PREFIX%TASK_BATCH WHERE JOB_EXECUTION_ID = ?";
private static final String FIND_JOB_EXECUTION_BY_TASK_EXECUTION_ID = "SELECT JOB_EXECUTION_ID FROM %PREFIX%TASK_BATCH WHERE TASK_EXECUTION_ID = ?";
public static final String DEFAULT_TABLE_PREFIX = "TASK_";
private String tablePrefix = DEFAULT_TABLE_PREFIX;
private String tablePrefix = TaskProperties.DEFAULT_TABLE_PREFIX;
private JdbcOperations jdbcTemplate;
@@ -123,6 +122,22 @@ public class JdbcTaskExecutionDao implements TaskExecutionDao {
private DataFieldMaxValueIncrementer taskIncrementer;
/**
* Initializes the JdbcTaskExecutionDao.
* @param dataSource used by the dao to execute queries and update the tables.
* @param tablePrefix the table prefix to use for this dao.
*/
public JdbcTaskExecutionDao(DataSource dataSource, String tablePrefix) {
this(dataSource);
Assert.hasText(tablePrefix, "tablePrefix must not be null nor empty");
this.tablePrefix = tablePrefix;
}
/**
* Initializes the JdbTaskExecutionDao and defaults the table prefix to
* {@link TaskProperties#DEFAULT_TABLE_PREFIX}.
* @param dataSource used by the dao to execute queries and update the tables.
*/
public JdbcTaskExecutionDao(DataSource dataSource) {
Assert.notNull(dataSource);
this.jdbcTemplate = new JdbcTemplate(dataSource);
@@ -192,10 +207,11 @@ public class JdbcTaskExecutionDao implements TaskExecutionDao {
/**
* Public setter for the table prefix property. This will be prefixed to all
* the table names before queries are executed. Defaults to
* {@link #DEFAULT_TABLE_PREFIX}.
* {@link TaskProperties#DEFAULT_TABLE_PREFIX}.
*
* @param tablePrefix the tablePrefix to set
*/
@Deprecated
public void setTablePrefix(String tablePrefix) {
this.tablePrefix = tablePrefix;
}

View File

@@ -20,6 +20,8 @@ import javax.sql.DataSource;
import org.springframework.batch.item.database.support.DataFieldMaxValueIncrementerFactory;
import org.springframework.batch.item.database.support.DefaultDataFieldMaxValueIncrementerFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.cloud.task.configuration.TaskConfigurer;
import org.springframework.cloud.task.configuration.TaskProperties;
import org.springframework.cloud.task.repository.dao.JdbcTaskExecutionDao;
import org.springframework.cloud.task.repository.dao.MapTaskExecutionDao;
import org.springframework.cloud.task.repository.dao.TaskExecutionDao;
@@ -31,16 +33,15 @@ import org.springframework.util.Assert;
* {@link TaskExecutionDao} based on the provided information.
*
* @author Michael Minella
* @author Glenn Renfro
*/
public class TaskExecutionDaoFactoryBean implements FactoryBean<TaskExecutionDao> {
public static final String DEFAULT_TABLE_PREFIX = "TASK_";
private DataSource dataSource;
private TaskExecutionDao dao = null;
private String tablePrefix = DEFAULT_TABLE_PREFIX;
private String tablePrefix = TaskProperties.DEFAULT_TABLE_PREFIX;
/**
* Default constructor will result in a Map based TaskExecutionDao. <b>This is only
@@ -49,6 +50,18 @@ public class TaskExecutionDaoFactoryBean implements FactoryBean<TaskExecutionDao
public TaskExecutionDaoFactoryBean() {
}
/**
* {@link DataSource} to be used.
*
* @param dataSource {@link DataSource} to be used.
* @param tablePrefix the table prefix to use for this dao.
*/
public TaskExecutionDaoFactoryBean(DataSource dataSource, String tablePrefix) {
this(dataSource);
Assert.hasText(tablePrefix, "tablePrefix must not be null nor empty");
this.tablePrefix = tablePrefix;
}
/**
* {@link DataSource} to be used.
*
@@ -90,13 +103,14 @@ public class TaskExecutionDaoFactoryBean implements FactoryBean<TaskExecutionDao
*
* @param tablePrefix the string prefix for the task table names
*/
@Deprecated
public void setTablePrefix(String tablePrefix) {
this.tablePrefix = tablePrefix;
}
private void buildTaskExecutionDao(DataSource dataSource) {
DataFieldMaxValueIncrementerFactory incrementerFactory = new DefaultDataFieldMaxValueIncrementerFactory(dataSource);
this.dao = new JdbcTaskExecutionDao(dataSource);
this.dao = new JdbcTaskExecutionDao(dataSource, this.tablePrefix);
String databaseType;
try {
databaseType = org.springframework.batch.support.DatabaseType.fromMetaData(dataSource).name();
@@ -105,6 +119,5 @@ public class TaskExecutionDaoFactoryBean implements FactoryBean<TaskExecutionDao
throw new IllegalStateException(e);
}
((JdbcTaskExecutionDao) this.dao).setTaskIncrementer(incrementerFactory.getIncrementer(databaseType, this.tablePrefix + "SEQ"));
((JdbcTaskExecutionDao) this.dao).setTablePrefix(this.tablePrefix);
}
}

View File

@@ -16,6 +16,8 @@
package org.springframework.cloud.task;
import javax.sql.DataSource;
import org.junit.After;
import org.junit.Test;
@@ -79,12 +81,12 @@ public class SimpleTaskConfigurationTests {
@Bean
public TaskConfigurer taskConfigurer1() {
return new DefaultTaskConfigurer(null);
return new DefaultTaskConfigurer((DataSource) null);
}
@Bean
public TaskConfigurer taskConfigurer2() {
return new DefaultTaskConfigurer(null);
return new DefaultTaskConfigurer((DataSource) null);
}
}

View File

@@ -99,8 +99,7 @@ public class TaskExecutionDaoFactoryBeanTests {
DataSource dataSource = this.context.getBean(DataSource.class);
TaskExecutionDaoFactoryBean factoryBean = new TaskExecutionDaoFactoryBean(dataSource);
factoryBean.setTablePrefix("foo_");
TaskExecutionDaoFactoryBean factoryBean = new TaskExecutionDaoFactoryBean(dataSource, "foo_");
TaskExecutionDao taskExecutionDao = factoryBean.getObject();
assertEquals("foo_", ReflectionTestUtils.getField(taskExecutionDao, "tablePrefix"));

View File

@@ -135,6 +135,33 @@ If your application utilizes more than one `DataSource`, you'll need to configur
task repository with the appropriate `DataSource`. This customization can be done via an
implementation of the `TaskConfigurer`.
[[features-table-prefix]]
=== Table Prefix
One modifiable property of the TaskRepository is the table prefix for the
task tables. By default they are all prefaced with `TASK_`.
TASK_EXECUTION and TASK_EXECUTION_PARAMS are two examples. However, there are
potential reasons to modify this prefix. If the schema names needs to be
prepended to the table names, or if more than one set of task tables is
needed within the same schema, then the table prefix will need to be changed.
This is done by setting the `spring.cloud.task.tablePrefix` to the prefix
that is required.
```
spring.cloud.task.tablePrefix=<yourPrefix>
```
[[features-table-initialization]]
=== Enable/Disable table initialization
In cases where you are creating the task tables and do not wish for
Spring Cloud Task to create them (if not present) at task startup set the
`spring.cloud.task.initialize.enable` property to `false`. It is currently
defaulted to `true`.
```
spring.cloud.task.initialize.enable=<true or false>
```
[[features-generated_task_id]]
=== Externally Generated Task Id