Consolidated the calculation for non default ExitStatuses and applied that logic on job stops

This commit is contained in:
Michael Minella
2014-01-03 15:11:22 -06:00
parent 62c541ee42
commit 589ffdc9b7
4 changed files with 56 additions and 24 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2013 the original author or authors.
* Copyright 2006-2014 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.
@@ -269,4 +269,17 @@ public class ExitStatus implements Serializable, Comparable<ExitStatus> {
return addExitDescription(message);
}
/**
* @param status the exit code to be evaluated
* @return true if the value matches a known exit code
*/
public static boolean isNonDefaultExitStatus(ExitStatus status) {
return status == null || status.getExitCode() == null ||
status.getExitCode().equals(ExitStatus.COMPLETED.getExitCode()) ||
status.getExitCode().equals(ExitStatus.EXECUTING.getExitCode()) ||
status.getExitCode().equals(ExitStatus.FAILED.getExitCode()) ||
status.getExitCode().equals(ExitStatus.NOOP.getExitCode()) ||
status.getExitCode().equals(ExitStatus.STOPPED.getExitCode()) ||
status.getExitCode().equals(ExitStatus.UNKNOWN.getExitCode());
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2013 the original author or authors.
* Copyright 2006-2014 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.
@@ -322,12 +322,12 @@ InitializingBean {
if (logger.isDebugEnabled()) {
logger.debug("Full exception", e);
}
execution.setExitStatus(getDefaultExitStatusForFailure(e));
execution.setExitStatus(getDefaultExitStatusForFailure(e, execution));
execution.setStatus(BatchStatus.max(BatchStatus.STOPPED, e.getStatus()));
execution.addFailureException(e);
} catch (Throwable t) {
logger.error("Encountered fatal error executing job", t);
execution.setExitStatus(getDefaultExitStatusForFailure(t));
execution.setExitStatus(getDefaultExitStatusForFailure(t, execution));
execution.setStatus(BatchStatus.FAILED);
execution.addFailureException(t);
} finally {
@@ -394,7 +394,7 @@ InitializingBean {
* the cause of the failure
* @return an {@link ExitStatus}
*/
private ExitStatus getDefaultExitStatusForFailure(Throwable ex) {
protected ExitStatus getDefaultExitStatusForFailure(Throwable ex, JobExecution execution) {
ExitStatus exitStatus;
if (ex instanceof JobInterruptedException
|| ex.getCause() instanceof JobInterruptedException) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013 the original author or authors.
* Copyright 2013-2014 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.
@@ -42,8 +42,9 @@ public class JsrFlowExecutor extends JobFlowExecutor {
*/
@Override
public void addExitStatus(String code) {
if((exitStatus != null && isNonDefaultExitStatus(exitStatus.getExitCode())) && !isNonDefaultExitStatus(code)) {
exitStatus = exitStatus.and(new ExitStatus(code));
ExitStatus status = new ExitStatus(code);
if((exitStatus != null && ExitStatus.isNonDefaultExitStatus(exitStatus)) && !ExitStatus.isNonDefaultExitStatus(status)) {
exitStatus = exitStatus.and(status);
}
}
@@ -57,7 +58,7 @@ public class JsrFlowExecutor extends JobFlowExecutor {
execution.setStatus(findBatchStatus(status));
ExitStatus curStatus = execution.getExitStatus();
if(isNonDefaultExitStatus(curStatus.getExitCode())) {
if(ExitStatus.isNonDefaultExitStatus(curStatus)) {
exitStatus = exitStatus.and(new ExitStatus(status.getName()));
execution.setExitStatus(exitStatus);
} else {
@@ -65,18 +66,4 @@ public class JsrFlowExecutor extends JobFlowExecutor {
execution.setExitStatus(exitStatus);
}
}
/**
* @param curStatus the exit code to be evaluated
* @return true if the value matches a known exit code
*/
protected boolean isNonDefaultExitStatus(String curStatus) {
return curStatus == null ||
curStatus.equals(ExitStatus.COMPLETED.getExitCode()) ||
curStatus.equals(ExitStatus.EXECUTING.getExitCode()) ||
curStatus.equals(ExitStatus.FAILED.getExitCode()) ||
curStatus.equals(ExitStatus.NOOP.getExitCode()) ||
curStatus.equals(ExitStatus.STOPPED.getExitCode()) ||
curStatus.equals(ExitStatus.UNKNOWN.getExitCode());
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013 the original author or authors.
* Copyright 2013-2014 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.
@@ -15,13 +15,17 @@
*/
package org.springframework.batch.core.jsr.job.flow;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionException;
import org.springframework.batch.core.JobInterruptedException;
import org.springframework.batch.core.job.AbstractJob;
import org.springframework.batch.core.job.SimpleStepHandler;
import org.springframework.batch.core.job.flow.FlowExecutionException;
import org.springframework.batch.core.job.flow.FlowJob;
import org.springframework.batch.core.job.flow.JobFlowExecutor;
import org.springframework.batch.core.launch.NoSuchJobException;
import org.springframework.batch.core.launch.support.ExitCodeMapper;
/**
* JSR-352 specific extension of the {@link FlowJob}.
@@ -64,4 +68,32 @@ public class JsrFlowJob extends FlowJob {
throw new JobExecutionException("Flow execution ended unexpectedly", e);
}
}
/**
* Default mapping from throwable to {@link ExitStatus}.
*
* @param ex the cause of the failure
* @return an {@link ExitStatus}
*/
@Override
protected ExitStatus getDefaultExitStatusForFailure(Throwable ex, JobExecution execution) {
if(!ExitStatus.isNonDefaultExitStatus(execution.getExitStatus())) {
return execution.getExitStatus();
} else {
ExitStatus exitStatus;
if (ex instanceof JobInterruptedException
|| ex.getCause() instanceof JobInterruptedException) {
exitStatus = ExitStatus.STOPPED
.addExitDescription(JobInterruptedException.class.getName());
} else if (ex instanceof NoSuchJobException
|| ex.getCause() instanceof NoSuchJobException) {
exitStatus = new ExitStatus(ExitCodeMapper.NO_SUCH_JOB, ex
.getClass().getName());
} else {
exitStatus = ExitStatus.FAILED.addExitDescription(ex);
}
return exitStatus;
}
}
}