committed by
Michael Minella
parent
2eb1c49e12
commit
10bb457a5a
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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"));
|
||||
|
||||
Reference in New Issue
Block a user