Home » Java » Enterprise Java » Scheduling principals in Java applications

About Byron Kiourtzoglou

Byron Kiourtzoglou
Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.

Scheduling principals in Java applications

Many projects require scheduling functionality, such us scheduled jobs, repeated jobs, asynchronous execution etc.

Our preferred method is to use an enterprise job scheduler such us Quartz from OpenSymphony.

One of the most tricky parts when coding using scheduled tasks is the execution part. The main rule of thumb here is to pay attention to the actual code that will be executed. Scheduling frameworks use worker threads in the background so as to execute code asynchronously. Quartz enterprise job scheduler, for example, maintains a pool of worker threads that are monitored by a main “controller” thread.

You should keep in mind that scheduled jobs are asynchronous by nature, so introducing an asynchronous execution layer is our preferred way to say that we keep things clean.

Many times in the past we have come across with the dilemma, letting scheduling framework worker threads execute the actual code, or implement an alternative asynchronous execution layer. Either way has its pros and cons.

Using the scheduling framework to execute actual code, pros :

  1. Requires less implementation effort
  2. Worker threads are monitored by the framework “controller” thread
  3. Implementation code is executed at the precise scheduled time

Using the scheduling framework to execute actual code, cons :

  1. Implementation code coexists with the scheduling framework
  2. For “long lived” implementation code, such as communication with external systems, database queries at the “busy hour”, file parsing, loops etc, arises the major issue that you will probably run out of scheduling framework worker threads, especially in the case of repeated jobs at short time intervals.

We like to think that the scheduling framework should act as the “scheduler” and not the “executor”. Scheduling framework main responsibility should be to “fire” execution events at the appropriate time. Event listeners should be used to perform the actual execution of the code.

The best way to implement this design pattern is to use asynchronous JMS messaging. Scheduling framework worker threads should insert a message to a JMS queue upon execution. JMS listener threads should execute the actual code upon receipt of the message. Doing so a slight delay will be introduced before the actual code execution, due to JMS read/write overhead, but “long lived” implementation code will not cause any problems to the scheduling framework.

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 ....

 

Leave a Reply

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

*


six × = 36

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Do you want to know how to develop your skillset and become a ...

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!
Get ready to Rock!
To download the books, please verify your email address by following the instructions found on the email we just sent you.

THANK YOU!

Close