How to Create a Task-Oriented Batch with the Java Batch API

BatchSample.java

import static java.lang.System.out;

import javax.batch.operations.JobOperator;
import javax.batch.runtime.BatchRuntime;

public final class BatchSample {

  public static void main(final String[] args) throws Exception {
    final JobOperator jobOperator = BatchRuntime.getJobOperator();

    out.println(jobOperator.start("samplejob", null));
  }

}

SampleBatchlet.java

import javax.batch.api.AbstractBatchlet;

public final class SampleBatchlet extends AbstractBatchlet {

  public String process() {
    return "COMPLETED";
  }

}

META-INF/batch-jobs/samplejob.xml

<?xml version="1.0" encoding="UTF-8"?>
<job xmlns="http://xmlns.jcp.org/xml/ns/javaee" id="samplejob">
  <step id="step1">
    <batchlet ref="sampleBatchlet" />
    <next on="COMPLETED" to="step2" /> <!-- go the "step2" if the exit status is "COMPLETED" -->
  </step>
  <step id="step2">
    ...
  </step>
</job>

META-INF/batch.xml

<?xml version="1.0" encoding="UTF-8"?>
<batch-artifacts xmlns="http://xmlns.jcp.org/xml/ns/javaee">
  <ref id="sampleBatchlet" class="SampleBatchlet" />
</batch-artifacts>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s