diff --git a/samples/.classpath b/samples/.classpath index 65de75cb7..8c5ea1d1b 100644 --- a/samples/.classpath +++ b/samples/.classpath @@ -1,10 +1,53 @@ - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/samples/.project b/samples/.project index c9e918e64..bb428d2f7 100644 --- a/samples/.project +++ b/samples/.project @@ -1,47 +1,39 @@ - - - batch-samples - - - - - - com.ibm.etools.common.migration.MigrationBuilder - - - - - org.eclipse.wst.common.project.facet.core.builder - - - - - org.eclipse.jdt.core.javabuilder - - - - - org.eclipse.wst.validation.validationbuilder - - - - - org.springframework.ide.eclipse.core.springbuilder - - - - - org.maven.ide.eclipse.maven2Builder - - - - - - org.eclipse.jdt.core.javanature - org.maven.ide.eclipse.maven2Nature - org.springframework.ide.eclipse.core.springnature - org.eclipse.wst.common.project.facet.core.nature - org.eclipse.wst.common.modulecore.ModuleCoreNature - org.eclipse.jem.workbench.JavaEMFNature - - + + spring-batch-samples + Example batch jobs using Spring Batch Core and Execution. + + + + com.ibm.etools.common.migration.MigrationBuilder + + + + org.eclipse.wst.common.project.facet.core.builder + + + + org.eclipse.jdt.core.javabuilder + + + + org.eclipse.wst.validation.validationbuilder + + + + org.springframework.ide.eclipse.core.springbuilder + + + + org.maven.ide.eclipse.maven2Builder + + + + + org.eclipse.jdt.core.javanature + org.maven.ide.eclipse.maven2Nature + org.springframework.ide.eclipse.core.springnature + org.eclipse.wst.common.project.facet.core.nature + org.eclipse.wst.common.modulecore.ModuleCoreNature + org.eclipse.jem.workbench.JavaEMFNature + + \ No newline at end of file diff --git a/samples/src/main/java/org/springframework/batch/sample/item/provider/CollectionItemProvider.java b/samples/src/main/java/org/springframework/batch/sample/item/provider/CollectionItemProvider.java index 642bc031a..395f61083 100644 --- a/samples/src/main/java/org/springframework/batch/sample/item/provider/CollectionItemProvider.java +++ b/samples/src/main/java/org/springframework/batch/sample/item/provider/CollectionItemProvider.java @@ -1,120 +1,120 @@ -/* - * 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.sample.item.provider; - -import java.util.ArrayList; -import java.util.Collection; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.springframework.batch.io.InputSource; -import org.springframework.batch.io.file.FieldSet; -import org.springframework.batch.io.file.FieldSetMapper; -import org.springframework.batch.item.ItemProvider; -import org.springframework.batch.item.provider.AbstractItemProvider; - -/** - * An {@link ItemProvider} that delivers a list as its item, storing up objects - * from the injected {@link InputSource} until they are ready to be packed out - * as a collection.
- * - * This class is thread safe (it can be used concurrently by multiple threads) as - * long as the {@link InputSource} is also thread safe. - * - * @author Dave Syer - * - */ -public class CollectionItemProvider extends AbstractItemProvider { - - private static final Log log = LogFactory - .getLog(CollectionItemProvider.class); - - private InputSource inputSource; - - // maps a single line to a simple record - private FieldSetMapper fieldSetMapper; - - /** - * Get the next list of records. - * - * @see org.springframework.batch.item.ItemProvider#next() - */ - public Object next() { - ResultHolder holder = new ResultHolder(); - - while (process((FieldSet)inputSource.read(), holder)) { - continue; - } - - if (!holder.exhausted) { - return holder.records; - } else { - return null; - } - } - - private boolean process(FieldSet fieldSet, ResultHolder holder) { - // finish processing if we hit the end of file - if (fieldSet == null) { - log.debug("Exhausted InputSource"); - holder.exhausted = true; - return false; - } - - // start a new collection - if (fieldSet.readString(0).equals("BEGIN")) { - log.debug("Start of new record detected"); - return true; - } - - // mark we are finished with current collection - if (fieldSet.readString(0).equals("END")) { - log.debug("End of record detected"); - return false; - } - - // add a simple record to the current collection - log.debug("Mapping: " + fieldSet); - holder.records.add(fieldSetMapper.mapLine(fieldSet)); - return true; - } - - /** - * Injection setter for {@link InputSource}. - * @param inputSource an {@link InputSource}. - */ - public void setInputSource(InputSource inputSource) { - this.inputSource = inputSource; - } - - public void setFieldSetMapper(FieldSetMapper mapper) { - this.fieldSetMapper = mapper; - } - - /** - * Private class for temporary state management while item is being - * collected. - * - * @author Dave Syer - * - */ - private class ResultHolder { - Collection records = new ArrayList(); - boolean exhausted = false; - } - -} +/* + * 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.sample.item.provider; + +import java.util.ArrayList; +import java.util.Collection; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.batch.io.InputSource; +import org.springframework.batch.io.file.FieldSet; +import org.springframework.batch.io.file.FieldSetMapper; +import org.springframework.batch.item.ItemProvider; +import org.springframework.batch.item.provider.AbstractItemProvider; + +/** + * An {@link ItemProvider} that delivers a list as its item, storing up objects + * from the injected {@link InputSource} until they are ready to be packed out + * as a collection.
+ * + * This class is thread safe (it can be used concurrently by multiple threads) as + * long as the {@link InputSource} is also thread safe. + * + * @author Dave Syer + * + */ +public class CollectionItemProvider extends AbstractItemProvider { + + private static final Log log = LogFactory + .getLog(CollectionItemProvider.class); + + private InputSource inputSource; + + // maps a single line to a simple record + private FieldSetMapper fieldSetMapper; + + /** + * Get the next list of records. + * + * @see org.springframework.batch.item.ItemProvider#next() + */ + public Object next() { + ResultHolder holder = new ResultHolder(); + + while (process((FieldSet)inputSource.read(), holder)) { + continue; + } + + if (!holder.exhausted) { + return holder.records; + } else { + return null; + } + } + + private boolean process(FieldSet fieldSet, ResultHolder holder) { + // finish processing if we hit the end of file + if (fieldSet == null) { + log.debug("Exhausted InputSource"); + holder.exhausted = true; + return false; + } + + // start a new collection + if (fieldSet.readString(0).equals("BEGIN")) { + log.debug("Start of new record detected"); + return true; + } + + // mark we are finished with current collection + if (fieldSet.readString(0).equals("END")) { + log.debug("End of record detected"); + return false; + } + + // add a simple record to the current collection + log.debug("Mapping: " + fieldSet); + holder.records.add(fieldSetMapper.mapLine(fieldSet)); + return true; + } + + /** + * Injection setter for {@link InputSource}. + * @param inputSource an {@link InputSource}. + */ + public void setInputSource(InputSource inputSource) { + this.inputSource = inputSource; + } + + public void setFieldSetMapper(FieldSetMapper mapper) { + this.fieldSetMapper = mapper; + } + + /** + * Private class for temporary state management while item is being + * collected. + * + * @author Dave Syer + * + */ + private static class ResultHolder { + Collection records = new ArrayList(); + boolean exhausted = false; + } + +} diff --git a/samples/src/main/resources/jobs/ibatisJob.xml b/samples/src/main/resources/jobs/ibatisJob.xml index 31e16d895..712aa8905 100644 --- a/samples/src/main/resources/jobs/ibatisJob.xml +++ b/samples/src/main/resources/jobs/ibatisJob.xml @@ -5,12 +5,13 @@ xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd - http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> + http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd" > Example for iBATIS integration. + diff --git a/samples/src/main/resources/simple-container-definition.xml b/samples/src/main/resources/simple-container-definition.xml index f14139453..c546e088a 100644 --- a/samples/src/main/resources/simple-container-definition.xml +++ b/samples/src/main/resources/simple-container-definition.xml @@ -28,6 +28,7 @@ + diff --git a/samples/src/test/java/org/springframework/batch/sample/MultilineJobFunctionalTests.java b/samples/src/test/java/org/springframework/batch/sample/MultilineJobFunctionalTests.java index 0e4b80ecf..4e172c071 100644 --- a/samples/src/test/java/org/springframework/batch/sample/MultilineJobFunctionalTests.java +++ b/samples/src/test/java/org/springframework/batch/sample/MultilineJobFunctionalTests.java @@ -1,40 +1,40 @@ -/* - * 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.sample; - -import org.apache.commons.io.IOUtils; -import org.springframework.core.io.FileSystemResource; -import org.springframework.core.io.Resource; -import org.springframework.util.StringUtils; - -public class MultilineJobFunctionalTests extends AbstractLifecycleSpringContextTests { - - private final String EXPECTED_RESULT = - "[Trade: [isin=UK21341EAH45,quantity=978,price=98.34,customer=customer1], Trade: [isin=UK21341EAH46,quantity=112,price=18.12,customer=customer2]]" + - "[Trade: [isin=UK21341EAH47,quantity=245,price=12.78,customer=customer2], Trade: [isin=UK21341EAH48,quantity=108,price=9.25,customer=customer3], Trade: [isin=UK21341EAH49,quantity=854,price=23.39,customer=customer4]]"; - - private Resource output = new FileSystemResource("20070122.testStream.multilineStep.txt"); - -// @Override - protected String[] getConfigLocations() { - return new String[] {"jobs/multilineJob.xml"}; - } - - protected void validatePostConditions() throws Exception { - assertEquals(EXPECTED_RESULT, StringUtils.replace(IOUtils.toString(output.getInputStream()), System.getProperty("line.separator"), "")); - } -} +/* + * 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.sample; + +import org.apache.commons.io.IOUtils; +import org.springframework.core.io.FileSystemResource; +import org.springframework.core.io.Resource; +import org.springframework.util.StringUtils; + +public class MultilineJobFunctionalTests extends AbstractLifecycleSpringContextTests { + + private static final String EXPECTED_RESULT = + "[Trade: [isin=UK21341EAH45,quantity=978,price=98.34,customer=customer1], Trade: [isin=UK21341EAH46,quantity=112,price=18.12,customer=customer2]]" + + "[Trade: [isin=UK21341EAH47,quantity=245,price=12.78,customer=customer2], Trade: [isin=UK21341EAH48,quantity=108,price=9.25,customer=customer3], Trade: [isin=UK21341EAH49,quantity=854,price=23.39,customer=customer4]]"; + + private Resource output = new FileSystemResource("20070122.testStream.multilineStep.txt"); + +// @Override + protected String[] getConfigLocations() { + return new String[] {"jobs/multilineJob.xml"}; + } + + protected void validatePostConditions() throws Exception { + assertEquals(EXPECTED_RESULT, StringUtils.replace(IOUtils.toString(output.getInputStream()), System.getProperty("line.separator"), "")); + } +} diff --git a/samples/src/test/java/org/springframework/batch/sample/TradeJobFunctionalTests.java b/samples/src/test/java/org/springframework/batch/sample/TradeJobFunctionalTests.java index 8a4154d84..a433bcbae 100644 --- a/samples/src/test/java/org/springframework/batch/sample/TradeJobFunctionalTests.java +++ b/samples/src/test/java/org/springframework/batch/sample/TradeJobFunctionalTests.java @@ -119,7 +119,7 @@ public class TradeJobFunctionalTests extends AbstractLifecycleSpringContextTests assertTrue(((Resource)applicationContext.getBean("fileLocator")).exists()); } - private class Customer { + private static class Customer { private String name; private double credit; diff --git a/samples/src/test/java/org/springframework/batch/sample/dao/SqlNflGameDaoIntegrationTests.java b/samples/src/test/java/org/springframework/batch/sample/dao/SqlNflGameDaoIntegrationTests.java index 4639b474c..311817532 100644 --- a/samples/src/test/java/org/springframework/batch/sample/dao/SqlNflGameDaoIntegrationTests.java +++ b/samples/src/test/java/org/springframework/batch/sample/dao/SqlNflGameDaoIntegrationTests.java @@ -67,7 +67,7 @@ public class SqlNflGameDaoIntegrationTests extends assertEquals(tempGame, game); } - public class NflGameRowMapper implements RowMapper { + public static class NflGameRowMapper implements RowMapper { public Object mapRow(ResultSet rs, int arg1) throws SQLException{