Chapter 14: Job Scheduling with Spring

Overview

Most application logic happens in response to some form of user action, such as a button click or a form submission. However, in many applications certain processes must be invoked without user interaction, usually at a given interval. For example, you might have a process that cleans out temporary files once an hour or a process that creates a data export from a database and sends it to an external system once a day at midnight. Most nontrivial applications require some kind of scheduling support, if not directly related to business logic of the application, then to support system housekeeping.

When you are building scheduled tasks for your application, it is fairly simple to create a task that runs once an hour or once a day. But what about a task that runs at 15:00 every Monday, Wednesday, and Friday? This is a little more difficult to code, and it makes sense to use prebuilt scheduling solutions rather than attempt to create your own scheduling framework.

When talking about scheduling from a programming perspective, we tend to talk about three distinct concepts. A job is a unit of work that needs to be scheduled to run at a specific interval. A trigger is a condition that causes a job to run, perhaps a fixed interval or a given piece of data. A schedule is a collection of triggers that govern the complete schedule of a job. Typically, you encapsulate a job by implementing some interface or extending some given base class. You define your triggers in whatever terms your scheduling framework supports. Some frameworks may support only basic interval-based triggers, whereas others, such as Quartz, provide much more flexible trigger schemes. In general, a job only has a single trigger in its schedule, and the terms schedule and trigger are often used interchangeably.

Scheduling support in Spring comes in two distinct forms: JDK Timer-based and Quartz- based. The JDK Timer-based approach provides scheduling capabilities on any 1.3 or later JVM, and it does not need external dependencies beyond Spring. Timer-based scheduling is quite primitive and provides limited flexibility when defining job schedules. However, Timer support is included with Java and requires no external libraries, which might be beneficial if you are restricted by application size or corporate policy. Quartz-based scheduling is much more flexible and allows triggers to be defined in a much more real-world way, such as the earlier example of 15:00 every Monday, Wednesday, and Friday.

In this chapter, we explore both of the scheduling solutions included with Spring. In particular, this chapter discusses three core topics:

  • Scheduling with the JDK Timer: In this section, we explore Spring's support for JDK Timer-based scheduling. This section introduces the different trigger types available with Timer-based scheduling and looks at how you can schedule any arbitrary logic without needing to create additional Java code.

  • Quartz-based scheduling: In this section, we look at the comprehensive Quartz scheduling engine and how it is integrated into Spring. In particular, we examine Quartz support for cron expressions allowing for highly complex schedules to be configured using a concise format. As with the JDK Timer section, in this section, you see how to schedule any logic without needing to encapsulate it for Quartz.

  • Job scheduling considerations: In this section, we discuss the various points to consider when choosing a scheduling implementation and patterns to use when creating logic for scheduled execution.



Pro Spring
Pro Spring
ISBN: 1590594614
EAN: 2147483647
Year: 2006
Pages: 189

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net