BATCH-401: prototype of JobInterruptionAdvice added to samples (see GracefulShutdownTests)

This commit is contained in:
robokaso
2008-02-29 18:21:06 +00:00
parent 9336443e01
commit b4655119ca
7 changed files with 248 additions and 10 deletions

View File

@@ -0,0 +1,44 @@
package org.springframework.batch.sample.advice;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.core.domain.BatchStatus;
import org.springframework.batch.core.domain.JobExecution;
import org.springframework.batch.core.domain.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");
}
}

View File

@@ -0,0 +1,25 @@
package org.springframework.batch.sample.advice;
import org.springframework.batch.core.domain.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;
}

View File

@@ -0,0 +1,38 @@
package org.springframework.batch.sample.item.reader;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.exception.MarkFailedException;
import org.springframework.batch.item.exception.ResetFailedException;
/**
* Creates items infinitely - useful for testing job interruption by user.
*/
public class InfiniteItemReader implements ItemReader {
private static final Log logger = LogFactory.getLog(InfiniteItemReader.class);
private long counter = 0;
public Object read() throws Exception {
Object item = "item" + counter;
Thread.sleep(10);
logger.info("read item: " + item);
return item;
}
public void mark() throws MarkFailedException {
// no-op
}
public void reset() throws ResetFailedException {
// no-op
}
}

View File

@@ -0,0 +1,15 @@
package org.springframework.batch.sample.step.support;
import org.springframework.batch.core.domain.JobInterruptedException;
import org.springframework.batch.execution.step.support.StepInterruptionPolicy;
import org.springframework.batch.repeat.RepeatContext;
public class NoopStepInterruptionPolicy implements StepInterruptionPolicy {
public void checkInterrupted(RepeatContext context)
throws JobInterruptedException {
// no-op
}
}