Take Java to Task
Building your own Java component to run and schedule tasks is easy with two new JDK classes
by Efraim Berkovich
Many applications, from small to enterprise-sized, schedule tasks to run at set times. A task might be a once-a-day data export from the database at 2 A.M., a reminder e-mail sent two hours after some system event, or a periodic check every two minutes to see if a new file has arrived by FTP into a target directory. Although many off-the-shelf products for managing jobs and tasks in your application exist, developers can quickly build their own lightweight customizable component for creating and scheduling tasks by taking advantage of two new JDK Java classes.
The task scheduler component described here does not attempt to solve all task scheduling problems. Some things, like backing up your data, are best left to the tools and utilities designed for them. But for those situations where jobs and tasks are best controlled by the application, this task scheduler framework may be very useful. The scheduler component usually is deployed on the server and can run either as an in-process part of the application or as a separate process. In a recent project, we deployed the scheduler as part of our Web application—it would run whenever the application Web server ran. A Web-based interface allows administration of the scheduler and its tasks.
What's in a Scheduler?
If not using a commercial job-scheduling product, programmers often build some combination of constantly running programs and batch jobs scheduled by the operating system (for example, using the cron command in UNIX). However, this approach does not provide an efficient, integrated design. The system wastes resources on the continually cycling programs, the tasks are not integrated under one component, and creating a new task at runtime is not straightforward.
A task scheduler framework constructed in Java is a better alternative to a motley assembly of programs and technologies. The framework makes things simple for an application developer: Programmers write Java classes that perform the tasks they want; then programmers schedule their task class in the task scheduler framework. The application instantiates the task classes and schedules the tasks at any point during the application's execution. The task scheduler sees to it that all the tasks run at the appointed times.
Back to top
|