Added TaskNameResolver and related implementation

TaskNameResolver allows the ability to customize how a task is named.
During normal use cases, the provided SimpleTaskNameResolver should
suffice.

resolves spring-cloud/spring-cloud-task#54
This commit is contained in:
Michael Minella
2016-01-06 15:27:00 -06:00
committed by Glenn Renfro
parent a2674b50bd
commit 1c3bb94bbd
9 changed files with 186 additions and 30 deletions

View File

@@ -27,7 +27,9 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.task.listener.TaskLifecycleListener;
import org.springframework.cloud.task.repository.TaskNameResolver;
import org.springframework.cloud.task.repository.TaskRepository;
import org.springframework.cloud.task.repository.support.SimpleTaskNameResolver;
import org.springframework.cloud.task.repository.support.TaskDatabaseInitializer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
@@ -76,7 +78,7 @@ public class SimpleTaskConfiguration {
@Bean
public TaskLifecycleListener taskLifecycleListener() {
return new TaskLifecycleListener(taskRepository());
return new TaskLifecycleListener(taskRepository(), taskNameResolver());
}
@Bean
@@ -84,6 +86,11 @@ public class SimpleTaskConfiguration {
return this.transactionManager;
}
@Bean
public TaskNameResolver taskNameResolver() {
return new SimpleTaskNameResolver();
}
/**
* Sets up the basic components by extracting them from the {@link TaskConfigurer}, defaulting to some
* sensible values as long as a unique DataSource is available.

View File

@@ -33,9 +33,9 @@ public interface TaskConfigurer {
*
* @return A TaskRepository
*/
public TaskRepository getTaskRepository();
TaskRepository getTaskRepository();
public PlatformTransactionManager getTransactionManager();
PlatformTransactionManager getTransactionManager();
public TaskExplorer getTaskExplorer();
TaskExplorer getTaskExplorer();
}

View File

@@ -23,16 +23,15 @@ import java.util.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.boot.context.event.ApplicationFailedEvent;
import org.springframework.cloud.task.repository.TaskExecution;
import org.springframework.cloud.task.repository.TaskNameResolver;
import org.springframework.cloud.task.repository.TaskRepository;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.util.Assert;
/**
* Monitors the lifecycle of a task. This listener will record both the start and end of
@@ -52,8 +51,7 @@ import org.springframework.context.event.ContextRefreshedEvent;
*
* @author Michael Minella
*/
public class TaskLifecycleListener implements ApplicationListener<ApplicationEvent>,
ApplicationContextAware {
public class TaskLifecycleListener implements ApplicationListener<ApplicationEvent>{
private final static Logger logger = LoggerFactory.getLogger(TaskLifecycleListener.class);
@@ -63,13 +61,18 @@ public class TaskLifecycleListener implements ApplicationListener<ApplicationEve
private boolean started = false;
private ApplicationContext applicationContext;
private TaskNameResolver taskNameResolver;
/**
* @param taskRepository The repository to record executions in.
*/
public TaskLifecycleListener(TaskRepository taskRepository) {
public TaskLifecycleListener(TaskRepository taskRepository,
TaskNameResolver taskNameResolver) {
Assert.notNull(taskRepository, "A taskRepository is required");
Assert.notNull(taskNameResolver, "A taskNameResolver is required");
this.taskRepository = taskRepository;
this.taskNameResolver = taskNameResolver;
}
/**
@@ -143,7 +146,7 @@ public class TaskLifecycleListener implements ApplicationListener<ApplicationEve
String executionId = UUID.randomUUID().toString();
this.taskExecution = new TaskExecution();
this.taskExecution.setTaskName(applicationContext.getId());
this.taskExecution.setTaskName(taskNameResolver.getTaskName());
this.taskExecution.setStartTime(new Date());
this.taskExecution.setExecutionId(executionId);
this.taskRepository.createTaskExecution(this.taskExecution);
@@ -161,10 +164,4 @@ public class TaskLifecycleListener implements ApplicationListener<ApplicationEve
TaskExecution getTaskExecution() {
return this.taskExecution;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;
}
}

View File

@@ -0,0 +1,29 @@
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.repository;
/**
* Strategy interface for customizing how the name of a task is determined.
*
* @author Michael Minella
*/
public interface TaskNameResolver {
/**
* @return the name of the task being executed within this context.
*/
String getTaskName();
}

View File

@@ -0,0 +1,61 @@
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.repository.support;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.task.repository.TaskNameResolver;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.util.StringUtils;
/**
* Simple implementation of the {@link TaskNameResolver} interface. Names the task based
* on the following order of precidence:
* <ol>
* <li>A configured property <code>spring.cloud.task.name</code></li>
* <li>The {@link ApplicationContext}'s id.</li>
* </ol>
*
* @author Michael Minella
* @see org.springframework.boot.context.ContextIdApplicationContextInitializer
*/
public class SimpleTaskNameResolver implements TaskNameResolver, ApplicationContextAware {
private ApplicationContext context;
private String configuredName;
@Value("${spring.cloud.task.name:}")
public void setConfiguredName(String configuredName) {
this.configuredName = configuredName;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.context = applicationContext;
}
@Override
public String getTaskName() {
if(StringUtils.hasText(configuredName)) {
return configuredName;
}
else {
return context.getId();
}
}
}

View File

@@ -35,8 +35,6 @@ import java.util.Set;
import java.util.TreeSet;
import java.util.UUID;
import javax.sql.DataSource;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
@@ -44,6 +42,7 @@ import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
@@ -54,7 +53,6 @@ import org.springframework.cloud.task.repository.TaskExplorer;
import org.springframework.cloud.task.repository.dao.TaskExecutionDao;
import org.springframework.cloud.task.util.TestVerifierUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.io.ResourceLoader;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
@@ -67,18 +65,12 @@ public class SimpleTaskExplorerTests {
private AnnotationConfigApplicationContext context;
@Autowired(required = false)
private DataSource dataSource;
@Autowired
private TaskExecutionDao dao;
@Autowired
private TaskExplorer taskExplorer;
@Autowired
private ResourceLoader resourceLoader;
private DaoType testType;
@Parameterized.Parameters

View File

@@ -0,0 +1,63 @@
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.repository.support;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.springframework.context.support.GenericApplicationContext;
/**
* @author Michael Minella
*/
public class SimpleTaskNameResolverTests {
@Test
public void testDefault() {
GenericApplicationContext context = new GenericApplicationContext();
SimpleTaskNameResolver taskNameResolver = new SimpleTaskNameResolver();
taskNameResolver.setApplicationContext(context);
assertTrue(taskNameResolver.getTaskName().startsWith("org.springframework.context.support.GenericApplicationContext"));
}
@Test
public void testApplicationName() {
GenericApplicationContext context = new GenericApplicationContext();
context.setId("foo");
SimpleTaskNameResolver taskNameResolver = new SimpleTaskNameResolver();
taskNameResolver.setApplicationContext(context);
assertEquals("foo", taskNameResolver.getTaskName());
}
@Test
public void testExternalConfig() {
GenericApplicationContext context = new GenericApplicationContext();
context.setId("foo");
SimpleTaskNameResolver taskNameResolver = new SimpleTaskNameResolver();
taskNameResolver.setApplicationContext(context);
taskNameResolver.setConfiguredName("bar");
assertEquals("bar", taskNameResolver.getTaskName());
}
}

View File

@@ -16,9 +16,11 @@
package org.springframework.cloud.task.util;
import org.springframework.cloud.task.listener.TaskLifecycleListener;
import org.springframework.cloud.task.listener.TaskLifecycleListener;
import org.springframework.cloud.task.repository.TaskNameResolver;
import org.springframework.cloud.task.repository.TaskRepository;
import org.springframework.cloud.task.repository.dao.MapTaskExecutionDao;
import org.springframework.cloud.task.repository.support.SimpleTaskNameResolver;
import org.springframework.cloud.task.repository.support.SimpleTaskRepository;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -36,8 +38,13 @@ public class TestDefaultConfiguration {
return new SimpleTaskRepository(new MapTaskExecutionDao());
}
@Bean
public TaskNameResolver taskNameResolver() {
return new SimpleTaskNameResolver();
}
@Bean
public TaskLifecycleListener taskHandler(){
return new TaskLifecycleListener(taskRepository());
return new TaskLifecycleListener(taskRepository(), taskNameResolver());
}
}

View File

@@ -45,7 +45,7 @@ public class TaskApplication {
}
public static void main(String[] args) {
SpringApplication.run(TaskApplication.class);
SpringApplication.run(TaskApplication.class, args);
}
/**