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;
}