Kohei Nozaki's blog 

JBatch examples: get started JBatch with WildFly and batchee-servlet-embedded


Posted on Sunday May 24, 2015 at 12:43PM in JBatch


JSR352 aka JBatch is the standardized batch processing framework for the Java EE platform. it eases tedious work on batch programming such as transaction management of bulk processing, parallel processing, flow control. and it gives well-integrated job information management mechanism, well-designed interfaces that enables us to develop common modules for frequently use. there are some convenient modules aim to be used in typical situation. in this entry, I introduce you some examples to get started.

Setup

Setup WildFly 9.0.0.CR1: download the full distribution from wildfly.org and unpack.

Next, create a war application contains following resources:

pom.xml

This contains a dependency to batchee-servlet-embedded. it brings a simple web application which enables us to control batch jobs, also it supplies simple REST style interface.

<?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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>jbatch-example</groupId>
    <artifactId>jbatch-example</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <failOnMissingWebXml>false</failOnMissingWebXml>
    </properties>

    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.batchee</groupId>
            <artifactId>batchee-servlet-embedded</artifactId>
            <version>0.2-incubating</version>
        </dependency>
    </dependencies>

</project>

/src/main/java/jbatch/MyBatchlet.java

@Named
@Dependent
public class MyBatchlet extends AbstractBatchlet {
    @Override
    public String process() throws Exception {
        System.out.println("Hello, JBatch");
        return null;
    }
}

/src/main/resources/META-INF/batch-jobs/simple-job.xml

<job id="simple-job" version="1.0" xmlns="http://xmlns.jcp.org/xml/ns/javaee">
    <step id="myStep">
        <batchlet ref="myBatchlet"/>
    </step>
</job>

Deploy and run the batch

Then deploy the war, and go to http://localhost:8080/jbatch-example-1.0-SNAPSHOT/jbatch/ from your browser. you’ll see following page:

26c69072 c35c 4885 b8ab cb7c8445c7ac

Then click New Batch button. you’ll be transited to following page. enter simple-job to the text box, then click Set Job Name, and click Submit

41f38684 4e35 4e09 8463 28b81ae49f92

If the batch executed successfully, you’ll be transited to following page:

5913a110 1457 483b 878b d8d08f2520a2

Also you’ll see following output in your WildFly console:

12:23:02,046 INFO  [stdout] (Batch Thread - 2) Hello, JBatch

You can see job execution history from the web. click simple-job in home page and you’ll be transited following page:

e2f7d3c1 c52b 4261 9727 a3a22c1277ce

Instead of using web browser, you can launch a job with simple REST style API as follows:

curl http://localhost:8080/jbatch-example-1.0-SNAPSHOT/jbatch/rest/start/simple-job

For details of the REST API, you can see help with following command:

curl http://localhost:8080/jbatch-example-1.0-SNAPSHOT/jbatch/rest/

It shows:

Known commands are:

* start/ - start a new batch job
  Sample: http://localhost:8080/myapp/jbatch/rest/start/myjobname?param1=x&param2=y
  BatchEE will start the job and immediately return

* status/ - query the current status
  Sample: http://localhost:8080/myapp/jbatch/rest/status/23
  will return the state of executionId 23

* stop/ - stop the job with the given executionId
  Sample: http://localhost:8080/myapp/jbatch/rest/stop/23
  will stop the job with executionId 23

* restart/ - restart the job with the given executionId
  Sample: http://localhost:8080/myapp/jbatch/rest/restart/23
  will restart the job with executionId 23

The project which used in this entry can be obtained from my GitHub repository.



No one has commented yet.

Leave a Comment

HTML Syntax: NOT allowed