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