BATCH-768:Updated reference documentation for m1. Also added classes to samples that are necessary for docs.
This commit is contained in:
@@ -1951,15 +1951,15 @@
|
||||
that contains another <classname>ItemReader</classname>. For
|
||||
example:</para>
|
||||
|
||||
<programlisting> public class CompositeItemWriter implements ItemWriter {
|
||||
<programlisting> public class CompositeItemWriter<T> implements ItemWriter<T> {
|
||||
|
||||
ItemWriter itemWriter;
|
||||
ItemWriter<T> itemWriter;
|
||||
|
||||
public CompositeItemWriter(ItemWriter itemWriter) {
|
||||
public CompositeItemWriter(ItemWriter<T> itemWriter) {
|
||||
this.itemWriter = itemWriter;
|
||||
}
|
||||
|
||||
public void write(Object item) throws Exception {
|
||||
public void write(T item) throws Exception {
|
||||
|
||||
//Add business logic here
|
||||
|
||||
@@ -1990,10 +1990,10 @@
|
||||
just want to modify the item. For this scenario, Spring Batch provides the
|
||||
<classname>ItemTransformer</classname> interface:</para>
|
||||
|
||||
<programlisting> public interface ItemTransformer {
|
||||
<programlisting> public interface ItemProcessor<I, O> {
|
||||
|
||||
Object transform(Object item) throws Exception;
|
||||
}</programlisting>
|
||||
O process(I item) throws Exception;
|
||||
}</programlisting>
|
||||
|
||||
<para>An <classname>ItemTransformer</classname> is very simple, given one
|
||||
object, transorm it and return another. The object provided may or may not
|
||||
@@ -2015,7 +2015,7 @@
|
||||
public Bar(Foo foo) {}
|
||||
}
|
||||
|
||||
public class FooTransformer implements ItemTransformer{
|
||||
public class FooTransformer implements ItemProcessor{
|
||||
|
||||
//Preform simple transformation, convert a Foo to a Barr
|
||||
public Object transform(Object item) throws Exception {
|
||||
@@ -2126,7 +2126,7 @@
|
||||
<classname>BarTransformer</classname> can be 'chained' together to give
|
||||
the resultant <classname>Foobar</classname>:</para>
|
||||
|
||||
<programlisting> CompositeItemTransformer compositeTransformer = new CompositeItemTransformer();
|
||||
<programlisting> CompositeItemProcessor compositeTransformer = new CompositeItemProcessor();
|
||||
List itemTransformers = new ArrayList();
|
||||
itemTransformers.add(new FooTransformer());
|
||||
itemTransformers.add(new BarTransformer());
|
||||
@@ -2299,15 +2299,15 @@
|
||||
basic contract of <classname>ItemReader</classname>,
|
||||
<methodname>read</methodname>:</para>
|
||||
|
||||
<programlisting> public class CustomItemReader implements ItemReader{
|
||||
<programlisting> public class CustomItemReader<T> implements ItemReader<T>{
|
||||
|
||||
List items;
|
||||
List<T> items;
|
||||
|
||||
public CustomItemReader(List items) {
|
||||
public CustomItemReader(List<T> items) {
|
||||
this.items = items;
|
||||
}
|
||||
|
||||
public Object read() throws Exception, UnexpectedInputException,
|
||||
public T read() throws Exception, UnexpectedInputException,
|
||||
NoWorkFoundException, ParseException {
|
||||
|
||||
if (!items.isEmpty()) {
|
||||
@@ -2326,12 +2326,12 @@
|
||||
thus satisfying the most basic requirements of an
|
||||
<classname>ItemReader</classname>, as illustrated below:</para>
|
||||
|
||||
<programlisting> List items = new ArrayList();
|
||||
<programlisting> List<String> items = new ArrayList<String>();
|
||||
items.add("1");
|
||||
items.add("2");
|
||||
items.add("3");
|
||||
|
||||
ItemReader itemReader = new CustomItemReader(items);
|
||||
ItemReader itemReader = new CustomItemReader<String>(items);
|
||||
assertEquals("1", itemReader.read());
|
||||
assertEquals("2", itemReader.read());
|
||||
assertEquals("3", itemReader.read());
|
||||
@@ -2351,17 +2351,17 @@
|
||||
empty, but we'll need to add code to them in order to support the
|
||||
rollback scenario:</para>
|
||||
|
||||
<programlisting> public class CustomItemReader implements ItemReader{
|
||||
<programlisting> public class CustomItemReader<T> implements ItemReader<T>{
|
||||
|
||||
List items;
|
||||
List<T> items;
|
||||
int currentIndex = 0;
|
||||
int lastMarkedIndex = 0;
|
||||
|
||||
public CustomItemReader(List items) {
|
||||
public CustomItemReader(List<T> items) {
|
||||
this.items = items;
|
||||
}
|
||||
|
||||
public Object read() throws Exception, UnexpectedInputException,
|
||||
public T read() throws Exception, UnexpectedInputException,
|
||||
NoWorkFoundException, ParseException {
|
||||
|
||||
if (currentIndex < items.size()) {
|
||||
@@ -2434,49 +2434,51 @@
|
||||
implemented with the <classname>ItemStream</classname>
|
||||
interface:</para>
|
||||
|
||||
<programlisting> public class CustomItemReader implements ItemReader, ItemStream{
|
||||
<programlisting> public class CustomItemReader<T> implements ItemReader<T>, ItemStream{
|
||||
|
||||
List items;
|
||||
List<T> items;
|
||||
int currentIndex = 0;
|
||||
int lastMarkedIndex = 0;
|
||||
private static String CURRENT_INDEX = "current.index";
|
||||
private static final String CURRENT_INDEX = "current.index";
|
||||
|
||||
public CustomItemReader(List items) {
|
||||
public CustomItemReader(List<T> items) {
|
||||
this.items = items;
|
||||
}
|
||||
|
||||
public Object read() throws Exception, UnexpectedInputException,
|
||||
public T read() throws Exception, UnexpectedInputException,
|
||||
NoWorkFoundException, ParseException {
|
||||
|
||||
if (currentIndex < items.size()) {
|
||||
return items.get(currentIndex++);
|
||||
}
|
||||
return null;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public void mark() throws MarkFailedException {
|
||||
lastMarkedIndex = currentIndex;
|
||||
};
|
||||
}
|
||||
|
||||
public void reset() throws ResetFailedException {
|
||||
currentIndex = lastMarkedIndex;
|
||||
}
|
||||
|
||||
public void open(ExecutionContext executionContext) throws ItemStreamTException {
|
||||
public void open(ExecutionContext executionContext) throws ItemStreamException {
|
||||
|
||||
if(executionContext.containsKey(CURRENT_INDEX)){
|
||||
currentIndex = new Long(executionContext.getLong(CURRENT_INDEX)).intValue();
|
||||
}
|
||||
else{
|
||||
currentIndex = 0;
|
||||
lastMarkedIndex = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void close(ExecutionContext executionContext) throws ItemStreamException {}
|
||||
|
||||
public void update(ExecutionContext executionContext) throws ItemStreamException {
|
||||
executionContext.putLong(CURRENT_INDEX, new Long(currentIndex).longValue());
|
||||
};
|
||||
|
||||
public void close(ExecutionContext executionContext) throws ItemStreamException {}
|
||||
}</programlisting>
|
||||
|
||||
<para>On each call to <classname>ItemStream</classname>
|
||||
@@ -2494,11 +2496,11 @@
|
||||
assertEquals("1", itemReader.read());
|
||||
((ItemStream)itemReader).update(executionContext);
|
||||
|
||||
List items = new ArrayList();
|
||||
List<String> items = new ArrayList<String>();
|
||||
items.add("1");
|
||||
items.add("2");
|
||||
items.add("3");
|
||||
itemReader = new CustomItemReader(items);
|
||||
itemReader = new CustomItemReader<String>(items);
|
||||
|
||||
((ItemStream)itemReader).open(executionContext);
|
||||
assertEquals("2", itemReader.read());</programlisting>
|
||||
@@ -2532,11 +2534,11 @@
|
||||
example. As with the <classname>ItemReader</classname> example, a List
|
||||
will be used in order to keep the example as simple as possible:</para>
|
||||
|
||||
<programlisting> public class CustomItemWriter implements ItemWriter{
|
||||
<programlisting> public class CustomItemWriter<T> implements ItemWriter<T>{
|
||||
|
||||
List output = new ArrayList();
|
||||
List<T> output = new ArrayList<T>();
|
||||
|
||||
public void write(Object item) throws Exception {
|
||||
public void write(T item) throws Exception {
|
||||
output.add(item);
|
||||
}
|
||||
|
||||
@@ -2563,12 +2565,12 @@
|
||||
<methodname>flush</methodname> and <methodname>clear</methodname>
|
||||
should be implemented to allow for a buffering solution:</para>
|
||||
|
||||
<programlisting> public class CustomItemWriter implements ItemWriter{
|
||||
<programlisting> public class CustomItemWriter<T> implements ItemWriter<T>{
|
||||
|
||||
List output = new ArrayList();
|
||||
List buffer = new ArrayList();
|
||||
List<T> output = new ArrayList<T>();
|
||||
List<T> buffer = new ArrayList<T>();
|
||||
|
||||
public void write(Object item) throws Exception {
|
||||
public void write(T item) throws Exception {
|
||||
buffer.add(item);
|
||||
}
|
||||
|
||||
@@ -2577,9 +2579,8 @@
|
||||
}
|
||||
|
||||
public void flush() throws FlushFailedException {
|
||||
for(Iterator it = buffer.iterator(); it.hasNext();){
|
||||
output.add(it.next());
|
||||
it.remove();
|
||||
for(T t:buffer){
|
||||
output.add(t);
|
||||
}
|
||||
}
|
||||
}</programlisting>
|
||||
|
||||
Reference in New Issue
Block a user