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

BatchSample.java

import static java.lang.System.out;

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

public final class BatchSample {

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

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

}

SampleItemReader.java

import javax.batch.api.AbstractItemReader;

public final class SampleItemReader extends AbstractItemReader {

  public Object readItem() {
    return null;
  }

}

SampleItemProcessor.java

import javax.batch.api.ItemProcessor;

public final class SampleItemProcessor implements ItemProcessor {

  public Object processItem(final Object item) {
    return null;
  }

}

SampleItemWriter.java

import javax.batch.api.AbstractItemWriter;

import java.util.List;

public final class SampleItemWriter extends AbstractItemWriter {

  public void writeItems(final List items) {
  }

}

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">
    <chunk>
      <reader ref="sampleItemReader" />
      <processor ref="sampleItemProcessor" /> <!-- optional -->
      <writer ref="sampleItemWriter" />
    </chunk>
    <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="sampleItemReader" class="SampleItemReader" />
  <ref id="sampleItemProcessor" class="SampleItemProcessor" />
  <ref id="sampleItemWriter" class="SampleItemWriter" />
</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