BATCH-365: There should now be one ExecutionContext per step. All itemStreams will be opened with an execution context, and will be notified before it is saved, to ensure they have all state in the context. Most ItemReader/Writers should now have the logic for whether or not to put their state in the context, but a few have likely been missed.

This commit is contained in:
lucasward
2008-02-26 08:29:59 +00:00
parent 38c486e449
commit d24f0af439
8 changed files with 33 additions and 44 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.batch.sample.dao;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemStream;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.sample.domain.CustomerCredit;
@@ -39,7 +40,7 @@ public class FlatFileCustomerCreditWriter implements CustomerCreditDao,
public void writeCredit(CustomerCredit customerCredit) throws Exception {
if (!opened) {
open();
open(new ExecutionContext());
}
String line = "" + customerCredit.getName() + separator
@@ -56,9 +57,9 @@ public class FlatFileCustomerCreditWriter implements CustomerCreditDao,
this.outputSource = outputSource;
}
public void open() throws Exception {
public void open(ExecutionContext executionContext) throws Exception {
if (outputSource instanceof ItemStream) {
((ItemStream) outputSource).open();
((ItemStream) outputSource).open(executionContext);
}
opened = true;
}

View File

@@ -80,12 +80,6 @@ public class GeneratingItemReader extends AbstractItemReaderRecoverer implements
this.marked = this.counter;
}
/* (non-Javadoc)
* @see org.springframework.batch.item.ItemStream#open()
*/
public void open() throws StreamException {
}
/* (non-Javadoc)
* @see org.springframework.batch.item.ItemStream#reset()
*/
@@ -102,8 +96,10 @@ public class GeneratingItemReader extends AbstractItemReaderRecoverer implements
/* (non-Javadoc)
* @see org.springframework.batch.item.ExecutionContextProvider#getExecutionContext()
*/
public ExecutionContext getExecutionContext() {
return new ExecutionContext();
public void beforeSave() {
}
public void open(ExecutionContext context) throws StreamException {
}
}

View File

@@ -57,7 +57,7 @@ public class StagingItemReader extends JdbcDaoSupport implements ItemStream, Key
*
* @see org.springframework.batch.io.driving.DrivingQueryItemReader#open()
*/
public void open() {
public void open(ExecutionContext executionContext) {
// Can be called from multiple threads because of lazy initialisation...
synchronized (lock) {
if (keys == null) {
@@ -227,22 +227,12 @@ public class StagingItemReader extends JdbcDaoSupport implements ItemStream, Key
getBuffer().rollback();
}
/*
* (non-Javadoc)
*
* @see org.springframework.batch.item.ItemStream#restoreFrom(org.springframework.batch.item.ExecutionContext)
*/
public void restoreFrom(ExecutionContext context) {
// no-op
}
/*
* (non-Javadoc)
*
* @see org.springframework.batch.item.ExecutionContextProvider#getExecutionContext()
*/
public ExecutionContext getExecutionContext() {
return new ExecutionContext();
public void beforeSave() {
}
}

View File

@@ -18,7 +18,6 @@ package org.springframework.batch.sample.tasklet;
import org.springframework.batch.core.tasklet.Tasklet;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ExecutionContextProvider;
import org.springframework.batch.repeat.ExitStatus;
import org.springframework.batch.support.PropertiesConverter;
@@ -30,7 +29,7 @@ import org.springframework.batch.support.PropertiesConverter;
* @author Lucas Ward
*
*/
public class InfiniteLoopTasklet implements Tasklet, ExecutionContextProvider {
public class InfiniteLoopTasklet implements Tasklet {
private int count = 0;
@@ -47,11 +46,4 @@ public class InfiniteLoopTasklet implements Tasklet, ExecutionContextProvider {
return ExitStatus.CONTINUABLE;
}
/* (non-Javadoc)
* @see org.springframework.batch.item.ExecutionContextProvider#getExecutionContext()
*/
public ExecutionContext getExecutionContext() {
return new ExecutionContext(PropertiesConverter.stringToProperties("count=" + count));
}
}

View File

@@ -18,7 +18,8 @@ package org.springframework.batch.sample.tasklet;
import org.springframework.batch.core.tasklet.Tasklet;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ExecutionContextProvider;
import org.springframework.batch.item.ItemStream;
import org.springframework.batch.item.exception.StreamException;
import org.springframework.batch.item.writer.AbstractItemWriter;
import org.springframework.batch.sample.dao.TradeDao;
import org.springframework.batch.sample.domain.Trade;
@@ -38,7 +39,7 @@ import org.springframework.util.Assert;
* @author Lucas Ward
* @author Dave Syer
*/
public class SimpleTradeWriter extends AbstractItemWriter implements ExecutionContextProvider {
public class SimpleTradeWriter extends AbstractItemWriter implements ItemStream {
/*
* writes a Trade object to output
@@ -49,6 +50,8 @@ public class SimpleTradeWriter extends AbstractItemWriter implements ExecutionCo
* number of trade objects processed
*/
private int tradeCount = 0;
private ExecutionContext executionContext;
/**
* The input template is read using the readAndMap method, which accepts a
@@ -65,14 +68,18 @@ public class SimpleTradeWriter extends AbstractItemWriter implements ExecutionCo
public void setTradeDao(TradeDao tradeDao) {
this.tradeDao = tradeDao;
}
public void open(ExecutionContext context) throws StreamException {
this.executionContext = context;
}
/* (non-Javadoc)
* @see org.springframework.batch.item.ExecutionContextProvider#getExecutionContext()
*/
public ExecutionContext getExecutionContext() {
ExecutionContext statistics = new ExecutionContext();
statistics.putLong("trade.count", tradeCount);
return statistics;
public void beforeSave() {
executionContext.putLong("trade.count", tradeCount);
}
public void close() throws StreamException {
}
}