This commit is contained in:
Michael Minella
2018-10-31 22:11:01 -05:00
parent d2bc2530cc
commit 90c88c52e6
29 changed files with 197 additions and 205 deletions

View File

@@ -24,7 +24,6 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.cloud.task.repository.TaskRepository;
import org.springframework.context.annotation.Import;
/**
* <p>
@@ -62,6 +61,5 @@ import org.springframework.context.annotation.Import;
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({ })
public @interface EnableTask {
}

View File

@@ -18,8 +18,6 @@ package org.springframework.cloud.task.configuration;
import java.util.Arrays;
import java.util.Collection;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct;
import javax.sql.DataSource;
@@ -125,7 +123,7 @@ public class SimpleTaskAutoConfiguration {
* Determines the {@link TaskConfigurer} to use.
*/
@PostConstruct
protected void initialize() throws Exception {
protected void initialize() {
if (initialized) {
return;
}
@@ -177,7 +175,7 @@ public class SimpleTaskAutoConfiguration {
int configurers = this.context.getBeanNamesForType(TaskConfigurer.class).length;
// retrieve the count of dataSources (without instantiating them) excluding DataSource proxy beans
long dataSources = Arrays.stream(this.context.getBeanNamesForType(DataSource.class))
.filter((name -> !ScopedProxyUtils.isScopedTarget(name))).collect(Collectors.counting());
.filter((name -> !ScopedProxyUtils.isScopedTarget(name))).count();
if(configurers == 0 && dataSources > 1) {
throw new IllegalStateException("To use the default TaskConfigurer the context must contain no more than" +

View File

@@ -206,7 +206,7 @@ public class TaskLifecycleListener implements ApplicationListener<ApplicationEve
}
else if (this.listenerFailed || this.applicationFailedEvent != null) {
Throwable exception = this.listenerException;
if (exception != null && exception instanceof TaskExecutionException) {
if (exception instanceof TaskExecutionException) {
TaskExecutionException taskExecutionException = (TaskExecutionException) exception;
if (taskExecutionException.getCause() instanceof InvocationTargetException) {
InvocationTargetException invocationTargetException = (InvocationTargetException) taskExecutionException
@@ -217,7 +217,7 @@ public class TaskLifecycleListener implements ApplicationListener<ApplicationEve
}
}
if (exception != null && exception instanceof ExitCodeGenerator) {
if (exception instanceof ExitCodeGenerator) {
exitCode = ((ExitCodeGenerator) exception).getExitCode();
}
else {
@@ -229,46 +229,54 @@ public class TaskLifecycleListener implements ApplicationListener<ApplicationEve
}
private void doTaskStart() {
try {
if(!this.started) {
this.taskExecutionListeners = new ArrayList<>();
this.taskListenerExecutorObjectFactory.getObject();
if(!CollectionUtils.isEmpty(this.taskExecutionListenersFromContext)) {
this.taskExecutionListeners.addAll(this.taskExecutionListenersFromContext);
}
this.taskExecutionListeners.add(this.taskListenerExecutorObjectFactory.getObject());
if(!this.started) {
this.taskExecutionListeners = new ArrayList<>();
this.taskListenerExecutorObjectFactory.getObject();
if(!CollectionUtils.isEmpty(this.taskExecutionListenersFromContext)) {
this.taskExecutionListeners.addAll(this.taskExecutionListenersFromContext);
}
this.taskExecutionListeners.add(this.taskListenerExecutorObjectFactory.getObject());
List<String> args = new ArrayList<>(0);
List<String> args = new ArrayList<>(0);
if(this.applicationArguments != null) {
args = Arrays.asList(this.applicationArguments.getSourceArgs());
}
if(this.taskProperties.getExecutionid() != null) {
TaskExecution taskExecution = this.taskExplorer.getTaskExecution(this.taskProperties.getExecutionid());
Assert.notNull(taskExecution, String.format("Invalid TaskExecution, ID %s not found", this.taskProperties.getExecutionid()));
Assert.isNull(taskExecution.getEndTime(), String.format(
"Invalid TaskExecution, ID %s task is already complete", this.taskProperties.getExecutionid()));
this.taskExecution = this.taskRepository.startTaskExecution(this.taskProperties.getExecutionid(),
this.taskNameResolver.getTaskName(), new Date(), args,
this.taskProperties.getExternalExecutionId(),
this.taskProperties.getParentExecutionId());
if(this.applicationArguments != null) {
args = Arrays.asList(this.applicationArguments.getSourceArgs());
}
if(this.taskProperties.getExecutionid() != null) {
TaskExecution taskExecution = this.taskExplorer.getTaskExecution(this.taskProperties.getExecutionid());
Assert.notNull(taskExecution, String.format("Invalid TaskExecution, ID %s not found", this.taskProperties.getExecutionid()));
Assert.isNull(taskExecution.getEndTime(), String.format(
"Invalid TaskExecution, ID %s task is already complete", this.taskProperties.getExecutionid()));
this.taskExecution = this.taskRepository.startTaskExecution(this.taskProperties.getExecutionid(),
this.taskNameResolver.getTaskName(), new Date(), args,
this.taskProperties.getExternalExecutionId(),
this.taskProperties.getParentExecutionId());
}
else {
TaskExecution taskExecution = new TaskExecution();
taskExecution.setTaskName(this.taskNameResolver.getTaskName());
taskExecution.setStartTime(new Date());
taskExecution.setArguments(args);
taskExecution.setExternalExecutionId(this.taskProperties.getExternalExecutionId());
taskExecution.setParentExecutionId(this.taskProperties.getParentExecutionId());
this.taskExecution = this.taskRepository.createTaskExecution(
taskExecution);
}
}
else {
TaskExecution taskExecution = new TaskExecution();
taskExecution.setTaskName(this.taskNameResolver.getTaskName());
taskExecution.setStartTime(new Date());
taskExecution.setArguments(args);
taskExecution.setExternalExecutionId(this.taskProperties.getExternalExecutionId());
taskExecution.setParentExecutionId(this.taskProperties.getParentExecutionId());
this.taskExecution = this.taskRepository.createTaskExecution(
taskExecution);
logger.error("Multiple start events have been received. The first one was " +
"recorded.");
}
setExitMessage(invokeOnTaskStartup(this.taskExecution));
}
else {
logger.error("Multiple start events have been received. The first one was " +
"recorded.");
catch (Throwable t) {
// This scenario will result in a context that was not startup.
this.doTaskEnd();
throw t;
}
setExitMessage(invokeOnTaskStartup(this.taskExecution));
}
private TaskExecution invokeOnTaskStartup(TaskExecution taskExecution){
@@ -388,7 +396,6 @@ public class TaskLifecycleListener implements ApplicationListener<ApplicationEve
@Override
public void destroy() throws Exception {
this.doTaskEnd();
}
}

View File

@@ -30,12 +30,9 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.aop.framework.autoproxy.AutoProxyUtils;
import org.springframework.aop.scope.ScopedObject;
import org.springframework.aop.scope.ScopedProxyUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.cloud.task.listener.TaskExecutionListener;
import org.springframework.cloud.task.listener.annotation.AfterTask;
import org.springframework.cloud.task.listener.annotation.BeforeTask;
import org.springframework.cloud.task.listener.annotation.FailedTask;
@@ -55,7 +52,7 @@ public class TaskListenerExecutorObjectFactory implements ObjectFactory<TaskExec
private final static Log logger = LogFactory.getLog(TaskListenerExecutor.class);
private final Set<Class<?>> nonAnnotatedClasses =
Collections.newSetFromMap(new ConcurrentHashMap<Class<?>, Boolean>());
Collections.newSetFromMap(new ConcurrentHashMap<>());
private ConfigurableApplicationContext context;
@@ -153,12 +150,7 @@ public class TaskListenerExecutorObjectFactory implements ObjectFactory<TaskExec
private static class MethodGetter<T extends Annotation> {
public Map<Method, T> getMethods(final Class<?> type, final Class<T> annotationClass){
return MethodIntrospector.selectMethods(type,
new MethodIntrospector.MetadataLookup<T>() {
@Override
public T inspect(Method method) {
return AnnotationUtils.findAnnotation(method, annotationClass);
}
});
(MethodIntrospector.MetadataLookup<T>) method -> AnnotationUtils.findAnnotation(method, annotationClass));
}
}
}

View File

@@ -38,7 +38,7 @@ import static org.junit.Assert.assertNotNull;
public class SimpleSingleTaskAutoConfigurationTests {
@Test
public void testConfiguration() throws Exception {
public void testConfiguration() {
ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(

View File

@@ -39,7 +39,7 @@ import static org.junit.Assert.assertNotNull;
public class SimpleSingleTaskAutoConfigurationWithDataSourceTests {
@Test
public void testConfiguration() throws Exception {
public void testConfiguration() {
ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(

View File

@@ -68,7 +68,7 @@ public class SimpleTaskAutoConfigurationTests {
}
@Test
public void testRepository() throws Exception {
public void testRepository() {
ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(
PropertyPlaceholderAutoConfiguration.class,
@@ -84,7 +84,7 @@ public class SimpleTaskAutoConfigurationTests {
}
@Test
public void testAutoConfigurationDisabled() throws Exception {
public void testAutoConfigurationDisabled() {
ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(
PropertyPlaceholderAutoConfiguration.class,
@@ -102,7 +102,7 @@ public class SimpleTaskAutoConfigurationTests {
}
@Test
public void testRepositoryInitialized() throws Exception {
public void testRepositoryInitialized() {
ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(EmbeddedDataSourceConfiguration.class,
PropertyPlaceholderAutoConfiguration.class,
@@ -115,7 +115,7 @@ public class SimpleTaskAutoConfigurationTests {
}
@Test
public void testRepositoryNotInitialized() throws Exception {
public void testRepositoryNotInitialized() {
ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(EmbeddedDataSourceConfiguration.class,
PropertyPlaceholderAutoConfiguration.class,
@@ -221,12 +221,12 @@ public class SimpleTaskAutoConfigurationTests {
@Bean
public DataSource dataSource() {
return mock(DataSource.class);
};
}
@Bean
public DataSource dataSource2() {
return mock(DataSource.class);
};
}
}

View File

@@ -66,10 +66,9 @@ public class TaskCoreTests {
@Test
public void successfulTaskTest() {
this.applicationContext = SpringApplication.run( TaskConfiguration.class,
new String[] {
"--spring.cloud.task.closecontext.enable=false",
"--spring.cloud.task.name=" + TASK_NAME,
"--spring.main.web-environment=false" });
"--spring.cloud.task.closecontext.enable=false",
"--spring.cloud.task.name=" + TASK_NAME,
"--spring.main.web-environment=false");
String output = this.outputCapture.toString();
assertTrue("Test results do not show create task message: " + output,
@@ -86,10 +85,9 @@ public class TaskCoreTests {
@Test
public void successfulTaskTestWithAnnotation() {
this.applicationContext = SpringApplication.run( TaskConfigurationWithAnotation.class,
new String[] {
"--spring.cloud.task.closecontext.enable=false",
"--spring.cloud.task.name=" + TASK_NAME,
"--spring.main.web-environment=false" });
"--spring.cloud.task.closecontext.enable=false",
"--spring.cloud.task.name=" + TASK_NAME,
"--spring.main.web-environment=false");
String output = this.outputCapture.toString();
assertTrue("Test results do not show create task message: " + output,
@@ -105,10 +103,9 @@ public class TaskCoreTests {
boolean exceptionFired = false;
try {
this.applicationContext = SpringApplication.run( TaskExceptionConfiguration.class,
new String[] {
"--spring.cloud.task.closecontext.enable=false",
"--spring.cloud.task.name=" + TASK_NAME,
"--spring.main.web-environment=false" });
"--spring.cloud.task.closecontext.enable=false",
"--spring.cloud.task.name=" + TASK_NAME,
"--spring.main.web-environment=false");
}
catch (IllegalStateException exception) {
exceptionFired = true;
@@ -132,12 +129,11 @@ public class TaskCoreTests {
public void invalidExecutionId() {
boolean exceptionFired = false;
try {
applicationContext = this.applicationContext = SpringApplication.run(
TaskExceptionConfiguration.class, new String[]{
"--spring.cloud.task.closecontext.enable=false",
this.applicationContext = SpringApplication.run(
TaskExceptionConfiguration.class, "--spring.cloud.task.closecontext.enable=false",
"--spring.cloud.task.name=" + TASK_NAME,
"--spring.main.web-environment=false",
"--spring.cloud.task.executionid=55"});
"--spring.cloud.task.executionid=55");
}
catch (ApplicationContextException exception) {
exceptionFired = true;

View File

@@ -50,7 +50,7 @@ public class TaskRepositoryInitializerDefaultTaskConfigurerTests {
private DataSource dataSource;
@Test
public void testTablesCreated() throws Exception {
public void testTablesCreated() {
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
List<Map<String, Object>> rows= jdbcTemplate.queryForList("SHOW TABLES");
assertThat(rows.size()).isEqualTo(4);

View File

@@ -53,7 +53,7 @@ public class TaskRepositoryInitializerNoDataSourceTaskConfigurerTests {
private DataSource dataSource;
@Test
public void testNoTablesCreated() throws Exception {
public void testNoTablesCreated() {
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
List<Map<String, Object>> rows= jdbcTemplate.queryForList("SHOW TABLES");
assertThat(rows.size()).isEqualTo(0);

View File

@@ -22,6 +22,7 @@ import java.util.Date;
import org.junit.After;
import org.junit.Test;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.context.event.ApplicationFailedEvent;
@@ -53,9 +54,9 @@ public class TaskExecutionListenerTests {
private static final String EXCEPTION_MESSAGE = "This was expected";
public static boolean beforeTaskDidFireOnError = false;
public static boolean endTaskDidFireOnError = false;
public static boolean failedTaskDidFireOnError = false;
private static boolean beforeTaskDidFireOnError = false;
private static boolean endTaskDidFireOnError = false;
private static boolean failedTaskDidFireOnError = false;
@BeforeTask
public void setup() {
@@ -81,8 +82,8 @@ public class TaskExecutionListenerTests {
DefaultTaskListenerConfiguration.TestTaskExecutionListener taskExecutionListener =
context.getBean(DefaultTaskListenerConfiguration.TestTaskExecutionListener.class);
TaskExecution taskExecution = new TaskExecution(0, null, "wombat",
new Date(), new Date(), null, new ArrayList<String>(), null, null);
verifyListenerResults(true, false, false, taskExecution,taskExecutionListener);
new Date(), new Date(), null, new ArrayList<>(), null, null);
verifyListenerResults(false, false, taskExecution,taskExecutionListener);
}
/**
@@ -130,8 +131,8 @@ public class TaskExecutionListenerTests {
context.getBean(AfterTaskErrorAnnotationConfiguration.AnnotatedTaskListener.class);
context.publishEvent(new ApplicationReadyEvent(new SpringApplication(), new String[0], context));
assertEquals(true,taskExecutionListener.isTaskStartup());
assertEquals(true,taskExecutionListener.isTaskEnd());
assertTrue(taskExecutionListener.isTaskStartup());
assertTrue(taskExecutionListener.isTaskEnd());
assertEquals(TestListener.END_MESSAGE, taskExecutionListener.getTaskExecution().getExitMessage());
assertTrue(taskExecutionListener.getTaskExecution().getErrorMessage().contains("Failed to process @BeforeTask or @AfterTask annotation because: AfterTaskFailure"));
assertNull(taskExecutionListener.getThrowable());
@@ -149,8 +150,8 @@ public class TaskExecutionListenerTests {
context.publishEvent(new ApplicationReadyEvent(new SpringApplication(), new String[0], context));
TaskExecution taskExecution = new TaskExecution(0, 0, "wombat",
new Date(), new Date(), null, new ArrayList<String>(), null, null);
verifyListenerResults(true, true, false, taskExecution,taskExecutionListener);
new Date(), new Date(), null, new ArrayList<>(), null, null);
verifyListenerResults(true, false, taskExecution,taskExecutionListener);
}
/**
@@ -168,8 +169,8 @@ public class TaskExecutionListenerTests {
context.publishEvent(new ApplicationReadyEvent(application, new String[0], context));
TaskExecution taskExecution = new TaskExecution(0, 1, "wombat", new Date(),
new Date(), null, new ArrayList<String>(), null, null);
verifyListenerResults(true, true, true, taskExecution,taskExecutionListener);
new Date(), null, new ArrayList<>(), null, null);
verifyListenerResults(true, true, taskExecution,taskExecutionListener);
}
/**
@@ -177,13 +178,13 @@ public class TaskExecutionListenerTests {
* method is called.
*/
@Test
public void testAnnotationCreate() throws Exception {
public void testAnnotationCreate() {
setupContextForAnnotatedListener();
DefaultAnnotationConfiguration.AnnotatedTaskListener annotatedListener =
context.getBean(DefaultAnnotationConfiguration.AnnotatedTaskListener.class);
TaskExecution taskExecution = new TaskExecution(0, null, "wombat",
new Date(), new Date(), null, new ArrayList<String>(), null, null);
verifyListenerResults(true, false, false, taskExecution,annotatedListener);
new Date(), new Date(), null, new ArrayList<>(), null, null);
verifyListenerResults(false, false, taskExecution,annotatedListener);
}
/**
@@ -198,8 +199,8 @@ public class TaskExecutionListenerTests {
context.publishEvent(new ApplicationReadyEvent(new SpringApplication(), new String[0], context));
TaskExecution taskExecution = new TaskExecution(0, 0, "wombat",
new Date(), new Date(), null, new ArrayList<String>(), null, null);
verifyListenerResults(true, true, false, taskExecution,annotatedListener);
new Date(), new Date(), null, new ArrayList<>(), null, null);
verifyListenerResults(true, false, taskExecution,annotatedListener);
}
/**
@@ -217,14 +218,14 @@ public class TaskExecutionListenerTests {
context.publishEvent(new ApplicationReadyEvent(application, new String[0], context));
TaskExecution taskExecution = new TaskExecution(0, 1, "wombat", new Date(),
new Date(), null, new ArrayList<String>(), null, null);
verifyListenerResults(true, true, true, taskExecution,annotatedListener);
new Date(), null, new ArrayList<>(), null, null);
verifyListenerResults(true, true, taskExecution,annotatedListener);
}
private void verifyListenerResults (boolean isTaskStartup, boolean isTaskEnd,
boolean isTaskFailed, TaskExecution taskExecution,
TestListener actualListener){
assertEquals(isTaskStartup,actualListener.isTaskStartup());
private void verifyListenerResults(boolean isTaskEnd,
boolean isTaskFailed, TaskExecution taskExecution,
TestListener actualListener){
assertTrue(actualListener.isTaskStartup());
assertEquals(isTaskEnd,actualListener.isTaskEnd());
assertEquals(isTaskFailed,actualListener.isTaskFailed());
if(isTaskFailed){
@@ -323,6 +324,11 @@ public class TaskExecutionListenerTests {
return new AnnotatedTaskListener();
}
@Bean
public CommandLineRunner commandLineRunner() {
return args -> System.out.println("I was run");
}
public static class AnnotatedTaskListener {
@BeforeTask

View File

@@ -19,7 +19,6 @@ package org.springframework.cloud.task.repository.support;
import java.util.Collections;
import java.util.Date;
import java.util.UUID;
import javax.sql.DataSource;
import org.junit.Test;
@@ -30,7 +29,6 @@ import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoCon
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
import org.springframework.cloud.task.configuration.SimpleTaskAutoConfiguration;
import org.springframework.cloud.task.repository.TaskExecution;
import org.springframework.cloud.task.repository.TaskExplorer;
import org.springframework.cloud.task.repository.TaskRepository;
import org.springframework.cloud.task.util.TaskExecutionCreator;
import org.springframework.cloud.task.util.TestDBUtils;
@@ -59,9 +57,6 @@ public class SimpleTaskRepositoryJdbcTests {
@Autowired
private DataSource dataSource;
@Autowired
private TaskExplorer taskExplorer;
@Test
@DirtiesContext
public void testCreateEmptyExecution() {

View File

@@ -57,7 +57,7 @@ public class TaskDatabaseInitializerTests {
}
@Test
public void testDefaultContext() throws Exception {
public void testDefaultContext() {
this.context = new AnnotationConfigApplicationContext();
this.context.register( TestConfiguration.class,
EmbeddedDataSourceConfiguration.class,
@@ -68,7 +68,7 @@ public class TaskDatabaseInitializerTests {
}
@Test
public void testNoDatabase() throws Exception {
public void testNoDatabase() {
this.context = new AnnotationConfigApplicationContext(EmptyConfiguration.class);
SimpleTaskRepository repository = new SimpleTaskRepository(new TaskExecutionDaoFactoryBean());
assertThat(repository.getTaskExecutionDao(), instanceOf(MapTaskExecutionDao.class));
@@ -77,7 +77,7 @@ public class TaskDatabaseInitializerTests {
}
@Test
public void testNoTaskConfiguration() throws Exception {
public void testNoTaskConfiguration() {
this.context = new AnnotationConfigApplicationContext();
this.context.register(EmptyConfiguration.class,
EmbeddedDataSourceConfiguration.class,
@@ -87,7 +87,7 @@ public class TaskDatabaseInitializerTests {
}
@Test(expected = BeanCreationException.class)
public void testMultipleDataSourcesContext() throws Exception {
public void testMultipleDataSourcesContext() {
this.context = new AnnotationConfigApplicationContext();
this.context.register( SimpleTaskAutoConfiguration.class,
EmbeddedDataSourceConfiguration.class,

View File

@@ -66,7 +66,7 @@ public class TestDefaultConfiguration implements InitializingBean {
}
@Bean
public TaskExplorer taskExplorer() throws Exception {
public TaskExplorer taskExplorer() {
return new SimpleTaskExplorer(this.factoryBean);
}