
Leading the Way: Payara Platform Community 7 Beta Now Fully Jakarta EE 11 Certified
We’re excited to announce that Payara Platform Community 7 Beta application server is now fully certified as Jakarta EE 11 […]
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.
@Dependent
@Named("TestBatchlet")
public class TestBatchlet implements Batchlet {
@Inject
private JobContext jobCtx;
@Override
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));
output.append(timestamp);
output.newLine();
output.close();
return "COMPLETED";
}
@Override
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.
BatchTest.xml
<?xml version="1.0" encoding="UTF-8"?>
<job id="testjob" xmlns="http://xmlns.jcp.org/xml/ns/javaee" version="1.0">
<properties>
<property name="outfile" value="tmpoutput.txt"/>
</properties>
<step id="teststep">
<batchlet ref="TestBatchlet"/>
</step>
</job>
Finally, we will create an EJB to trigger our job every 30 seconds
@Stateless
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!
{{cta(‘b515e79e-d13e-489f-8bc0-230da7e083c5’)}}
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.
Share:
We’re excited to announce that Payara Platform Community 7 Beta application server is now fully certified as Jakarta EE 11 […]
If your Java EE 8 applications run on Red Hat JBoss Enterprise Application Platform (EAP) 7, you can’t afford […]
Every software evolves, until it reaches its natural end of life, even the strongest one. This is valid for […]
Hello! What need for run this sample with security role?
Have you tried the @RunAs annotation in the timer EJB?
Payara 5.2020 has removed the Derby DB that was default in GF. Can you please advise how to use H2 for the timer pool (as per default timer pool SQL tables are not being created) – or how to get the Derby DB back again on Payara? Thanks!!
I’ve provided an answer how to create the required Timer tables in H2 in Payara Forum here: https://groups.google.com/d/msg/payara-forum/wcmyy1JjAJs/PeD8acOOCAAJ
Hi Stf,
Please try creating the tables in H2 using the script for H2 in Payara installation, in directory glassfish/lib/install/databases.