Job scheduling is a common need in business software environments. The first thing that likely comes to mind is cron. While cron is a very useful tool, it fails to provide for even basic business needs including logging, history and monitoring and availability (beyond the availability of the host it runs on).
Open source tools such as Quartz, cron4j and Spring’s scheduler are common choices. These schedulers are all moderate improvements over cron in that you can run them within existing jvm instances, be it a running servlet container or even a J2EE server. Generally, though, these schedulers fall short of even the most basic software expectations.
Let’s first of all dispense with Spring’s scheduler – using TimerTask. Spring’s focus is not automation or scheduling. This is clear when we start to review the feature set and usage of scheduling in Spring. While some may argue for Spring’s wiring via static configuration (I would not), this is much less pracitcal when it comes to scheduling jobs. Can you work around this static configuration by rolling your own solution around it? Sure, but you’d be violating a basic principle of Spring and do you really want to introduce into your software project the maintenance of code to handle deficencies in an afterthought feature of a framework with an alternate focus? This is besides that fact that there is no built in failover, logging or monitoring.
What about other java schedulers whose focus is in fact scheduling and execution? Some of these acknowledge that they are nothing more than cron in java, but others are clear improvements over Spring’s scheduler. The most popular and comprehensive open source tool available is Quartz. Let’s evaluate Quartz.
Quartz does provide much control over when a job runs. Time of day, days of week, days of month, days of year and any combination of these with support for custom calendars. Apart from the custom calendars, you probably recognize what it supports as being very similar to cron. It probably wouldn’t surprise you that quartz uses cron patterns as a basis for their configurations. Unfortunately, they do not use a true cron pattern adding an extra seconds field at the beginning of each pattern. For anyone who has been using cron for even a short period of time, it’s truly bewildering and frustrating that they chose to not adhere to the established pattern and that when they did so, made their changes non-optional and at the beginning of each pattern. Quartz also does not support some functionality in cron such as day-of-week combined with day-of-month.
This is about all Quartz does moderately well. Our next blog post will identify all the things a world-class, full-featured scheduler needs to support and the right way to do so.