From 3f316a01708cb59f8c73c365b5a242217c25cb13 Mon Sep 17 00:00:00 2001 From: Glenn Renfro Date: Thu, 5 Oct 2017 17:22:05 -0400 Subject: [PATCH] SCT now handles exceptions thrown by execution listeners resolves #341 --- .../task/listener/TaskLifecycleListener.java | 146 ++++++++++---- .../annotation/TaskListenerExecutor.java | 8 +- .../listener/TaskExecutionListenerTests.java | 184 +++++++++++++++++- .../src/main/asciidoc/features.adoc | 45 +++++ 4 files changed, 341 insertions(+), 42 deletions(-) diff --git a/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/listener/TaskLifecycleListener.java b/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/listener/TaskLifecycleListener.java index ec13993f..db7971ad 100644 --- a/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/listener/TaskLifecycleListener.java +++ b/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/listener/TaskLifecycleListener.java @@ -17,6 +17,7 @@ package org.springframework.cloud.task.listener; import java.io.PrintWriter; import java.io.StringWriter; +import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -31,6 +32,7 @@ import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ExitCodeEvent; +import org.springframework.boot.ExitCodeGenerator; import org.springframework.boot.context.event.ApplicationFailedEvent; import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.cloud.task.configuration.TaskProperties; @@ -44,6 +46,7 @@ import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.SmartLifecycle; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.util.Assert; +import org.springframework.util.StringUtils; /** * Monitors the lifecycle of a task. This listener will record both the start and end of @@ -63,6 +66,7 @@ import org.springframework.util.Assert; * property spring.cloud.task.closecontext.enable (defaults to true). * * @author Michael Minella + * @author Glenn Renfro */ public class TaskLifecycleListener implements ApplicationListener, SmartLifecycle, DisposableBean { @@ -86,6 +90,10 @@ public class TaskLifecycleListener implements ApplicationListener args = new ArrayList<>(0); if(this.applicationArguments != null) { args = Arrays.asList(this.applicationArguments.getSourceArgs()); } - if(taskProperties.getExecutionid() != null) { - TaskExecution taskExecution = taskExplorer.getTaskExecution(taskProperties.getExecutionid()); - Assert.notNull(taskExecution, String.format("Invalid TaskExecution, ID %s not found", taskProperties.getExecutionid())); + 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", taskProperties.getExecutionid())); - this.taskExecution = this.taskRepository.startTaskExecution(taskProperties.getExecutionid(), + "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, - taskProperties.getExternalExecutionId(), - taskProperties.getParentExecutionId()); + 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(taskProperties.getExternalExecutionId()); - taskExecution.setParentExecutionId(taskProperties.getParentExecutionId()); + taskExecution.setExternalExecutionId(this.taskProperties.getExternalExecutionId()); + taskExecution.setParentExecutionId(this.taskProperties.getParentExecutionId()); this.taskExecution = this.taskRepository.createTaskExecution( taskExecution); } @@ -219,14 +253,23 @@ public class TaskLifecycleListener implements ApplicationListener