21-Oct-03 (Created: 21-Oct-03) | More in 'Howto-Advanced'

How to use basic scheduler

How to use basic scheduler

Aspire has a rudimentary scheduler in a package called com.ai.scheduler. This scheduler called BasicScheduler allows you to schedule tasks every 'n' number of intervals. Where an "interval" is defined as a time duration in milliseconds.

Here is a basic usage of the scheduler

Prepare your task to be scheduled


public class MyTask implements ISchedule
{
	public boolean execute()
	{
		// do the necessary work
	}
}

Obtain the scheduler

IScheduler theScheduler = (IScheduler) AppObjects.getObject(IScheduler.NAME,null);

Hand over the task to the scheduler


theScheduler.scheduler(MyTask,new BasicScheduleTime(5));
The above code is saying, call MyTask every 5 intervals or 5 timer ticks. How do you know what is the interval of a timer tick. To understand this you need to see the basic scheduler definition in the properties file.

BasicScheduler definition in the properties file


request.AppObjects.scheduler.className=com.ai.scheduler.BasicScheduler
AppObjects.scheduler.timer_interval_in_milli_secs=20000

The first line identifies an implementation for IScheduler. The second line defines the time interval in milliseconds for each tick.

Related interfaces

1. IScheduleTask
2. BasicScheduleTime
3. IScheduler
4. ConnectionPoolConnectionManager1 (One of the users of scheduling)

IScheduleTask


package com.ai.scheduler;

public interface IScheduleTask 
{
   public boolean execute();
} 

IScheduler


package com.ai.scheduler;

/**
 * Schedule the given task with the given time constraints
 */
public interface IScheduler 
{
   // A symbolic name for retrieving the singleton that implements
   // the IScheduler interface.
   public static final String GET_SCHEDULER_REQUEST = "AppObjects.scheduler";
   
   public void schedule( IScheduleTask task, IScheduleTime scheduleTime )
               throws SchedulerException;
}

ConnectionPoolConnectionManager1

This class is a user of the basic scheduler. Here is an excerpt from the source code of this class



	// register the cleanuptask
	IScheduler scheduler = (IScheduler)(AppObjects.getIFactory().getObject(IScheduler.GET_SCHEDULER_REQUEST,null));
	scheduler.schedule(new ConnectionPoolCleanupTask(), new BasicScheduleTime(5));
	
  //*************************************************************************
  //* ConnectionPoolCleanUptask
  //*************************************************************************
  class ConnectionPoolCleanupTask implements com.ai.scheduler.IScheduleTask
  {
     public boolean execute()
     {
        AppObjects.log("cp: Connection view before cleanup at: " + AICalendar.getCurTimeString());
        printConnections();
        testAndCleanupConnections();
        AppObjects.log("cp: Connection view after cleanup: " + AICalendar.getCurTimeString());
        printConnections();
        return true;        
     }
  }