Created
May 28, 2015 14:00
-
-
Save pz78/732108fdf290e8397247 to your computer and use it in GitHub Desktop.
JSR-352 batch with Spring Batch as implementation: Batch job initialization fails on error while creating bean with name 'batchPropertyPostProcessor'
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package x98.batch; | |
import java.util.Properties; | |
import javax.batch.operations.JobOperator; | |
import javax.batch.runtime.BatchRuntime; | |
public class BatchRunner { | |
public static void main(String[] args) { | |
System.setProperty("JSR-352-BASE-CONTEXT", "x98_batch_local.xml"); | |
Properties jobParameters = new Properties(); | |
jobParameters.put("message", "Hello!"); | |
JobOperator jobOperator = BatchRuntime.getJobOperator(); | |
jobOperator.start("x98SampleJob", jobParameters); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package x98.batch; | |
import javax.batch.api.BatchProperty; | |
import javax.batch.api.chunk.ItemProcessor; | |
import javax.inject.Inject; | |
public class DataListingItemProcessor implements ItemProcessor { | |
@Inject | |
@BatchProperty(name = "forceFailAtRecord") | |
private String forceFailAtRecord = "-1"; | |
private int processedCount = 0; | |
@Override | |
public Object processItem(Object item) throws Exception { | |
if (this.processedCount == Integer.valueOf(this.forceFailAtRecord)) { | |
throw new RuntimeException("Forced fail!"); | |
} | |
this.processedCount++; | |
return item; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package x98.batch; | |
import java.io.Serializable; | |
import javax.batch.api.chunk.ItemReader; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
public class DataListingItemReader implements ItemReader { | |
private static final Logger LOGGER = LoggerFactory.getLogger(DataListingItemReader.class); | |
private Integer countOfGeneratedNumbers = 0; | |
@Override | |
public void open(Serializable checkpoint) throws Exception { | |
} | |
@Override | |
public void close() throws Exception { | |
countOfGeneratedNumbers = 0; | |
} | |
@Override | |
public Object readItem() throws Exception { | |
countOfGeneratedNumbers++; | |
Double generatedValue = null; | |
if (countOfGeneratedNumbers < 100) { | |
LOGGER.info("Generating Double #" + countOfGeneratedNumbers); | |
generatedValue = Math.random(); | |
} | |
return generatedValue; | |
} | |
@Override | |
public Serializable checkpointInfo() throws Exception { | |
return countOfGeneratedNumbers; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package x98.batch; | |
import java.io.Serializable; | |
import java.util.List; | |
import javax.batch.api.chunk.ItemWriter; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
public class DataListingItemWriter implements ItemWriter { | |
private static final Logger LOGGER = LoggerFactory.getLogger(DataListingItemWriter.class); | |
private Serializable checkpointInfo; | |
@Override | |
public void open(Serializable checkpoint) throws Exception { | |
// nothing to do | |
} | |
@Override | |
public void close() throws Exception { | |
// nothing to do | |
} | |
@Override | |
public void writeItems(List<Object> items) throws Exception { | |
for (Object item : items) { | |
LOGGER.info("Generated Double: " + item); | |
} | |
} | |
@Override | |
public Serializable checkpointInfo() throws Exception { | |
return checkpointInfo; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package x98.batch; | |
import javax.batch.api.listener.StepListener; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
public class DataListingStepListener implements StepListener { | |
private static final Logger LOGGER = LoggerFactory.getLogger(DataListingStepListener.class); | |
@Override | |
public void beforeStep() throws Exception { | |
LOGGER.info("Before step..."); | |
} | |
@Override | |
public void afterStep() throws Exception { | |
LOGGER.info("After step..."); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>x98</groupId> | |
<artifactId>x98-sample-batch</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<name>${project.artifactId}</name> | |
<dependencies> | |
<dependency> | |
<groupId>org.springframework.batch</groupId> | |
<artifactId>spring-batch-core</artifactId> | |
<version>3.0.4.RELEASE</version> | |
<scope>compile</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-jdbc</artifactId> | |
<version>4.0.5.RELEASE</version> | |
<scope>compile</scope> | |
</dependency> | |
<dependency> | |
<groupId>javax.batch</groupId> | |
<artifactId>javax.batch-api</artifactId> | |
<version>1.0</version> | |
<scope>provided</scope> | |
</dependency> | |
<dependency> | |
<groupId>javax</groupId> | |
<artifactId>javaee-web-api</artifactId> | |
<version>7.0</version> | |
<scope>compile</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.slf4j</groupId> | |
<artifactId>slf4j-api</artifactId> | |
<version>1.7.8</version> | |
<scope>compile</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.slf4j</groupId> | |
<artifactId>slf4j-jdk14</artifactId> | |
<version>1.7.8</version> | |
<scope>compile</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.hsqldb</groupId> | |
<artifactId>hsqldb</artifactId> | |
<version>2.3.2</version> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>commons-dbcp</groupId> | |
<artifactId>commons-dbcp</artifactId> | |
<version>1.2.2</version> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<version>3.1</version> | |
<configuration> | |
<source>1.8</source> | |
<target>1.8</target> | |
</configuration> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package x98.batch; | |
import javax.batch.api.BatchProperty; | |
import javax.batch.api.Batchlet; | |
import javax.inject.Inject; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
public class SimpleBatchlet implements Batchlet { | |
private static Logger LOGGER = LoggerFactory.getLogger(SimpleBatchlet.class); | |
@Inject | |
@BatchProperty(name="message") | |
private String message; | |
@Override | |
public String process() throws Exception { | |
LOGGER.info("Running batchlet step in X98 template"); | |
LOGGER.info("Message is: " + message); | |
return "OK"; | |
} | |
@Override | |
public void stop() throws Exception { | |
// finishing fast, not needed to implement stop | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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"> | |
<bean name="simpleBatchlet" class="x98.batch.SimpleBatchlet"/> | |
<bean name="dataListingItemProcessor" class="x98.batch.DataListingItemProcessor"/> | |
<bean name="dataListingItemReader" class="x98.batch.DataListingItemReader"/> | |
<bean name="dataListingItemWriter" class="x98.batch.DataListingItemWriter"/> | |
<bean name="dataListingStepListener" class="x98.batch.DataListingStepListener"/> | |
</beans> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<job id="x98SampleJob" xmlns="http://xmlns.jcp.org/xml/ns/javaee" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/jobXML_1_0.xsd" | |
version="1.0"> | |
<step id="simple" next="dataListing"> | |
<batchlet ref="simpleBatchlet"> | |
<properties> | |
<property name="message" value="#{jobParameters['message']}" /> | |
</properties> | |
</batchlet> | |
</step> | |
<step id="dataListing"> | |
<listeners> | |
<listener ref="dataListingStepListener"/> | |
</listeners> | |
<chunk item-count="3"> | |
<reader ref="dataListingItemReader" /> | |
<processor ref="dataListingItemProcessor"> | |
<properties> | |
<property name="forceFailAtRecord" value="20"/> | |
</properties> | |
</processor> | |
<writer ref="dataListingItemWriter"/> | |
</chunk> | |
</step> | |
</job> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment