From 589ffdc9b750403d6f38ea32183bd034c56fd3ae Mon Sep 17 00:00:00 2001 From: Michael Minella Date: Fri, 3 Jan 2014 15:11:22 -0600 Subject: [PATCH] Consolidated the calculation for non default ExitStatuses and applied that logic on job stops --- .../batch/core/ExitStatus.java | 15 +++++++- .../batch/core/job/AbstractJob.java | 8 ++--- .../core/jsr/job/flow/JsrFlowExecutor.java | 23 +++---------- .../batch/core/jsr/job/flow/JsrFlowJob.java | 34 ++++++++++++++++++- 4 files changed, 56 insertions(+), 24 deletions(-) diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/ExitStatus.java b/spring-batch-core/src/main/java/org/springframework/batch/core/ExitStatus.java index 66225cdef..90a695672 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/ExitStatus.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/ExitStatus.java @@ -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 { 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()); + } } diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/job/AbstractJob.java b/spring-batch-core/src/main/java/org/springframework/batch/core/job/AbstractJob.java index 7468b77a8..10ac5d74e 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/job/AbstractJob.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/job/AbstractJob.java @@ -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) { diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/job/flow/JsrFlowExecutor.java b/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/job/flow/JsrFlowExecutor.java index c4dd26117..830664933 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/job/flow/JsrFlowExecutor.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/job/flow/JsrFlowExecutor.java @@ -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()); - } } diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/job/flow/JsrFlowJob.java b/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/job/flow/JsrFlowJob.java index 5a213559b..76e6537ef 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/job/flow/JsrFlowJob.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/job/flow/JsrFlowJob.java @@ -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; + } + } }