OPEN - issue BATCH-364: StepScope responsibilities can be assumed by Step (not ApplicationContext)

http://jira.springframework.org/browse/BATCH-364

Make StreamManager responsible for calling open() and close().
This commit is contained in:
dsyer
2008-02-15 13:44:36 +00:00
parent 66ddfa81f0
commit ec5379ebac
26 changed files with 460 additions and 420 deletions

View File

@@ -37,13 +37,11 @@ import org.springframework.batch.item.ItemReader;
* @author Dave Syer
*
*/
public class AggregateItemReader extends AbstractItemReader {
public class AggregateItemReader extends DelegatingItemReader {
private static final Log log = LogFactory
.getLog(AggregateItemReader.class);
private ItemReader inputSource;
/**
* Marker for the end of a multi-object record.
*/
@@ -63,7 +61,7 @@ public class AggregateItemReader extends AbstractItemReader {
public Object read() throws Exception {
ResultHolder holder = new ResultHolder();
while (process(inputSource.read(), holder)) {
while (process(getItemReader().read(), holder)) {
continue;
}
@@ -100,16 +98,6 @@ public class AggregateItemReader extends AbstractItemReader {
return true;
}
/**
* Injection setter for {@link ItemReader}.
*
* @param inputSource
* an {@link ItemReader}.
*/
public void setItemReader(ItemReader inputSource) {
this.inputSource = inputSource;
}
/**
* Private class for temporary state management while item is being
* collected.

View File

@@ -28,7 +28,7 @@ import org.springframework.util.Assert;
* Simple wrapper around {@link ItemReader}. The input source is expected to
* take care of open and close operations. If necessary it should be registered
* as a step scoped bean to ensure that the lifecycle methods are called.
*
*
* @author Dave Syer
*/
public class DelegatingItemReader extends AbstractItemReader implements Skippable, InitializingBean, ItemStream {
@@ -38,9 +38,10 @@ public class DelegatingItemReader extends AbstractItemReader implements Skippabl
public void afterPropertiesSet() throws Exception {
Assert.notNull(inputSource, "ItemReader must not be null.");
}
/**
* Get the next object from the input source.
* @throws Exception
* @throws Exception
* @see org.springframework.batch.item.ItemReader#read()
*/
public Object read() throws Exception {
@@ -53,9 +54,10 @@ public class DelegatingItemReader extends AbstractItemReader implements Skippabl
* {@link ItemStream}.
*/
public ExecutionContext getExecutionContext() {
// TODO: this is not necessary...
Assert.state(inputSource instanceof ItemStream, "Input source is not ItemStream");
return ((ItemStream) inputSource).getExecutionContext();
if (inputSource instanceof ItemStream) {
return ((ItemStream) inputSource).getExecutionContext();
}
return new ExecutionContext();
}
/**
@@ -64,8 +66,9 @@ public class DelegatingItemReader extends AbstractItemReader implements Skippabl
* {@link ItemStream}.
*/
public void restoreFrom(ExecutionContext data) {
Assert.state(inputSource instanceof ItemStream, "Input source is not ItemStream");
((ItemStream) inputSource).restoreFrom(data);
if (inputSource instanceof ItemStream) {
((ItemStream) inputSource).restoreFrom(data);
}
}
/**
@@ -82,11 +85,12 @@ public class DelegatingItemReader extends AbstractItemReader implements Skippabl
public void skip() {
if (inputSource instanceof Skippable) {
((Skippable)inputSource).skip();
((Skippable) inputSource).skip();
}
}
/* (non-Javadoc)
/*
* (non-Javadoc)
* @see org.springframework.batch.item.ItemStream#open()
*/
public void open() throws StreamException {
@@ -95,7 +99,8 @@ public class DelegatingItemReader extends AbstractItemReader implements Skippabl
}
}
/* (non-Javadoc)
/*
* (non-Javadoc)
* @see org.springframework.batch.item.ItemStream#open()
*/
public void close() throws StreamException {
@@ -116,7 +121,8 @@ public class DelegatingItemReader extends AbstractItemReader implements Skippabl
return false;
}
/* (non-Javadoc)
/*
* (non-Javadoc)
* @see org.springframework.batch.item.ItemStream#mark(org.springframework.batch.item.ExecutionContext)
*/
public void mark() {
@@ -125,7 +131,8 @@ public class DelegatingItemReader extends AbstractItemReader implements Skippabl
}
}
/* (non-Javadoc)
/*
* (non-Javadoc)
* @see org.springframework.batch.item.ItemStream#reset(org.springframework.batch.item.ExecutionContext)
*/
public void reset() {

View File

@@ -116,7 +116,7 @@ public class SimpleStreamManager implements StreamManager {
* @see org.springframework.batch.item.stream.StreamManager#register(java.lang.Object,
* org.springframework.batch.item.ItemStream, ExecutionContext)
*/
public void register(Object key, ItemStream stream, ExecutionContext executionContext) {
public void register(Object key, ItemStream stream) {
synchronized (registry) {
Set set = (Set) registry.get(key);
if (set == null) {
@@ -125,9 +125,19 @@ public class SimpleStreamManager implements StreamManager {
}
set.add(stream);
}
}
/* (non-Javadoc)
* @see org.springframework.batch.item.stream.StreamManager#restoreFrom(java.lang.Object, org.springframework.batch.item.ExecutionAttributes)
*/
public void restoreFrom(Object key, final ExecutionContext executionContext) {
if (executionContext != null) {
stream.restoreFrom(extract(stream, executionContext));
}
iterate(key, new Callback() {
public void execute(ItemStream stream) {
stream.restoreFrom(extract(stream, executionContext));
}
});
}
}
/**
@@ -153,7 +163,7 @@ public class SimpleStreamManager implements StreamManager {
/**
* Broadcast the call to close from this {@link StreamManager}.
* @throws Exception
* @throws StreamException
*/
public void close(Object key) throws StreamException {
iterate(key, new Callback() {
@@ -163,6 +173,18 @@ public class SimpleStreamManager implements StreamManager {
});
}
/**
* Broadcast the call to open from this {@link StreamManager}.
* @throws StreamException
*/
public void open(Object key) throws StreamException {
iterate(key, new Callback() {
public void execute(ItemStream stream) {
stream.open();
}
});
}
/**
* Delegate to the {@link PlatformTransactionManager} to create a new
* transaction.

View File

@@ -37,9 +37,8 @@ public interface StreamManager {
*
* @param key the key under which to add the provider
* @param stream an {@link ItemStream}
* @param executionContext the context (may be null) to restore from on registration
*/
void register(Object key, ItemStream stream, ExecutionContext executionContext);
void register(Object key, ItemStream stream);
/**
* Extract and aggregate the {@link ExecutionContext} from all streams under
@@ -60,6 +59,15 @@ public interface StreamManager {
* been registered.
*/
void close(Object key) throws StreamException;
/**
* If any resources are needed for the stream to operate they need to be
* opened here.
*
* @param key the key under which {@link ItemStream} instances might have
* been registered.
*/
void open(Object key) throws StreamException;
TransactionStatus getTransaction(Object key);
@@ -67,4 +75,12 @@ public interface StreamManager {
void rollback(TransactionStatus transaction);
/**
* Restore the streams registered under a goven key.
*
* @param key the key under which the streams are stored
* @param executionContext the context (may be null) to restore from
*/
void restoreFrom(Object key, ExecutionContext executionContext);
}