[BATCH-432] Polishing up exception declarations

This commit is contained in:
nebhale
2008-03-07 10:40:25 +00:00
parent 2154a70069
commit a60fc1e621
7 changed files with 75 additions and 172 deletions

View File

@@ -22,6 +22,7 @@ import java.util.List;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionException;
import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.JobInterruptedException;
import org.springframework.batch.core.JobListener;
@@ -33,9 +34,8 @@ import org.springframework.batch.io.exception.InfrastructureException;
import org.springframework.batch.repeat.ExitStatus;
/**
* Simple implementation of (@link Job} interface providing the ability to run a
* {@link JobExecution}. Sequentially executes a job by iterating through its
* list of steps.
* Simple implementation of (@link Job} interface providing the ability to run a {@link JobExecution}. Sequentially
* executes a job by iterating through its list of steps.
*
* @author Lucas Ward
* @author Dave Syer
@@ -47,8 +47,8 @@ public class SimpleJob extends AbstractJob {
private CompositeJobListener listener = new CompositeJobListener();
/**
* Public setter for injecting {@link JobListener}s. They will all be given
* the {@link JobListener} callbacks at the appropriate point in the job.
* Public setter for injecting {@link JobListener}s. They will all be given the {@link JobListener} callbacks at
* the appropriate point in the job.
*
* @param listeners the listeners to set.
*/
@@ -57,9 +57,10 @@ public class SimpleJob extends AbstractJob {
this.listener.register(listeners[i]);
}
}
/**
* Register a single listener for the {@link JobListener} callbacks.
*
* @param listener a {@link JobListener}
*/
public void registerListener(JobListener listener) {
@@ -67,12 +68,11 @@ public class SimpleJob extends AbstractJob {
}
/**
* Run the specified job by looping through the steps and delegating to the
* {@link Step}.
* Run the specified job by looping through the steps and delegating to the {@link Step}.
*
* @see org.springframework.batch.core.Job#execute(org.springframework.batch.core.JobExecution)
*/
public void execute(JobExecution execution) throws InfrastructureException {
public void execute(JobExecution execution) throws JobExecutionException {
JobInstance jobInstance = execution.getJobInstance();
@@ -112,27 +112,22 @@ public class SimpleJob extends AbstractJob {
listener.afterJob(execution);
}
catch (JobInterruptedException e) {
} catch (JobInterruptedException e) {
execution.setStatus(BatchStatus.STOPPED);
rethrow(e);
}
catch (Throwable t) {
} catch (Throwable t) {
execution.setStatus(BatchStatus.FAILED);
rethrow(t);
}
finally {
} finally {
ExitStatus status = ExitStatus.FAILED;
if (startedCount == 0) {
if (steps.size() > 0) {
status = ExitStatus.NOOP
.addExitDescription("All steps already completed. No processing was done.");
}
else {
.addExitDescription("All steps already completed. No processing was done.");
} else {
status = ExitStatus.NOOP.addExitDescription("No steps configured for this job.");
}
}
else if (currentStepExecution != null) {
} else if (currentStepExecution != null) {
status = currentStepExecution.getExitStatus();
}
@@ -149,25 +144,24 @@ public class SimpleJob extends AbstractJob {
}
/*
* Given a step and configuration, return true if the step should start,
* false if it should not, and throw an exception if the job should finish.
* Given a step and configuration, return true if the step should start, false if it should not, and throw an
* exception if the job should finish.
*/
private boolean shouldStart(JobInstance jobInstance, Step step) {
private boolean shouldStart(JobInstance jobInstance, Step step) throws JobExecutionException {
BatchStatus stepStatus;
// if the last execution is null, the step has never been executed.
StepExecution lastStepExecution = jobRepository.getLastStepExecution(jobInstance, step);
if (lastStepExecution == null) {
stepStatus = BatchStatus.STARTING;
}
else {
} else {
stepStatus = lastStepExecution.getStatus();
}
if (stepStatus == BatchStatus.UNKNOWN) {
throw new InfrastructureException("Cannot restart step from UNKNOWN status. "
+ "The last execution ended with a failure that could not be rolled back, "
+ "so it may be dangerous to proceed. " + "Manual intervention is probably necessary.");
throw new JobExecutionException("Cannot restart step from UNKNOWN status. "
+ "The last execution ended with a failure that could not be rolled back, "
+ "so it may be dangerous to proceed. " + "Manual intervention is probably necessary.");
}
if (stepStatus == BatchStatus.COMPLETED && step.isAllowStartIfComplete() == false) {
@@ -179,11 +173,10 @@ public class SimpleJob extends AbstractJob {
if (jobRepository.getStepExecutionCount(jobInstance, step) < step.getStartLimit()) {
// step start count is less than start max, return true
return true;
}
else {
} else {
// start max has been exceeded, throw an exception.
throw new InfrastructureException("Maximum start limit exceeded for step: " + step.getName() + "StartMax: "
+ step.getStartLimit());
+ step.getStartLimit());
}
}
@@ -193,16 +186,14 @@ public class SimpleJob extends AbstractJob {
private static void rethrow(Throwable t) throws RuntimeException {
if (t instanceof RuntimeException) {
throw (RuntimeException) t;
}
else {
} else {
throw new InfrastructureException(t);
}
}
/**
* Public setter for the {@link JobRepository} that is needed to manage the
* state of the batch meta domain (jobs, steps, executions) during the life
* of a job.
* Public setter for the {@link JobRepository} that is needed to manage the state of the batch meta domain (jobs,
* steps, executions) during the life of a job.
*
* @param jobRepository
*/

View File

@@ -24,6 +24,7 @@ import junit.framework.TestCase;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.ItemSkipPolicy;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionException;
import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.JobInterruptedException;
import org.springframework.batch.core.JobParameters;
@@ -50,8 +51,8 @@ import org.springframework.batch.retry.RetryPolicy;
import org.springframework.transaction.PlatformTransactionManager;
/**
* Tests for DefaultJobLifecycle. MapJobDao and MapStepExecutionDao are used instead of a
* mock repository to test that status is being stored correctly.
* Tests for DefaultJobLifecycle. MapJobDao and MapStepExecutionDao are used instead of a mock repository to test that
* status is being stored correctly.
*
* @author Lucas Ward
*/
@@ -60,9 +61,9 @@ public class SimpleJobTests extends TestCase {
private JobRepository jobRepository;
private JobInstanceDao jobInstanceDao;
private JobExecutionDao jobExecutionDao;
private StepExecutionDao stepExecutionDao;
private List list = new ArrayList();
@@ -82,9 +83,9 @@ public class SimpleJobTests extends TestCase {
private JobParameters jobParameters = new JobParameters();
private SimpleJob job;
private Step step1;
private Step step2;
protected void setUp() throws Exception {
@@ -131,16 +132,15 @@ public class SimpleJobTests extends TestCase {
stepExecution2 = new StepExecution(step2, jobExecution, null);
}
//Test to ensure the exit status returned by the last step is returned
public void testExitStatusReturned(){
final ExitStatus customStatus = new ExitStatus(true, "test");
Step testStep = new Step(){
public void execute(StepExecution stepExecution)
throws JobInterruptedException {
// Test to ensure the exit status returned by the last step is returned
public void testExitStatusReturned() throws JobExecutionException {
final ExitStatus customStatus = new ExitStatus(true, "test");
Step testStep = new Step() {
public void execute(StepExecution stepExecution) throws JobInterruptedException {
stepExecution.setExitStatus(customStatus);
}
@@ -154,7 +154,8 @@ public class SimpleJobTests extends TestCase {
public boolean isAllowStartIfComplete() {
return false;
}};
}
};
List steps = new ArrayList();
steps.add(testStep);
job.setSteps(steps);
@@ -177,14 +178,15 @@ public class SimpleJobTests extends TestCase {
}
public void testRunNormallyWithListener() throws Exception {
job.setJobListeners(new JobListenerSupport[] {new JobListenerSupport() {
job.setJobListeners(new JobListenerSupport[] { new JobListenerSupport() {
public void beforeJob(JobExecution jobExecution) {
list.add("before");
}
public void afterJob(JobExecution jobExecution) {
list.add("after");
}
}});
} });
job.execute(jobExecution);
assertEquals(4, list.size());
}
@@ -228,8 +230,7 @@ public class SimpleJobTests extends TestCase {
stepConfiguration1.setProcessException(exception);
try {
job.execute(jobExecution);
}
catch (InfrastructureException e) {
} catch (InfrastructureException e) {
assertEquals(exception, e.getCause());
}
assertEquals(0, list.size());
@@ -243,8 +244,7 @@ public class SimpleJobTests extends TestCase {
stepConfiguration1.setProcessException(exception);
try {
job.execute(jobExecution);
}
catch (RuntimeException e) {
} catch (RuntimeException e) {
assertEquals(exception, e);
}
assertEquals(0, list.size());
@@ -258,11 +258,10 @@ public class SimpleJobTests extends TestCase {
try {
job.execute(jobExecution);
fail("Expected BatchCriticalException");
}
catch (InfrastructureException ex) {
} catch (InfrastructureException ex) {
// expected
assertTrue("Wrong message in exception: " + ex.getMessage(), ex.getMessage()
.indexOf("start limit exceeded") >= 0);
.indexOf("start limit exceeded") >= 0);
}
}
@@ -272,26 +271,25 @@ public class SimpleJobTests extends TestCase {
job.execute(jobExecution);
ExitStatus exitStatus = jobExecution.getExitStatus();
assertTrue("Wrong message in execution: " + exitStatus, exitStatus.getExitDescription().indexOf(
"No steps configured") >= 0);
"No steps configured") >= 0);
}
// public void testNoStepsExecuted() throws Exception {
// StepExecution completedExecution = new StepExecution("completedExecution", jobExecution);
// completedExecution.setStatus(BatchStatus.COMPLETED);
//
// job.execute(jobExecution);
// ExitStatus exitStatus = jobExecution.getExitStatus();
// assertEquals(ExitStatus.NOOP.getExitCode(), exitStatus.getExitCode());
// assertTrue("Wrong message in execution: " + exitStatus, exitStatus.getExitDescription().contains(
// "steps already completed"));
// }
// public void testNoStepsExecuted() throws Exception {
// StepExecution completedExecution = new StepExecution("completedExecution", jobExecution);
// completedExecution.setStatus(BatchStatus.COMPLETED);
//
// job.execute(jobExecution);
// ExitStatus exitStatus = jobExecution.getExitStatus();
// assertEquals(ExitStatus.NOOP.getExitCode(), exitStatus.getExitCode());
// assertTrue("Wrong message in execution: " + exitStatus, exitStatus.getExitDescription().contains(
// "steps already completed"));
// }
public void testNotExecutedIfAlreadyStopped() throws Exception {
jobExecution.stop();
try {
job.execute(jobExecution);
}
catch (InfrastructureException e) {
} catch (InfrastructureException e) {
assertTrue(e.getCause() instanceof JobInterruptedException);
}
assertEquals(0, list.size());
@@ -354,21 +352,20 @@ public class SimpleJobTests extends TestCase {
public void execute(StepExecution stepExecution) throws JobInterruptedException, InfrastructureException {
if (exception instanceof RuntimeException) {
stepExecution.setExitStatus(ExitStatus.FAILED);
throw (RuntimeException)exception;
throw (RuntimeException) exception;
}
if (exception instanceof JobInterruptedException) {
stepExecution.setExitStatus(ExitStatus.INTERRUPTED);
throw (JobInterruptedException)exception;
throw (JobInterruptedException) exception;
}
if (runnable!=null) {
if (runnable != null) {
runnable.run();
}
stepExecution.setExitStatus(ExitStatus.FINISHED);
}
/**
* Set the name property. Always overrides the default value if this object
* is a Spring bean.
* Set the name property. Always overrides the default value if this object is a Spring bean.
*
* @see #setBeanName(java.lang.String)
*/
@@ -378,6 +375,7 @@ public class SimpleJobTests extends TestCase {
/**
* Public setter for the {@link RetryPolicy}.
*
* @param retryPolicy the {@link RetryPolicy} to set
*/
public void setRetryPolicy(RetryPolicy retryPolicy) {