Enterprise Java

Schedule Java EE 7 Batch Jobs

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

1
2
3
4
5
6
7
<job id="myJob" xmlns="http://xmlns.jcp.org/xml/ns/javaee" version="1.0">
  <step id="myStep">
    <chunk item-count="3">
    <reader ref="myItemReader"/>
    <processor ref="myItemProcessor"/>
  <writer ref="myItemWriter"/>
</chunk>

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:

1
BatchRuntime.getJobOperator().start("myJob", new Properties());

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.
    1
    2
    3
    4
    5
    6
    7
    @Singleton
    public class MyEJB {
      @Schedule(hour = "23", minute = "59", second = "59")
      public void myJob() {
        BatchRuntime.getJobOperator().start("myJob", new Properties());
      }
    }

    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:
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    @Stateless
    public class MyStatelessEJB {
        @Resource
        ManagedScheduledExecutorService executor;
     
        public void runJob() {
            executor.schedule(new MyJob(), new Trigger() {
     
                public Date getNextRunTime(LastExecution lastExecutionInfo, Date taskScheduledTime) {
                    Calendar cal = Calendar.getInstance();
                    cal.setTime(taskScheduledTime);
                    cal.add(Calendar.DATE, 1);
                    return cal.getTime();
                }
     
                public boolean skipRun(LastExecution lastExecutionInfo, Date scheduledRunTime) {
                    return null == lastExecutionInfo;
                }
     
            });
        }
     
        public void cancelJob() {
            executor.shutdown();
        }
    }

    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:

    1
    2
    3
    4
    5
    6
    7
    public class MyJob implements Runnable {
     
        public void run() {
            BatchRuntime.getJobOperator().start("myJob", new Properties());
        }
     
    }

    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:
    1
    2
    3
    public void runJob2() {
        executor.scheduleWithFixedDelay(new MyJob(), 2, 3, TimeUnit.HOURS);
    }

    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.

How are you scheduling your Batch jobs ?

Reference: Schedule Java EE 7 Batch Jobs from our JCG partner Arun Gupta at the Miles to go 2.0 … blog.
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Arun Gupta

Arun is a technology enthusiast, avid runner, author of a best-selling book, globe trotter, a community guy, Java Champion, JavaOne Rockstar, JUG Leader, Minecraft Modder, Devoxx4Kids-er, and a Red Hatter.
Subscribe
Notify of
guest


This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button