Scheduling principals in Java applications
Many projects require scheduling functionality, such us scheduled jobs, repeated jobs, asynchronous execution etc.
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 :
- Requires less implementation effort
- Worker threads are monitored by the framework “controller” thread
- Implementation code is executed at the precise scheduled time
Using the scheduling framework to execute actual code, cons :
- Implementation code coexists with the scheduling framework
- 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.