diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/DuplicateJobException.java b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/DuplicateJobException.java index ed2d93220..636a8860a 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/DuplicateJobException.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/DuplicateJobException.java @@ -16,6 +16,7 @@ package org.springframework.batch.core.repository; import org.springframework.batch.core.Job; +import org.springframework.batch.core.JobExecutionException; /** * Checked exception that indicates a name clash when registering @@ -24,7 +25,7 @@ import org.springframework.batch.core.Job; * @author Dave Syer * */ -public class DuplicateJobException extends JobException { +public class DuplicateJobException extends JobExecutionException { /** * Create an exception with the given message. diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/JobException.java b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/JobException.java deleted file mode 100644 index a68c3e3de..000000000 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/JobException.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright 2006-2007 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.batch.core.repository; - -import org.springframework.batch.core.Job; - -/** - * Base class for checked exceptions related to {@link Job} - * creation, registration or use. - * - * @author Dave Syer - * - */ -public class JobException extends Exception { - - /** - * Create an exception with the given message. - */ - public JobException(String msg) { - super(msg); - } - - /** - * @param msg The message to send to caller - * @param e the cause of the exception - */ - public JobException(String msg, Throwable e) { - super(msg, e); - } - -} diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/JobRestartException.java b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/JobRestartException.java index 8301df98f..778367c39 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/JobRestartException.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/JobRestartException.java @@ -15,13 +15,15 @@ */ package org.springframework.batch.core.repository; +import org.springframework.batch.core.JobExecutionException; + /** * An exception indicating an illegal attempt to restart a job. * * @author Dave Syer * */ -public class JobRestartException extends JobException { +public class JobRestartException extends JobExecutionException { /** * @param string the message diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/NoSuchJobException.java b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/NoSuchJobException.java index 171ab381e..ca251868a 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/NoSuchJobException.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/NoSuchJobException.java @@ -16,6 +16,7 @@ package org.springframework.batch.core.repository; import org.springframework.batch.core.Job; +import org.springframework.batch.core.JobExecutionException; /** @@ -25,9 +26,7 @@ import org.springframework.batch.core.Job; * @author Dave Syer * */ -public class NoSuchJobException extends JobException { - - private static final long serialVersionUID = -8044082897778706564L; +public class NoSuchJobException extends JobExecutionException { /** * Create an exception with the given message. diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/repository/JobExceptionTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/repository/JobExceptionTests.java deleted file mode 100644 index 8b010b715..000000000 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/repository/JobExceptionTests.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright 2006-2007 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.batch.core.repository; - -import org.springframework.batch.core.AbstractExceptionTests; -import org.springframework.batch.core.repository.JobException; - -/** - * @author Dave Syer - * - */ -public class JobExceptionTests extends AbstractExceptionTests { - - /* - * (non-Javadoc) - * @see org.springframework.batch.io.exception.AbstractExceptionTests#getException(java.lang.String) - */ - public Exception getException(String msg) throws Exception { - return new JobException(msg); - } - - /* - * (non-Javadoc) - * @see org.springframework.batch.io.exception.AbstractExceptionTests#getException(java.lang.String, - * java.lang.Throwable) - */ - public Exception getException(String msg, Throwable t) throws Exception { - return new JobException(msg, t); - } - -} diff --git a/spring-batch-execution/src/main/java/org/springframework/batch/execution/job/SimpleJob.java b/spring-batch-execution/src/main/java/org/springframework/batch/execution/job/SimpleJob.java index ebf10b97d..e4a242ae8 100644 --- a/spring-batch-execution/src/main/java/org/springframework/batch/execution/job/SimpleJob.java +++ b/spring-batch-execution/src/main/java/org/springframework/batch/execution/job/SimpleJob.java @@ -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 */ diff --git a/spring-batch-execution/src/test/java/org/springframework/batch/execution/job/SimpleJobTests.java b/spring-batch-execution/src/test/java/org/springframework/batch/execution/job/SimpleJobTests.java index 34597b542..49ca09db2 100644 --- a/spring-batch-execution/src/test/java/org/springframework/batch/execution/job/SimpleJobTests.java +++ b/spring-batch-execution/src/test/java/org/springframework/batch/execution/job/SimpleJobTests.java @@ -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) {