diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/sample/advice/DefaultJobInterruptionAdvice.java b/spring-batch-samples/src/main/java/org/springframework/batch/sample/advice/DefaultJobInterruptionAdvice.java deleted file mode 100644 index 4209967c0..000000000 --- a/spring-batch-samples/src/main/java/org/springframework/batch/sample/advice/DefaultJobInterruptionAdvice.java +++ /dev/null @@ -1,44 +0,0 @@ -package org.springframework.batch.sample.advice; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.springframework.batch.core.BatchStatus; -import org.springframework.batch.core.JobExecution; -import org.springframework.batch.core.JobInterruptedException; - -/** - * Monitors {@link JobExecution} and throws a {@link JobInterruptedException} in - * case the execution has been requested to stop using - * {@link JobExecution#stop()} or executing thread has been interrupted. - * - * @author Robert Kasanicky - */ -public class DefaultJobInterruptionAdvice implements JobInterruptionAdvice { - - private static final Log logger = LogFactory.getLog(DefaultJobInterruptionAdvice.class); - - private JobExecution jobExecution; - - /* - * (non-Javadoc) - * @see org.springframework.batch.core.runtime.JobInterruptionAdvice#checkInterrupt() - */ - public void checkInterrupt() throws JobInterruptedException { - - logger.info("checking job execution for interrupt"); - - if (Thread.currentThread().isInterrupted() || jobExecution.getStatus() == BatchStatus.STOPPING) { - throw new JobInterruptedException("Job execution interrupted by user"); - } - } - - /** - * Setter for JobExecution - to be applied as 'after returning' advice that - * captures return value on the method that creates {@link JobExecution} for - * the job run. - */ - public void setJobExecution(JobExecution jobExecution) { - this.jobExecution = jobExecution; - logger.info("JobExecution set"); - } -} diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/sample/advice/JobInterruptionAdvice.java b/spring-batch-samples/src/main/java/org/springframework/batch/sample/advice/JobInterruptionAdvice.java deleted file mode 100644 index ab708ccc9..000000000 --- a/spring-batch-samples/src/main/java/org/springframework/batch/sample/advice/JobInterruptionAdvice.java +++ /dev/null @@ -1,25 +0,0 @@ -package org.springframework.batch.sample.advice; - -import org.springframework.batch.core.JobInterruptedException; - -/** - * Interface for monitors that check whether a job was interrupted by user. - * - * Interruption checking is a cross-cutting concern, therefore favorably handled - * by AOP. The implementation if expected to be used as 'before advice' or - * 'after advice' or both. - * - * @see DefaultJobInterruptionAdvice - * - * @author Robert Kasanicky - */ -public interface JobInterruptionAdvice { - - /** - * No-op unless job was interrupted by user. - * - * @throws JobInterruptedException if job was interrupted - */ - void checkInterrupt() throws JobInterruptedException; - -} \ No newline at end of file diff --git a/spring-batch-samples/src/main/resources/jobs/infiniteLoopJob.xml b/spring-batch-samples/src/main/resources/jobs/infiniteLoopJob.xml index edaaeec95..716446d52 100644 --- a/spring-batch-samples/src/main/resources/jobs/infiniteLoopJob.xml +++ b/spring-batch-samples/src/main/resources/jobs/infiniteLoopJob.xml @@ -34,19 +34,6 @@ - - - - - - - - - - \ No newline at end of file diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/advice/DefaultJobInterruptionAdviceTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/advice/DefaultJobInterruptionAdviceTests.java deleted file mode 100644 index e94f6f150..000000000 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/advice/DefaultJobInterruptionAdviceTests.java +++ /dev/null @@ -1,92 +0,0 @@ -package org.springframework.batch.sample.advice; - -import junit.framework.TestCase; - -import org.springframework.batch.core.BatchStatus; -import org.springframework.batch.core.JobExecution; -import org.springframework.batch.core.JobInstance; -import org.springframework.batch.core.JobInterruptedException; -import org.springframework.batch.core.JobParameters; -import org.springframework.batch.sample.tasklet.JobSupport; - -/** - * Tests for {@link DefaultJobInterruptionAdvice}. - * - * @author Robert Kasanicky - */ -public class DefaultJobInterruptionAdviceTests extends TestCase { - - private DefaultJobInterruptionAdvice tested = new DefaultJobInterruptionAdvice(); - - private JobExecution jobExecution = new JobExecution(new JobInstance(new Long(1), new JobParameters(), - new JobSupport("interruptJob"))); - - protected void setUp() throws Exception { - tested.setJobExecution(jobExecution); - } - - /** - * Scenario when JobExecution is requested to stop explicitly. - */ - public void testStop() throws JobInterruptedException { - - jobExecution.stop(); - try { - tested.checkInterrupt(); - fail(); - } - catch (JobInterruptedException e) { - // expected - } - } - - /** - * Scenario when executing thread is interrupted. Needs to be run in - * separate victim thread - interrupting current thread would affect other - * tests. - */ - public void testThreadInterrupt() throws Exception { - - class TestJob implements Runnable { - - volatile boolean interrupted = false; - - public void run() { - while (true) { - try { - tested.checkInterrupt(); - } - catch (JobInterruptedException expected) { - interrupted = true; - return; - } - } - } - } - - TestJob job = new TestJob(); - Thread victim = new Thread(job); - - victim.start(); - victim.interrupt(); - - Thread.sleep(1000); - assertTrue(job.interrupted); - - } - - /** - * No exception raised when JobExecution has non-terminating status. - */ - public void testNoInterrupt() throws JobInterruptedException { - - BatchStatus[] notInterruptedValues = { BatchStatus.STARTED, BatchStatus.STARTING, BatchStatus.STOPPED, - BatchStatus.UNKNOWN, BatchStatus.COMPLETED, BatchStatus.FAILED }; - - for (int i = 0; i < notInterruptedValues.length; i++) { - jobExecution.setStatus(notInterruptedValues[i]); - tested.checkInterrupt(); - assertTrue(true); - } - } -}