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.

Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

Leave a Reply


four + 3 =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
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.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books