Introduce KeyedItemReader to push the getKey() method into a separate place.

This commit is contained in:
dsyer
2008-01-30 08:55:53 +00:00
parent 152829ba5f
commit 6ffefbc184
31 changed files with 139 additions and 115 deletions

View File

@@ -55,7 +55,7 @@ public class SimpleJob extends JobSupport {
*
* @see org.springframework.batch.core.domain.Job#run(org.springframework.batch.core.domain.JobExecution)
*/
public ExitStatus run(JobExecution execution) throws BatchCriticalException {
public void run(JobExecution execution) throws BatchCriticalException {
JobInstance jobInstance = execution.getJobInstance();
updateStatus(execution, BatchStatus.STARTING);
@@ -78,7 +78,8 @@ public class SimpleJob extends JobSupport {
startedCount++;
updateStatus(execution, BatchStatus.STARTED);
StepExecution stepExecution = execution.createStepExecution(stepInstance);
status = step.process(stepExecution);
step.process(stepExecution);
status = stepExecution.getExitStatus();
}
}
@@ -111,7 +112,6 @@ public class SimpleJob extends JobSupport {
jobRepository.saveOrUpdate(execution);
}
return status;
}
private void updateStatus(JobExecution jobExecution, BatchStatus status) {

View File

@@ -24,7 +24,6 @@ import org.springframework.batch.core.domain.JobParameters;
import org.springframework.batch.core.domain.JobSupport;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.repeat.ExitStatus;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.task.SyncTaskExecutor;
import org.springframework.core.task.TaskExecutor;
@@ -83,10 +82,7 @@ public class SimpleJobLauncher implements JobLauncher, InitializingBean {
public void run() {
try {
logger.info("Job: [" + job + "] launched with the following parameters: [" + jobParameters + "]");
ExitStatus exitStatus = job.run(jobExecution);
// The exit status should be set by the Job. TODO: remove
// this line...
jobExecution.setExitStatus(exitStatus);
job.run(jobExecution);
logger.info("Job: [" + job + "] completed successfully with the following parameters: ["
+ jobParameters + "]");
}

View File

@@ -22,7 +22,6 @@ import org.springframework.batch.core.domain.StepSupport;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.tasklet.Tasklet;
import org.springframework.batch.io.exception.BatchCriticalException;
import org.springframework.batch.repeat.ExitStatus;
import org.springframework.batch.repeat.exception.handler.ExceptionHandler;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.util.Assert;
@@ -112,9 +111,9 @@ public abstract class AbstractStep extends StepSupport {
/* (non-Javadoc)
* @see org.springframework.batch.core.domain.StepSupport#process(org.springframework.batch.core.domain.StepExecution)
*/
public ExitStatus process(StepExecution stepExecution) throws StepInterruptedException, BatchCriticalException {
public void process(StepExecution stepExecution) throws StepInterruptedException, BatchCriticalException {
SimpleStepExecutor executor = createStepExecutor();
return executor.process(stepExecution);
executor.process(stepExecution);
}
/**

View File

@@ -20,7 +20,6 @@ import org.springframework.batch.core.domain.Step;
import org.springframework.batch.core.domain.StepExecution;
import org.springframework.batch.core.domain.StepInterruptedException;
import org.springframework.batch.io.exception.BatchCriticalException;
import org.springframework.batch.repeat.ExitStatus;
import org.springframework.batch.repeat.RepeatOperations;
/**
@@ -78,7 +77,7 @@ public class RepeatOperationsStep extends AbstractStep implements RepeatOperatio
/* (non-Javadoc)
* @see org.springframework.batch.execution.step.simple.AbstractStep#process(org.springframework.batch.core.domain.StepExecution)
*/
public ExitStatus process(StepExecution stepExecution) throws StepInterruptedException, BatchCriticalException {
public void process(StepExecution stepExecution) throws StepInterruptedException, BatchCriticalException {
assertMandatoryProperties();
SimpleStepExecutor executor = (SimpleStepExecutor) super.createStepExecutor();
if (stepOperations != null) {
@@ -87,6 +86,6 @@ public class RepeatOperationsStep extends AbstractStep implements RepeatOperatio
if (chunkOperations != null) {
executor.setChunkOperations(chunkOperations);
}
return executor.process(stepExecution);
executor.process(stepExecution);
}
}

View File

@@ -173,7 +173,7 @@ public class SimpleStepExecutor {
* execution
* @see StepExecutor#process(StepExecution)
*/
public ExitStatus process(final StepExecution stepExecution) throws BatchCriticalException,
public void process(final StepExecution stepExecution) throws BatchCriticalException,
StepInterruptedException {
final StepInstance stepInstance = stepExecution.getStep();
@@ -276,7 +276,6 @@ public class SimpleStepExecutor {
});
updateStatus(stepExecution, BatchStatus.COMPLETED);
return status;
}
catch (RuntimeException e) {

View File

@@ -21,6 +21,7 @@ import org.springframework.batch.io.Skippable;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemRecoverer;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.KeyedItemReader;
import org.springframework.batch.repeat.ExitStatus;
import org.springframework.batch.retry.RetryPolicy;
import org.springframework.batch.retry.callback.ItemReaderRetryCallback;
@@ -111,7 +112,8 @@ public class ItemOrientedTasklet implements Tasklet, Skippable, InitializingBean
template.setRetryPolicy(itemProviderRetryPolicy);
if (retryPolicy != null) {
retryCallback = new ItemReaderRetryCallback(itemProvider, itemWriter);
Assert.state(itemProvider instanceof KeyedItemReader, "ItemReader must be instance of KeyedItemReader to use the retry policy");
retryCallback = new ItemReaderRetryCallback((KeyedItemReader) itemProvider, itemWriter);
retryCallback.setRecoverer(itemRecoverer);
}

View File

@@ -272,7 +272,7 @@ public class SimpleJobTests extends TestCase {
this.runnable = runnable;
}
public ExitStatus process(StepExecution stepExecution) throws StepInterruptedException, BatchCriticalException {
public void process(StepExecution stepExecution) throws StepInterruptedException, BatchCriticalException {
if (exception instanceof RuntimeException) {
throw (RuntimeException)exception;
}
@@ -282,7 +282,7 @@ public class SimpleJobTests extends TestCase {
if (runnable!=null) {
runnable.run();
}
return ExitStatus.FINISHED;
stepExecution.setExitStatus(ExitStatus.FINISHED);
}
}

View File

@@ -41,8 +41,9 @@ public class SimpleJobLauncherTests extends TestCase {
private MockControl repositoryControl = MockControl.createControl(JobRepository.class);
private Job job = new JobSupport("foo") {
public ExitStatus run(JobExecution execution) {
return ExitStatus.FINISHED;
public void run(JobExecution execution) {
execution.setExitStatus(ExitStatus.FINISHED);
return;
}
};
@@ -88,7 +89,7 @@ public class SimpleJobLauncherTests extends TestCase {
public void testRunWithException() throws Exception {
job = new JobSupport() {
public ExitStatus run(JobExecution execution) {
public void run(JobExecution execution) {
execution.setExitStatus(ExitStatus.FAILED);
throw new RuntimeException("foo");
}
@@ -104,7 +105,7 @@ public class SimpleJobLauncherTests extends TestCase {
public void testRunWithError() throws Exception {
job = new JobSupport() {
public ExitStatus run(JobExecution execution) {
public void run(JobExecution execution) {
execution.setExitStatus(ExitStatus.FAILED);
throw new Error("foo");
}

View File

@@ -27,6 +27,7 @@ import org.springframework.batch.io.Skippable;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemRecoverer;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.KeyedItemReader;
import org.springframework.batch.item.reader.AbstractItemReader;
import org.springframework.batch.item.writer.AbstractItemWriter;
import org.springframework.batch.repeat.context.RepeatContextSupport;
@@ -217,11 +218,7 @@ public class ItemOrientedTaskletTests extends TestCase {
return true;
}
});
module.setItemReader(new AbstractItemReader() {
public Object read() throws Exception {
return "foo";
}
});
module.setItemReader(new MockItemReader());
module.setItemWriter(new AbstractItemWriter() {
public void write(Object data) throws Exception {
throw new RuntimeException("FOO");
@@ -267,7 +264,16 @@ public class ItemOrientedTaskletTests extends TestCase {
}
}
private class SkippableItemReader implements ItemReader,
private class MockItemReader extends AbstractItemReader implements KeyedItemReader {
public Object read() throws Exception {
return "foo";
}
public Object getKey(Object item) {
return item;
}
}
private class SkippableItemReader implements KeyedItemReader,
Skippable, StatisticsProvider {
public Object read() throws Exception {
return itemProvider.read();

View File

@@ -55,10 +55,6 @@ public class RestartableItemOrientedTaskletTests extends TestCase {
assertEquals(this.data.getProperties(), data.getProperties());
}
public Object getKey(Object item) {
return null;
}
public boolean recover(Object data, Throwable cause) {
return false;
}