Byron Kiourtzoglou

About Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. 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 two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


6 + = twelve



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close