Add basic JSR-352 configuration samples

* Inline class references
* batch.xml references
* Spring bean references
This commit is contained in:
Chris Schaefer
2014-04-16 17:54:30 -04:00
parent 9d17a3d088
commit ab293b57fb
13 changed files with 438 additions and 1 deletions

View File

@@ -219,6 +219,11 @@
<artifactId>mockito-all</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
</dependencies>
<build>
<plugins>

View File

@@ -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;
/**
* <p>
* Sample {@link javax.batch.api.Batchlet} implementation.
* </p>
*
* @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;
}
}

View File

@@ -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;
/**
* <p>
* Sample {@link javax.batch.api.chunk.ItemProcessor} implementation.
* </p>
*
* @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();
}
}

View File

@@ -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;
/**
* <p>
* Sample {@link javax.batch.api.chunk.ItemReader} implementation.
* </p>
*
* @since 3.0
* @author Chris Schaefer
*/
public class JsrSampleItemReader extends AbstractItemReader {
private static final Log LOG = LogFactory.getLog(JsrSampleItemReader.class);
private List<String> people = new ArrayList<String>() {{
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;
}
}

View File

@@ -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;
/**
* <p>
* Sample {@link javax.batch.api.chunk.ItemWriter} implementation.
* </p>
*
* @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<Object> people) throws Exception {
for(Object person : people) {
LOG.info("Writing person: " + person);
}
}
}

View File

@@ -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;
/**
* <p>
* Sample {@link org.springframework.batch.core.step.tasklet.Tasklet} implementation.
* </p>
*
* @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;
}
}

View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<job id="batchXmlConfigSample" xmlns="http://xmlns.jcp.org/xml/ns/javaee" version="1.0">
<step id="step1" next="step2">
<chunk>
<reader ref="reader"/>
<processor ref="processor"/>
<writer ref="writer"/>
</chunk>
</step>
<step id="step2">
<batchlet ref="batchlet">
<properties>
<property name="remoteServiceURL" value="#{jobParameters['remoteServiceURL']}"/>
</properties>
</batchlet>
</step>
</job>

View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<job id="inlineConfigSample" xmlns="http://xmlns.jcp.org/xml/ns/javaee" version="1.0">
<step id="step1" next="step2">
<chunk>
<reader ref="org.springframework.batch.sample.jsr352.JsrSampleItemReader"/>
<processor ref="org.springframework.batch.sample.jsr352.JsrSampleItemProcessor"/>
<writer ref="org.springframework.batch.sample.jsr352.JsrSampleItemWriter"/>
</chunk>
</step>
<step id="step2">
<batchlet ref="org.springframework.batch.sample.jsr352.JsrSampleBatchlet">
<properties>
<property name="remoteServiceURL" value="#{jobParameters['remoteServiceURL']}"/>
</properties>
</batchlet>
</step>
</job>

View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<job id="springConfigSample" xmlns="http://xmlns.jcp.org/xml/ns/javaee" version="1.0">
<step id="step1" next="step2">
<chunk>
<reader ref="itemReader"/>
<processor ref="itemProcessor"/>
<writer ref="itemWriter"/>
</chunk>
</step>
<step id="step2">
<batchlet ref="serviceBatchet">
<properties>
<property name="remoteServiceURL" value="#{jobParameters['remoteServiceURL']}"/>
</properties>
</batchlet>
</step>
</job>

View File

@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- Use different ref names to avoid picking up ref's with the same name from batch.xml if desired -->
<import resource="springConfigSample.xml"/>
<!-- Plug in an existing Spring Batch ItemReader component as your JSR-352 ItemReader -->
<bean id="itemReader" class="org.springframework.batch.item.support.ListItemReader" scope="step">
<constructor-arg>
<list>
<value>John</value>
<value>Joe</value>
<value>Mark</value>
<value>Jane</value>
</list>
</constructor-arg>
</bean>
<!-- Mix in JSR-352 ItemProcessor and ItemWriter implementations -->
<bean id="itemProcessor" class="org.springframework.batch.sample.jsr352.JsrSampleItemProcessor" scope="step"/>
<bean id="itemWriter" class="org.springframework.batch.sample.jsr352.JsrSampleItemWriter" scope="step"/>
<!-- Plug in an existing Spring Batch Tasklet to use for your JSR-352 Batchlet -->
<bean id="serviceBatchet" class="org.springframework.batch.sample.jsr352.JsrSampleTasklet" scope="step"/>
</beans>

View File

@@ -0,0 +1,6 @@
<batch-artifacts xmlns="http://xmlns.jcp.org/xml/ns/javaee">
<ref id="reader" class="org.springframework.batch.sample.jsr352.JsrSampleItemReader"/>
<ref id="processor" class="org.springframework.batch.sample.jsr352.JsrSampleItemProcessor"/>
<ref id="writer" class="org.springframework.batch.sample.jsr352.JsrSampleItemWriter"/>
<ref id="batchlet" class="org.springframework.batch.sample.jsr352.JsrSampleBatchlet"/>
</batch-artifacts>

View File

@@ -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

View File

@@ -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;
/**
* <p>
* Test cases to run JSR-352 configuration samples.
* </p>
*
* @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");
}
/**
* <p>
* Use inline class names as batch artifact references.
* </p>
*/
@Test
public void inlineConfigSampleTest() {
JobOperator jobOperator = BatchRuntime.getJobOperator();
Long executionId = jobOperator.start("inlineConfigSample", properties);
BatchStatus batchStatus = waitForJobComplete(jobOperator, executionId);
assertTrue(BatchStatus.COMPLETED.equals(batchStatus));
}
/**
* <p>
* Use batch artifact references defined in batch.xml.
* </p>
*/
@Test
public void batchXmlConfigSampleTest() {
JobOperator jobOperator = BatchRuntime.getJobOperator();
Long executionId = jobOperator.start("batchXmlConfigSample", properties);
BatchStatus batchStatus = waitForJobComplete(jobOperator, executionId);
assertTrue(BatchStatus.COMPLETED.equals(batchStatus));
}
/**
* <p>
* Use batch artifact references defined via Spring beans.
* </p>
*/
@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;
}
}