Schedule Java EE 7 Batch Jobs (Tech Tip #36)

Java EE 7 added the capability to perform Batch jobs in a standard way using JSR 352.

This code fragment is the Job Specification Language defined as XML, a.k.a. Job XML. It defines a canonical job, with a single step, using item-oriented or chunk-oriented processing. A chunk can have a reader, optional processor, and a writer. Each of these elements are identified using the corresponding elements in the Job XML, and are CDI beans packaged in the archive.

This job can be easily started using:

A typical question asked in different forums and conferences is how to schedule these jobs in a Java EE runtime. Batch 1.0 API itself does not offer anything to be schedule these jobs. However Java EE platform offers three different ways to schedule these jobs:

  1. Use the @javax.ejb.Schedule annotation in an EJB.
    Here is a sample code that will trigger the execution of batch job at 11:59:59 PM every day.

    Of course, you can change the parameters of @Schedule to start the batch job at the desired time.
  2. Use ManagedScheduledExecutorService using javax.enterprise.concurrent.Trigger as shown:

    Call runJob to initiate job execution and cancelJob to terminate job execution. In this case, a new job is started a day later than the previous task. And its not started until previous one is terminated. You will need more error checks for proper execution.

    MyJob is very trivial:

    Of course, you can automatically schedule it by calling this code in @PostConstruct.

  3. A slight variation of second technique allows to run the job after a fixed delay as shown:

    The first task is executed 2 hours after the runJob2 method is called. And then with a 3 hours delay between subsequent execution.

This support is available to you within the Java EE platform. In addition, you can also invoke BatchRuntime.getJobOperator().start("myJob", new Properties()); from any of your Quartz-scheduled methods as well.

You can try all of this on WildFly.

And there are a ton of Java EE 7 samples at

This particular sample is available at

How are you scheduling your Batch jobs ?

Be Sociable, Share!

5 thoughts on “Schedule Java EE 7 Batch Jobs (Tech Tip #36)

  1. It’s not very clear to me what this job does or what it’s supposed to do.

    I got the part about scheduling it.

    Could you explain it to me?

  2. I am not sure #3 is a good option. Because you directly instantiated MyJob, you won’t get dependency injection, right?

  3. Where do I have to place the XML? …/resouces/META-INF/batch-jobs/jobs.xml ?
    By the way, your XML is not well formed, there are 2 tag closures that are missing.

Leave a Reply

Your email address will not be published. Required fields are marked *