diff --git a/execution/src/main/java/org/springframework/batch/execution/tasklet/ReadProcessTasklet.java b/execution/src/main/java/org/springframework/batch/execution/tasklet/ReadProcessTasklet.java deleted file mode 100644 index 887d37e4b..000000000 --- a/execution/src/main/java/org/springframework/batch/execution/tasklet/ReadProcessTasklet.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright 2006-2007 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.batch.execution.tasklet; - -import org.springframework.batch.core.tasklet.Tasklet; -import org.springframework.batch.repeat.ExitStatus; - -/** - * Provides the basic batch module for reading and processing data. - * Implementations of this class will be handling both the input and output of - * data within one class. Developers should ensure that all reading is done - * before returning from the read() method. This is to ensure all data has been - * read first, before beginning to process. It is possibly detrimental to - * performance if processing begins when records still need to be read, because - * any writing of output will put the transaction in a volatile state, since - * errors with any additional input will need to cause a rollback, rather than - * simply skipping that record. - * - * @author Lucas Ward - * - */ -public abstract class ReadProcessTasklet implements Tasklet { - - /** - * Required for implementation of the {@link Tasklet} interface. The boolean returned - * from the abstract read method will be returned to the {@link Tasklet}, to indicate - * whether or not processing should continue. - */ - public final ExitStatus execute() throws Exception { - if (!read()) { - return ExitStatus.FINISHED; - } - process(); - return ExitStatus.CONTINUABLE; - } - - /** - * Abstract read method to be implemented by batch developers. All data - * should be read from within this method and a boolean indicated whether or - * not processing should continue should be returned. - * - * @return boolean indicating whether or not processing should continue. - */ - public abstract boolean read() throws Exception; - - /** - * Abstract process method to be implemented by batch developers. All - * processing and writing out of data should be done within this method. - */ - public abstract void process() throws Exception; -} diff --git a/samples/src/main/java/org/springframework/batch/sample/module/SimpleTradeTasklet.java b/samples/src/main/java/org/springframework/batch/sample/module/SimpleTradeTasklet.java index 51070b6f5..4c5eee662 100644 --- a/samples/src/main/java/org/springframework/batch/sample/module/SimpleTradeTasklet.java +++ b/samples/src/main/java/org/springframework/batch/sample/module/SimpleTradeTasklet.java @@ -18,28 +18,31 @@ package org.springframework.batch.sample.module; import java.util.Properties; -import org.springframework.batch.execution.tasklet.ReadProcessTasklet; +import org.springframework.batch.core.tasklet.Tasklet; +import org.springframework.batch.execution.tasklet.ItemProviderProcessTasklet; import org.springframework.batch.io.file.FieldSet; import org.springframework.batch.io.file.FieldSetMapper; import org.springframework.batch.io.file.support.DefaultFlatFileInputSource; +import org.springframework.batch.repeat.ExitStatus; import org.springframework.batch.sample.dao.TradeWriter; import org.springframework.batch.sample.domain.Trade; import org.springframework.batch.statistics.StatisticsProvider; /** - * Simple implementation of a {@link ReadProcessTasklet}, which illustrates the - * case when reading and processing of input is not separated. This can be - * viable in cases, when the input reading and processing logic need not to be - * reused in different contexts. In general it is recommended to separate these - * two concerns. + * Simple implementation of a {@link Tasklet}, which illustrates the reading + * and processing of input data. This can be viable in cases, when the input + * reading and processing logic need not to be reused in different contexts. In + * general it is recommended to separate these two concerns using an + * {@link ItemProviderProcessTasklet}. * - * Note this class is NOT thread-safe, contrast to 'standard' module - * implementations provided by the framework. + * Note this class is thread-safe, as per the 'standard' module implementations + * provided by the framework. * * @author Robert Kasanicky * @author Lucas Ward + * @author Dave Syer */ -public class SimpleTradeTasklet extends ReadProcessTasklet implements StatisticsProvider { +public class SimpleTradeTasklet implements Tasklet, StatisticsProvider { /** * reads the data from input file */ @@ -66,31 +69,22 @@ public class SimpleTradeTasklet extends ReadProcessTasklet implements Statistics private int tradeCount = 0; /** - * Read method, all reading from any input source(s) should be done here. * The input template is read using the readAndMap method, which accepts a - * FieldSetMapper. This call returns an object (which should be a Trade - * value object) then will be stored in a class-level variable for use by - * the process method. + * FieldSetMapper. This call returns a Trade object, which is then + * processed. Because this is a simple example job, the data is simply + * written out without any processing. */ - public boolean read() { + public ExitStatus execute() throws Exception { trade = (Trade) tradeFieldSetMapper.mapLine(inputSource.readFieldSet()); if (trade == null) { // no Trade object returned, reading input is finished - return false; + return ExitStatus.FINISHED; } tradeCount++; - return true; - } - - /** - * Process the data obtained during the read() method. Because this is a - * simple example job, the data is simply written out without any - * processing. - */ - public void process() { tradeWriter.writeTrade(trade); + return ExitStatus.CONTINUABLE; } /** diff --git a/samples/src/test/java/org/springframework/batch/sample/module/SimpleTradeTaskletTests.java b/samples/src/test/java/org/springframework/batch/sample/module/SimpleTradeTaskletTests.java index a4f62c817..d9a4195d0 100644 --- a/samples/src/test/java/org/springframework/batch/sample/module/SimpleTradeTaskletTests.java +++ b/samples/src/test/java/org/springframework/batch/sample/module/SimpleTradeTaskletTests.java @@ -14,7 +14,7 @@ public class SimpleTradeTaskletTests extends TestCase { private boolean inputCalled = false; private boolean writerCalled = false; - public void testReadAndProcess() { + public void testReadAndProcess() throws Exception { //create input DefaultFlatFileInputSource input = new DefaultFlatFileInputSource() { @@ -55,15 +55,13 @@ public class SimpleTradeTaskletTests extends TestCase { //call tested methods //read method should return true, because input returned fieldset - assertTrue(module.read()); - //call process method - see asserts in writer.writeTrade() - module.process(); + assertTrue(module.execute().isContinuable()); //verify whether input and writer were called assertTrue(inputCalled); assertTrue(writerCalled); //read should return false, because input returned null - assertFalse(module.read()); + assertFalse(module.execute().isContinuable()); } }