diff --git a/spring-batch-samples/pom.xml b/spring-batch-samples/pom.xml index 73f4ea543..18d711087 100644 --- a/spring-batch-samples/pom.xml +++ b/spring-batch-samples/pom.xml @@ -219,6 +219,11 @@ mockito-all test + + javax.inject + javax.inject + 1 + diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/sample/jsr352/JsrSampleBatchlet.java b/spring-batch-samples/src/main/java/org/springframework/batch/sample/jsr352/JsrSampleBatchlet.java new file mode 100644 index 000000000..c4803c723 --- /dev/null +++ b/spring-batch-samples/src/main/java/org/springframework/batch/sample/jsr352/JsrSampleBatchlet.java @@ -0,0 +1,50 @@ +/* + * Copyright 2014 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.jsr352; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import javax.batch.api.AbstractBatchlet; +import javax.batch.api.BatchProperty; +import javax.inject.Inject; + +/** + *

+ * Sample {@link javax.batch.api.Batchlet} implementation. + *

+ * + * @since 3.0 + * @author Chris Schaefer + */ +public class JsrSampleBatchlet extends AbstractBatchlet { + private static final Log LOG = LogFactory.getLog(JsrSampleBatchlet.class); + + @Inject + @BatchProperty + private String remoteServiceURL; + + @Override + public String process() throws Exception { + LOG.info("Calling remote service at: " + remoteServiceURL); + + Thread.sleep(2000); + + LOG.info("Remote service call complete"); + + return null; + } +} diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/sample/jsr352/JsrSampleItemProcessor.java b/spring-batch-samples/src/main/java/org/springframework/batch/sample/jsr352/JsrSampleItemProcessor.java new file mode 100644 index 000000000..e8b7f2354 --- /dev/null +++ b/spring-batch-samples/src/main/java/org/springframework/batch/sample/jsr352/JsrSampleItemProcessor.java @@ -0,0 +1,42 @@ +/* + * Copyright 2014 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.jsr352; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import javax.batch.api.chunk.ItemProcessor; + +/** + *

+ * Sample {@link javax.batch.api.chunk.ItemProcessor} implementation. + *

+ * + * @since 3.0 + * @author Chris Schaefer + */ +public class JsrSampleItemProcessor implements ItemProcessor { + private static final Log LOG = LogFactory.getLog(JsrSampleItemProcessor.class); + + @Override + public Object processItem(Object o) throws Exception { + String person = (String) o; + + LOG.info("Transforming person: " + person + " to uppercase"); + + return person.toUpperCase(); + } +} diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/sample/jsr352/JsrSampleItemReader.java b/spring-batch-samples/src/main/java/org/springframework/batch/sample/jsr352/JsrSampleItemReader.java new file mode 100644 index 000000000..d434de484 --- /dev/null +++ b/spring-batch-samples/src/main/java/org/springframework/batch/sample/jsr352/JsrSampleItemReader.java @@ -0,0 +1,56 @@ +/* + * Copyright 2014 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.jsr352; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import javax.batch.api.chunk.AbstractItemReader; +import java.util.ArrayList; +import java.util.List; + +/** + *

+ * Sample {@link javax.batch.api.chunk.ItemReader} implementation. + *

+ * + * @since 3.0 + * @author Chris Schaefer + */ +public class JsrSampleItemReader extends AbstractItemReader { + private static final Log LOG = LogFactory.getLog(JsrSampleItemReader.class); + + private List people = new ArrayList() {{ + add("John"); + add("Joe"); + add("Mark"); + add("Jane"); + }}; + + @Override + public Object readItem() throws Exception { + String person = null; + + if(people.iterator().hasNext()) { + person = people.iterator().next(); + people.remove(person); + + LOG.info("Read person: " + person); + } + + return person; + } +} diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/sample/jsr352/JsrSampleItemWriter.java b/spring-batch-samples/src/main/java/org/springframework/batch/sample/jsr352/JsrSampleItemWriter.java new file mode 100644 index 000000000..10f076368 --- /dev/null +++ b/spring-batch-samples/src/main/java/org/springframework/batch/sample/jsr352/JsrSampleItemWriter.java @@ -0,0 +1,41 @@ +/* + * Copyright 2014 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.jsr352; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import javax.batch.api.chunk.AbstractItemWriter; +import java.util.List; + +/** + *

+ * Sample {@link javax.batch.api.chunk.ItemWriter} implementation. + *

+ * + * @since 3.0 + * @author Chris Schaefer + */ +public class JsrSampleItemWriter extends AbstractItemWriter { + private static final Log LOG = LogFactory.getLog(JsrSampleItemWriter.class); + + @Override + public void writeItems(List people) throws Exception { + for(Object person : people) { + LOG.info("Writing person: " + person); + } + } +} diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/sample/jsr352/JsrSampleTasklet.java b/spring-batch-samples/src/main/java/org/springframework/batch/sample/jsr352/JsrSampleTasklet.java new file mode 100644 index 000000000..267fcaff5 --- /dev/null +++ b/spring-batch-samples/src/main/java/org/springframework/batch/sample/jsr352/JsrSampleTasklet.java @@ -0,0 +1,53 @@ +/* + * Copyright 2014 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.jsr352; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.batch.core.StepContribution; +import org.springframework.batch.core.scope.context.ChunkContext; +import org.springframework.batch.core.step.tasklet.Tasklet; +import org.springframework.batch.repeat.RepeatStatus; + +import javax.batch.api.BatchProperty; +import javax.inject.Inject; + +/** + *

+ * Sample {@link org.springframework.batch.core.step.tasklet.Tasklet} implementation. + *

+ * + * @since 3.0 + * @author Chris Schaefer + */ +public class JsrSampleTasklet implements Tasklet { + private static final Log LOG = LogFactory.getLog(JsrSampleTasklet.class); + + @Inject + @BatchProperty + private String remoteServiceURL; + + @Override + public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception { + LOG.info("Calling remote service at: " + remoteServiceURL); + + Thread.sleep(2000); + + LOG.info("Remote service call complete"); + + return RepeatStatus.FINISHED; + } +} diff --git a/spring-batch-samples/src/main/resources/META-INF/batch-jobs/batchXmlConfigSample.xml b/spring-batch-samples/src/main/resources/META-INF/batch-jobs/batchXmlConfigSample.xml new file mode 100644 index 000000000..b62343d6c --- /dev/null +++ b/spring-batch-samples/src/main/resources/META-INF/batch-jobs/batchXmlConfigSample.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + diff --git a/spring-batch-samples/src/main/resources/META-INF/batch-jobs/inlineConfigSample.xml b/spring-batch-samples/src/main/resources/META-INF/batch-jobs/inlineConfigSample.xml new file mode 100644 index 000000000..d99b61db9 --- /dev/null +++ b/spring-batch-samples/src/main/resources/META-INF/batch-jobs/inlineConfigSample.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + diff --git a/spring-batch-samples/src/main/resources/META-INF/batch-jobs/springConfigSample.xml b/spring-batch-samples/src/main/resources/META-INF/batch-jobs/springConfigSample.xml new file mode 100644 index 000000000..0f7dcb300 --- /dev/null +++ b/spring-batch-samples/src/main/resources/META-INF/batch-jobs/springConfigSample.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + diff --git a/spring-batch-samples/src/main/resources/META-INF/batch-jobs/springConfigSampleContext.xml b/spring-batch-samples/src/main/resources/META-INF/batch-jobs/springConfigSampleContext.xml new file mode 100644 index 000000000..200dc34c0 --- /dev/null +++ b/spring-batch-samples/src/main/resources/META-INF/batch-jobs/springConfigSampleContext.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + John + Joe + Mark + Jane + + + + + + + + + + + diff --git a/spring-batch-samples/src/main/resources/META-INF/batch.xml b/spring-batch-samples/src/main/resources/META-INF/batch.xml new file mode 100644 index 000000000..47caaf4ff --- /dev/null +++ b/spring-batch-samples/src/main/resources/META-INF/batch.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/spring-batch-samples/src/main/resources/batch-hsql.properties b/spring-batch-samples/src/main/resources/batch-hsql.properties index 41423fa1d..2dd53dcdb 100644 --- a/spring-batch-samples/src/main/resources/batch-hsql.properties +++ b/spring-batch-samples/src/main/resources/batch-hsql.properties @@ -19,4 +19,4 @@ batch.jdbc.pool.size=6 batch.grid.size=6 batch.verify.cursor.position=true batch.isolationlevel=ISOLATION_SERIALIZABLE - +batch.data.source.init=true diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/jsr352/JsrConfigSampleTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/jsr352/JsrConfigSampleTests.java new file mode 100644 index 000000000..8af93f10a --- /dev/null +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/jsr352/JsrConfigSampleTests.java @@ -0,0 +1,104 @@ +/* + * Copyright 2014 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.jsr352; + +import org.junit.Before; +import org.junit.Test; + +import javax.batch.operations.JobOperator; +import javax.batch.runtime.BatchRuntime; +import javax.batch.runtime.BatchStatus; +import javax.batch.runtime.JobExecution; +import java.util.Properties; + +import static org.junit.Assert.assertTrue; + +/** + *

+ * Test cases to run JSR-352 configuration samples. + *

+ * + * @since 3.0 + * @author Chris Schaefer + */ +public class JsrConfigSampleTests { + private Properties properties = new Properties(); + + @Before + public void setup() { + properties.setProperty("remoteServiceURL", "http://api.example.com"); + } + + /** + *

+ * Use inline class names as batch artifact references. + *

+ */ + @Test + public void inlineConfigSampleTest() { + JobOperator jobOperator = BatchRuntime.getJobOperator(); + Long executionId = jobOperator.start("inlineConfigSample", properties); + + BatchStatus batchStatus = waitForJobComplete(jobOperator, executionId); + assertTrue(BatchStatus.COMPLETED.equals(batchStatus)); + } + + /** + *

+ * Use batch artifact references defined in batch.xml. + *

+ */ + @Test + public void batchXmlConfigSampleTest() { + JobOperator jobOperator = BatchRuntime.getJobOperator(); + Long executionId = jobOperator.start("batchXmlConfigSample", properties); + + BatchStatus batchStatus = waitForJobComplete(jobOperator, executionId); + assertTrue(BatchStatus.COMPLETED.equals(batchStatus)); + } + + /** + *

+ * Use batch artifact references defined via Spring beans. + *

+ */ + @Test + public void springConfigSampleTest() { + JobOperator jobOperator = BatchRuntime.getJobOperator(); + Long executionId = jobOperator.start("springConfigSampleContext", properties); + + BatchStatus batchStatus = waitForJobComplete(jobOperator, executionId); + assertTrue(BatchStatus.COMPLETED.equals(batchStatus)); + } + + // JSR jobs run async + private BatchStatus waitForJobComplete(JobOperator jobOperator, long executionId) { + JobExecution execution = jobOperator.getJobExecution(executionId); + + BatchStatus curBatchStatus = execution.getBatchStatus(); + + while(true) { + if(curBatchStatus == BatchStatus.STOPPED || curBatchStatus == BatchStatus.COMPLETED || curBatchStatus == BatchStatus.FAILED) { + break; + } + + execution = jobOperator.getJobExecution(executionId); + curBatchStatus = execution.getBatchStatus(); + } + + return curBatchStatus; + } +}