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:
committed by
Glenn Renfro
parent
a2674b50bd
commit
1c3bb94bbd
@@ -27,7 +27,9 @@ import org.apache.commons.logging.LogFactory;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
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.TaskRepository;
|
||||||
|
import org.springframework.cloud.task.repository.support.SimpleTaskNameResolver;
|
||||||
import org.springframework.cloud.task.repository.support.TaskDatabaseInitializer;
|
import org.springframework.cloud.task.repository.support.TaskDatabaseInitializer;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
@@ -76,7 +78,7 @@ public class SimpleTaskConfiguration {
|
|||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public TaskLifecycleListener taskLifecycleListener() {
|
public TaskLifecycleListener taskLifecycleListener() {
|
||||||
return new TaskLifecycleListener(taskRepository());
|
return new TaskLifecycleListener(taskRepository(), taskNameResolver());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@@ -84,6 +86,11 @@ public class SimpleTaskConfiguration {
|
|||||||
return this.transactionManager;
|
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
|
* 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.
|
* sensible values as long as a unique DataSource is available.
|
||||||
|
|||||||
@@ -33,9 +33,9 @@ public interface TaskConfigurer {
|
|||||||
*
|
*
|
||||||
* @return A TaskRepository
|
* @return A TaskRepository
|
||||||
*/
|
*/
|
||||||
public TaskRepository getTaskRepository();
|
TaskRepository getTaskRepository();
|
||||||
|
|
||||||
public PlatformTransactionManager getTransactionManager();
|
PlatformTransactionManager getTransactionManager();
|
||||||
|
|
||||||
public TaskExplorer getTaskExplorer();
|
TaskExplorer getTaskExplorer();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,16 +23,15 @@ import java.util.UUID;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import org.springframework.beans.BeansException;
|
|
||||||
import org.springframework.boot.context.event.ApplicationFailedEvent;
|
import org.springframework.boot.context.event.ApplicationFailedEvent;
|
||||||
import org.springframework.cloud.task.repository.TaskExecution;
|
import org.springframework.cloud.task.repository.TaskExecution;
|
||||||
|
import org.springframework.cloud.task.repository.TaskNameResolver;
|
||||||
import org.springframework.cloud.task.repository.TaskRepository;
|
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.ApplicationEvent;
|
||||||
import org.springframework.context.ApplicationListener;
|
import org.springframework.context.ApplicationListener;
|
||||||
import org.springframework.context.event.ContextClosedEvent;
|
import org.springframework.context.event.ContextClosedEvent;
|
||||||
import org.springframework.context.event.ContextRefreshedEvent;
|
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
|
* 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
|
* @author Michael Minella
|
||||||
*/
|
*/
|
||||||
public class TaskLifecycleListener implements ApplicationListener<ApplicationEvent>,
|
public class TaskLifecycleListener implements ApplicationListener<ApplicationEvent>{
|
||||||
ApplicationContextAware {
|
|
||||||
|
|
||||||
private final static Logger logger = LoggerFactory.getLogger(TaskLifecycleListener.class);
|
private final static Logger logger = LoggerFactory.getLogger(TaskLifecycleListener.class);
|
||||||
|
|
||||||
@@ -63,13 +61,18 @@ public class TaskLifecycleListener implements ApplicationListener<ApplicationEve
|
|||||||
|
|
||||||
private boolean started = false;
|
private boolean started = false;
|
||||||
|
|
||||||
private ApplicationContext applicationContext;
|
private TaskNameResolver taskNameResolver;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param taskRepository The repository to record executions in.
|
* @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.taskRepository = taskRepository;
|
||||||
|
this.taskNameResolver = taskNameResolver;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -143,7 +146,7 @@ public class TaskLifecycleListener implements ApplicationListener<ApplicationEve
|
|||||||
String executionId = UUID.randomUUID().toString();
|
String executionId = UUID.randomUUID().toString();
|
||||||
this.taskExecution = new TaskExecution();
|
this.taskExecution = new TaskExecution();
|
||||||
|
|
||||||
this.taskExecution.setTaskName(applicationContext.getId());
|
this.taskExecution.setTaskName(taskNameResolver.getTaskName());
|
||||||
this.taskExecution.setStartTime(new Date());
|
this.taskExecution.setStartTime(new Date());
|
||||||
this.taskExecution.setExecutionId(executionId);
|
this.taskExecution.setExecutionId(executionId);
|
||||||
this.taskRepository.createTaskExecution(this.taskExecution);
|
this.taskRepository.createTaskExecution(this.taskExecution);
|
||||||
@@ -161,10 +164,4 @@ public class TaskLifecycleListener implements ApplicationListener<ApplicationEve
|
|||||||
TaskExecution getTaskExecution() {
|
TaskExecution getTaskExecution() {
|
||||||
return this.taskExecution;
|
return this.taskExecution;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setApplicationContext(ApplicationContext applicationContext)
|
|
||||||
throws BeansException {
|
|
||||||
this.applicationContext = applicationContext;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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();
|
||||||
|
}
|
||||||
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -35,8 +35,6 @@ import java.util.Set;
|
|||||||
import java.util.TreeSet;
|
import java.util.TreeSet;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
|
||||||
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
@@ -44,6 +42,7 @@ import org.junit.Test;
|
|||||||
import org.junit.rules.ExpectedException;
|
import org.junit.rules.ExpectedException;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.junit.runners.Parameterized;
|
import org.junit.runners.Parameterized;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
|
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
|
||||||
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
|
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.repository.dao.TaskExecutionDao;
|
||||||
import org.springframework.cloud.task.util.TestVerifierUtils;
|
import org.springframework.cloud.task.util.TestVerifierUtils;
|
||||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
import org.springframework.core.io.ResourceLoader;
|
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.domain.PageRequest;
|
import org.springframework.data.domain.PageRequest;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
@@ -67,18 +65,12 @@ public class SimpleTaskExplorerTests {
|
|||||||
|
|
||||||
private AnnotationConfigApplicationContext context;
|
private AnnotationConfigApplicationContext context;
|
||||||
|
|
||||||
@Autowired(required = false)
|
|
||||||
private DataSource dataSource;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private TaskExecutionDao dao;
|
private TaskExecutionDao dao;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private TaskExplorer taskExplorer;
|
private TaskExplorer taskExplorer;
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private ResourceLoader resourceLoader;
|
|
||||||
|
|
||||||
private DaoType testType;
|
private DaoType testType;
|
||||||
|
|
||||||
@Parameterized.Parameters
|
@Parameterized.Parameters
|
||||||
|
|||||||
@@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -16,9 +16,11 @@
|
|||||||
|
|
||||||
package org.springframework.cloud.task.util;
|
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.TaskRepository;
|
||||||
import org.springframework.cloud.task.repository.dao.MapTaskExecutionDao;
|
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.cloud.task.repository.support.SimpleTaskRepository;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
@@ -36,8 +38,13 @@ public class TestDefaultConfiguration {
|
|||||||
return new SimpleTaskRepository(new MapTaskExecutionDao());
|
return new SimpleTaskRepository(new MapTaskExecutionDao());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public TaskNameResolver taskNameResolver() {
|
||||||
|
return new SimpleTaskNameResolver();
|
||||||
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public TaskLifecycleListener taskHandler(){
|
public TaskLifecycleListener taskHandler(){
|
||||||
return new TaskLifecycleListener(taskRepository());
|
return new TaskLifecycleListener(taskRepository(), taskNameResolver());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ public class TaskApplication {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(TaskApplication.class);
|
SpringApplication.run(TaskApplication.class, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user