Batch Split

Run
How to run the sample
The source code for this sample can be found in the javaee7-samples GitHub repository. The first thing we need to do is to get the source by downloading the repository and then go into the samples folder:
git clone git://github.com/javaee-samples/javaee7-samples.git
cd javaee7-samples/batch/split/
Now we are ready to start testing. You can run all the tests in this sample by executing:
mvn test
Or you can run individual tests by executing one of the following:
mvn test -Dtest=BatchSplitTest

Batch JSL - Splitting Steps

BatchSplitTest

The Batch specification allows you to implement process workflow using a Job Specification Language (JSL). In this sample, by using the split element, it’s possible to configure a job that runs parallel flows. A split can only contain flow elements. These flow elements can be used to implement separate executions to be processed by the job.

Three simple Batchlet’s are configured in the file myJob.xml. MyBatchlet1 and MyBatchlet2 are setted up to execute in parallel by using the split and flow elements. MyBatchlet3 is only going to execute after MyBatchlet1 and MyBatchlet2 are both done with their job.

<?xml version="1.0" encoding="UTF-8"?>
<job id="myJob" xmlns="http://xmlns.jcp.org/xml/ns/javaee" version="1.0">
    <split id="split1" next="step3">
        <flow id="flow1">
            <step id="step1">
                <batchlet ref="myBatchlet1"/>
            </step>
        </flow>
        <flow id="flow2">
            <step id="step2">
                <batchlet ref="myBatchlet2"/>
            </step>
        </flow>
    </split>
    <step id="step3">
        <batchlet ref="myBatchlet3"/>
    </step>
</job>

We’re just going to deploy the application as a web archive. Note the inclusion of the following files:

/META-INF/batch-jobs/myJob.xml

The myJob.xml file is needed for running the batch definition.

@Deployment
public static WebArchive createDeployment() {
    WebArchive war = ShrinkWrap.create(WebArchive.class)
            .addClass(BatchTestHelper.class)
            .addPackage("org.javaee7.batch.split")
            .addAsWebInfResource(EmptyAsset.INSTANCE, ArchivePaths.create("beans.xml"))
            .addAsResource("META-INF/batch-jobs/myJob.xml");
    System.out.println(war.toString(true));
    return war;
}

In the test, we’re just going to invoke the batch execution and wait for completion. To validate the test expected behaviour we need to query JobOperator#getStepExecutions.

@Test
public void testBatchSplit() throws Exception {
    JobOperator jobOperator = BatchRuntime.getJobOperator();
    Long executionId = jobOperator.start("myJob", new Properties());
    JobExecution jobExecution = jobOperator.getJobExecution(executionId);

    jobExecution = BatchTestHelper.keepTestAlive(jobExecution);

    List<StepExecution> stepExecutions = jobOperator.getStepExecutions(executionId);
    List<String> executedSteps = new ArrayList<>();
    for (StepExecution stepExecution : stepExecutions) {
        executedSteps.add(stepExecution.getStepName());
    }

    (1)
    assertEquals(3, stepExecutions.size());
    assertTrue(executedSteps.contains("step1"));
    assertTrue(executedSteps.contains("step2"));
    assertTrue(executedSteps.contains("step3"));

    (2)
    assertTrue(executedSteps.get(0).equals("step1") || executedSteps.get(0).equals("step2"));
    assertTrue(executedSteps.get(1).equals("step1") || executedSteps.get(1).equals("step2"));
    (3)
    assertTrue(executedSteps.get(2).equals("step3"));

    (4)
    assertEquals(BatchStatus.COMPLETED, jobExecution.getBatchStatus());
}
  1. Make sure all the steps were executed.

  2. Steps step1 and step2 can appear in any order, since they were executed in parallel.

  3. Step step3 is always the last to be executed.

  4. Job should be completed.

Share the Knowledge

Find this sample useful? Share on

There's a lot more about JavaEE to cover. If you're ready to learn more, check out the other available samples.

Help Improve

Find a bug in the sample? Something missing? You can fix it by editing the source, making the correction and sending a pull request. Or report the problem to the issue tracker

Recent Changelog

  • Dec 14, 2014: Switch from polling on jobexecution (for job completion) to polling with joboperator and executionid by Scott Kurz
  • Jul 05, 2014: Removed header license for batch xml files by Roberto Cortez
  • Jun 22, 2014: Removed header license. the licensing is now referenced in the license file in the root of the project by Roberto Cortez
  • Jun 20, 2014: Added fqn to java ee api references to generate direct links to javadocs by radcortez
  • Jun 19, 2014: Documentation clarifications and typos by radcortez
  • May 30, 2014: Added documentation to split project by Roberto Cortez
  • Dec 31, 2013: Fixed test. assert steps order by Roberto Cortez
  • Dec 31, 2013: Added test for split project by Roberto Cortez
  • Sep 17, 2013: Removing netbeans configuration file by Arun Gupta
  • Sep 17, 2013: Improving output messages, adding copyrights and @author by Arun Gupta
How to help improve this sample
The source code for this sample can be found in the javaee7-samples GitHub repository. The first thing you need to do is to get the source by downloading the repository and then go into the samples folder:
git clone git://github.com/javaee-samples/javaee7-samples.git
cd javaee7-samples/batch/split/

Do the changes as you see fit and send a pull request!

Good Luck!