@Named()
public class MyBatchlet extends AbstractBatchlet {
public MyBatchlet();
@Override()
public String process();
}
cd javaee7-samples/batch/batchlet-simple/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=MyBatchletTest
Batchlet is the simplest processing style available in the Batch specification. It’s a task oriented step where the task is invoked once, executes, and returns an exit status.
A Batchlet need to implement Batchlet
interface or in alternative extend
AbstractBatchlet
that already provides empty implementations for all methods.
@Named()
public class MyBatchlet extends AbstractBatchlet {
public MyBatchlet();
@Override()
public String process();
}
We are mostly interested in overriding AbstractBatchlet#process
to provide the behaviour that we
want to achieve with the Batchlet itself. Common cases include: copy files to process with a chunk oriented step,
startup and cleanup, or validations to your processing workflow.
To run your Batchlet, just add it to the job xml file (myJob.xml
).
<?xml version="1.0" encoding="UTF-8"?>
<job id="myJob" xmlns="http://xmlns.jcp.org/xml/ns/javaee" version="1.0">
<step id="myStep" >
<batchlet ref="myBatchlet"/>
</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)
.addClass(MyBatchlet.class)
.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 just need to check the Batch Status in the JobExecution
object. We
should get a COMPLETED
.
@Test
public void testBatchletProcess() throws Exception {
JobOperator jobOperator = BatchRuntime.getJobOperator();
Long executionId = jobOperator.start("myJob", new Properties());
JobExecution jobExecution = jobOperator.getJobExecution(executionId);
jobExecution = BatchTestHelper.keepTestAlive(jobExecution);
(1)
assertEquals(jobExecution.getBatchStatus(), BatchStatus.COMPLETED);
}
Job should be completed.
There's a lot more about JavaEE to cover. If you're ready to learn more, check out the other available samples.
git clone git://github.com/javaee-samples/javaee7-samples.git
cd javaee7-samples/batch/batchlet-simple/
Do the changes as you see fit and send a pull request!
Good Luck!