Payara for Beginners - a Simple JBatch Schedule

05 Jan 2016
Both Payara Server and Payara Micro support Batch Applications for the Java Platform (JSR 352) for the implementation of batch jobs needing no direct user interaction. This article will describe a single step batch application that appends the current datatime to a file every 30 seconds to demonstrate the setup of a simple timer scheduled batch job. 


Firstly, we need to create a java class that appends the current datetime to a file.
JBatch implement two variations of Job step: Chunks - which are built around the traditional batch process of reader, processor and writer elements – and Batchlets for simple tasks.
For the simple process appending a datetime to a file we will be using a Batchlet.To correctly implement a Batchlet we must define process() and stop() methods.


public class TestBatchlet implements Batchlet {

    private JobContext jobCtx;

    public String process() throws Exception {
        String filename = jobCtx.getProperties().getProperty("outfile");
        String timestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());

        BufferedWriter output = new BufferedWriter(new FileWriter(filename, true));

        return "COMPLETED";

    public void stop() throws Exception {


Next, we need to define the steps that make-up our batch job using the Job Specification Language (JSL). The below JSL defines our single-step job and also the value of the outfile property used by the Batchlet.The JSL must be held in an XML file located in the META-INF/batch-jobs directory.



<?xml version="1.0" encoding="UTF-8"?>
<job id="testjob" xmlns="" version="1.0">
        <property name="outfile" value="\tmp\output.txt"/>
    <step id="teststep">
        <batchlet ref="TestBatchlet"/>


Finally, we will create an EJB to trigger our job every 30 seconds



public class TestSchedule {

    @Schedule(second = "*/30", minute = "*", hour = "*") // Thirty second intervals
    public void processFiles() {
        JobOperator jobOperator = BatchRuntime.getJobOperator();
        jobOperator.start("BatchTest", null);


Deploying the above will append the current datetime to the file specified in the XML at 30 second intervals:

2015-12-21 15:06:00
2015-12-21 15:06:30
2015-12-21 15:07:00
2015-12-21 15:07:30
2015-12-21 15:08:00
2015-12-21 15:08:30

And this is all that is required to implement the most basic batch process!

Beginner's Guides to Payara Server



Want to learn more about JBatch? Check out Batch Applications in Java EE 7 - Undertanding JSR 352 Concepts article by Arun Gupta on the GlassFish blog; or watch the 'Batch Applications for the Java Platform 1.0' video by Chris Vignola.