Enterprise Java

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.

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.
Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments
Back to top button