Scheduler Service
Some Scheduler Service Intro here.
8.3 Building the Scheduler Service
Large enterprise applications often have the need to schedule tasks that are executed at different times. These tasks could range from running batch jobs every night that generate reports of financial transactions, to running regular checks on the database to ensure data integrity. A banking application might have to schedule bill payments for its customers; a file transfer application might have to schedule periodic transfer of files and a product-based company might want to schedule contacts with prospective customers. Scheduling can be very beneficial to enterprise applications and encourages automation with little or no human intervention.

Figure 8.6 The “Man in the middle” payment scheduler
Let us consider an example. Most people tend to pay bills by check or by using an online payment facility if their bank provides it. If a customer wants to pay his bills online, but his bank does not offer this service, then he has to seek other methods of making payments, like the good old-fashioned way of sending checks. In Figure 8.6, we see the customer has to look up all the bill payment details and send out checks.
In this case, if the bank could offer a feature to pay bills online, then all the customer has to do is to log on, enter the payee information and schedule payments. The bank could have scheduling software to look up the bill payment information and transfer money from his account. No more late-fees! In Figure 8.7, the customer schedules payments online, specifying the date they need to be made and the bank executes these payments. We can develop a Scheduler Service to do this.

Figure 8.7 The automated payment scheduler
The Scheduler service would ensure that jobs are scheduled to run at specific times in the future. These jobs could be run once or multiple times based on the user’s preference. A fine level of detail could be incorporated into the Scheduler service allowing the user to specify exactly how often the job would need to run, right down to a precision of minutes and seconds. The Scheduler Service that we are going to develop will have the following features:
§ The ability to schedule jobs at fixed and varying times
§ The ability to schedule jobs that can run at fixed intervals indefinitely
§ The ability to cancel jobs
§ The ability to list all the currently scheduled jobs
8.3.1 Using the Scheduler Service: A Client’s perspective
A Client of the Scheduler service can be any web application, standalone application or an Enterprise Java Bean. As in the Configuration Service, the Scheduler Service client communicates with the service through an interface. By invoking methods on this interface, the client adds jobs, removes jobs, updates and schedules them. For each job that the client wants to schedule, a subclass of JobDefinition class has to be created.
A client can initialize and use the Scheduler service as follows. These steps would be the same regardless of the implementation. In the explanation below we are assuming a JDK1.4 Timer based implementation of the Scheduler Service is picked up due to component discovery mechanism described earlier. Figure 8.8 shows the class diagram for the Scheduler Service.

Figure 8.8 Scheduler Service class diagram
Figure 8.9 shows the sequence of events Scheduler Service.
§ Use the SchedulerFactory class to get an instance of the Scheduler Service. The Scheduler Factory returns an instance of type IScheduler. The factory discovers the implementation component using the standard component discovery mechanism and it will pick up the JDK based implementation in our case.
IScheduler scheduler = SchedulerFactory.getScheduler();
§ Invoke the createJob method on the IScheduler. This method is a factory, which creates and returns a class which implements ISchedulerJob interface. For e.g., if you invoke the SchedulerFactory with “JDK” as the parameter, a JDKTimerJob is returned.
ISchedulerJob job = scheduler.createJob();
§ Create a JobDefinition by extending the JobDefinition class. The JobDefinition is an abstract class with an abstract method execute(). Listing 8.5 shows a simple class called MyJobDef which prints out a message when the job is executed. Instantiate MyJobDef and initialize it with details such as the Job name and the time and duration it needs to run.
JobDefinition jobDef = new MyJobDef(new Date(),0, "Task 1");
§ Set the JobDefinition on the ISchedulerJob by invoking the setDefinition method.
job.setDefintion(jobDef);
§ Optionally add exception listeners. Whenever the execute method in the JobDefinition throws an exception, all registered listeners are notified.
job.addExceptionListener(myExceptionListener);
§ Add the jobs into the Scheduler
scheduler.addJob(job1);
§ Schedule the jobs by invoking scheduleAllJobs method on the Scheduler service instance.
scheduler.scheduleAllJobs();

Figure 8.9 Using the Scheduler Service
Listing 8.5 MyJob.java
public class MyJobDef extends JobDefinition {
public MyJobDef(Date date, long repeatPeriod, String jobName) {
super(date, repeatPeriod, jobName);
}
public void execute() {
try {
Date dt = new Date();
System.out.println(super.getJobName() + " ran at " + dt);
}
catch(Exception e) {
cancel();
}
}
}
8.3.1 The Scheduler Service API
At this point the only classes requiring some explanation are Scheduler Service Implementation classes. Figure 8.8 shows the Scheduler Service classes. All Scheduler Service implementations implement the IScheduler interface. A utility base implementation, SchedulerService_BaseImpl is provided, which provides the trivial functionality of adding, removing and updating jobs. A concrete implementation, SchedulerService_JDKImpl based on JDK Timer is also provided. This class implements the createJob method to return a JDKTimerJob (which implements ISchedulerJob). When you call the setDefnition method on the ISchedulerJob to set your custom JobDefnition, it gets set into the JDKTimerJob class. Note that the JDKTimerJob extends the java.util.TimerTask. When you finally invoke scheduleAllJobs on IScheduler, a java.util.Timer class is used to schedule your custom jobs.
Provide More explanation of each of these classes and expand a bit.