SCT-35 Tasks Support TaskExecutionListener

resolves spring-cloud/spring-cloud-task#35

TaskExecutionListener now supports TaskExecution Params
This commit is contained in:
Glenn Renfro
2016-02-23 10:44:54 -05:00
committed by Michael Minella
parent 91fa6be092
commit 20a56c9581
6 changed files with 331 additions and 2 deletions

View File

@@ -0,0 +1,50 @@
/*
* 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.listener;
import org.springframework.cloud.task.repository.TaskExecution;
import org.springframework.cloud.task.repository.TaskRepository;
/**
* The listener interface for receiving task execution events.
* @author Glenn Renfro
*/
public interface TaskExecutionListener {
/**
* Invoked after the {@link TaskExecution} has been stored in the {@link TaskRepository}.
* @param taskExecution instance containing the information about the current task.
*/
public void onTaskStartup(TaskExecution taskExecution);
/**
* Invoked before the {@link TaskExecution} has been updated in the {@link TaskRepository}
* upon task end.
* @param taskExecution instance containing the information about the current task.
*/
public void onTaskEnd(TaskExecution taskExecution);
/**
* Invoked if an uncaught exception occurs during a task execution. This invocation
* will occur before the {@link TaskExecution} has been updated in the {@link TaskRepository}
* and before the onTaskEnd is called.
* @param taskExecution instance containing the information about the current task.
* @param throwable the uncaught exception that was thrown during task execution.
*/
public void onTaskFailed(TaskExecution taskExecution, Throwable throwable);
}

View File

@@ -19,12 +19,14 @@ import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ExitCodeEvent;
import org.springframework.boot.context.event.ApplicationFailedEvent;
@@ -57,6 +59,9 @@ import org.springframework.util.Assert;
*/
public class TaskLifecycleListener implements ApplicationListener<ApplicationEvent>{
@Autowired(required = false)
Collection<TaskExecutionListener> taskExecutionListeners;
private final static Logger logger = LoggerFactory.getLogger(TaskLifecycleListener.class);
private final TaskRepository taskRepository;
@@ -142,6 +147,11 @@ public class TaskLifecycleListener implements ApplicationListener<ApplicationEve
this.taskExecution.setExitMessage(stackTraceToString(this.applicationFailedEvent.getException()));
}
if(this.taskExecution.getExitCode() != 0){
taskExecution.setExitMessage(invokeOnTaskError(taskExecution,
this.applicationFailedEvent.getException()).getExitMessage());
}
taskExecution.setExitMessage(invokeOnTaskEnd(taskExecution).getExitMessage());
taskRepository.update(taskExecution);
}
else {
@@ -169,5 +179,47 @@ public class TaskLifecycleListener implements ApplicationListener<ApplicationEve
logger.error("Multiple start events have been received. The first one was " +
"recorded.");
}
taskExecution.setExitMessage(invokeOnTaskStartup(taskExecution).getExitMessage());
}
private TaskExecution invokeOnTaskStartup(TaskExecution taskExecution){
TaskExecution listenerTaskExecution = getTaskExecutionCopy(taskExecution);
if (taskExecutionListeners != null) {
for (TaskExecutionListener taskExecutionListener : taskExecutionListeners) {
taskExecutionListener.onTaskStartup(listenerTaskExecution);
}
}
return listenerTaskExecution;
}
private TaskExecution invokeOnTaskEnd(TaskExecution taskExecution){
TaskExecution listenerTaskExecution = getTaskExecutionCopy(taskExecution);
if (taskExecutionListeners != null) {
for (TaskExecutionListener taskExecutionListener : taskExecutionListeners) {
taskExecutionListener.onTaskEnd(listenerTaskExecution);
}
}
return listenerTaskExecution;
}
private TaskExecution invokeOnTaskError(TaskExecution taskExecution, Throwable throwable){
TaskExecution listenerTaskExecution = getTaskExecutionCopy(taskExecution);
if (taskExecutionListeners != null) {
for (TaskExecutionListener taskExecutionListener : taskExecutionListeners) {
taskExecutionListener.onTaskFailed(listenerTaskExecution, throwable);
}
}
return listenerTaskExecution;
}
private TaskExecution getTaskExecutionCopy(TaskExecution taskExecution){
Date startTime = new Date(taskExecution.getStartTime().getTime());
Date endTime = (taskExecution.getEndTime() == null) ?
null : new Date(taskExecution.getEndTime().getTime());
return new TaskExecution(taskExecution.getExecutionId(),
taskExecution.getExitCode(), taskExecution.getTaskName(), startTime,
endTime,taskExecution.getExitMessage(),
Collections.unmodifiableList(taskExecution.getParameters()));
}
}