Java Dynamic Management Kit 3.2 Programming Guide
[ Previous ][ Fast Back ]Chapter 15. Event Handling, Alarm Clock, and Monitoring Services[ Fast Forward ][ Next ]

Scheduler Service

The scheduler service enables you to create alarms for specific dates and times. The alarms or `scheduler events' are sent to all objects registered to receive scheduler events. An object is registered to receive scheduler events by adding a scheduler listener interface. The scheduler service is not manageable as no MO stubs are provided, therefore it must be used locally.

The scheduler class uses a vector to store the scheduler events. A method is provided that allows you to add events. Each scheduler event runs in a separate thread and has a dedicated AlarmClock object. When the scheduler is started, all of the alarm clock objects associated with the scheduler events are updated and started. If any of the alarm clocks have dates that have passed, the scheduler either sends them immediately or discards them. This behavior is defined by setting the notifyPastEvents flag.

To create a scheduler, create an instance of the following class: com.sun.jaw.impl.agent.services.scheduler.Scheduler

Example 15-10. Creating a Scheduler
import com.sun.jaw.impl.agent.services.scheduler.*;
// Create a new scheduler,
// this object is not registered in the framework
private Scheduler sch = new Scheduler(); 
      

Scheduler Listener

You must implement a SchedulerListener interface in any object that receives scheduler event objects. This registers the object as a scheduler listener. To add a SchedulerListener, invoke the addSchedulerListener() method of the scheduler. Example 15-11 shows the code for registering as a scheduler listener with the sch scheduler created in Example 15-10.

Example 15-11. Scheduler Listener Interface
// Register calling object as a scheduler listener
sch.addSchedulerListener(this);

Scheduler Events

Scheduler events are defined as instances of the Java class: com.sun.jaw.impl.agent.services.scheduler.SchedulerEvent.

The scheduler that generates the event must be specified when instantiating a new SchedulerEvent object. Example 15-12 shows the constructor for an object called EventSpring. The EventSpring object extends SchedulerEvent.

Example 15-12. Creating a Scheduler Event
public class EventSpring extends SchedulerEvent {
   public EventSpring(Scheduler source) {
      super(source);
   }
}

There are two types of scheduler events:

The event type is defined by the parameters passed to the scheduler when the event is added.

Adding Events to the Scheduler

Events are added to the scheduler vector using the scheduler's performAddEvent() method. The performAddEvent() method is overloaded and, in addition to the event and date, can take the following optional parameters:

If the scheduler event to be inserted has a date that is before the current date, the method attempts to update the event by adding the defined period to the event date to make it later than the current date. If a null periodicity is defined, the event cannot be updated to a valid date and an InvalidPropertyValueException is thrown.

If the scheduler event has a non-null periodicity, the performAddEvent() method updates the event date by adding the periodicity until the event date is later than the current date. When the event date is later than the current date, the method inserts the scheduler event into the vector.

If a number of repetitions is specified with the periodicity, the performAddEvent() method updates the event date as before. The number of times that the method adds the period is limited to the specified number of repetitions. If the event date remains earlier than the current date, the method generates an exception.

After an event has been added to the scheduler vector, it cannot be updated.

Example 15-13 shows the addition of an evtSpring object to the scheduler sch that was created in Example 15-10. The variable ONE_SECOND is defined in the scheduler class.

Example 15-13. Adding Events to the Scheduler
Date currentDate = new Date();
Date dSpring = new Date();
   
// Date of the scheduler event
dSpring.setTime(currentDate.getTime() + 2 *
                            Scheduler.ONE_SECOND);

// Set the period of the scheduler event
long periodSpring = 8 * Scheduler.ONE_SECOND;

// Create the scheduler event
evtSpring = new EventSpring(sch);

// Add event
sch.performAddEvent((SchedulerEvent)evtSpring,
                                    dSpring, periodSpring);

Removing Events From the Scheduler

Events are removed from the scheduler vector using the scheduler's performRemoveEvent() method. The performRemoveEvent() method is overloaded and can take either the scheduler event or the position of the event in the vector as parameters. If the event specified is not in the vector or the index is invalid, the method throws an InstanceNotFoundException.

Example 15-14 shows both ways of removing a scheduler event from the scheduler vector.

Example 15-14. Removing Events From the Scheduler
// Remove the event evtSpring from the vector
//
sch.performRemoveEvent(evtSpring);
...
// Remove the event evtSpring from the vector
// evtSpring is in position number two in the vector
//
int index = 2;
sch.performRemoveEvent(index);

Starting and Stopping the Scheduler

The scheduler is started using the scheduler performStart() method and stopped using the performStop() method. If the vector is empty when the scheduler is started, the scheduler waits for an event to be added. If events are added later, the associated alarm clock is instantiated and started. The alarm clock timeout is set for the required event date.

You can determine whether the scheduler is running or not by invoking the scheduler method isActive(). The isActive() method returns true if the scheduler is running.

If any of the alarm clocks have dates that have passed when the scheduler is started, the event is ignored or sent immediately. The action taken is defined by the notifyPastEvents flag.

notifyPastEvents = true

All events with a date before the current date are handled; the alarm clocks are updated and notification is sent.

notifyPastEvents = false

Events with a date before the current date are ignored; the alarm clocks are updated but no notification is sent.

Setting the notifyPastEvents flag to false can be used to suppress a flood of scheduler events being sent out when the scheduler is started. The default value for this flag is false.

Example 15-15 shows code for starting and stopping a scheduler.

Example 15-15. Starting and Stopping the Scheduler
public void performStartScheduler() throws
InstanceAlreadyExistException, InvalidPropertyValueException {
        if (sch.isListOfEventsEmpty() == true) {
           performAddSeasonEvent();                  // Add events
        }

        sch.performStart();                          // Starts the scheduler
  }

...

public void performStopScheduler() throws
   SchedulerIllegalAccessException {
        sch.performStop();                // Stops the scheduler service.
        sch.performRemoveAllEvents();     // Deletes the remaining events
                                          // from the scheduler vector.
        state = "initial state";          // Resets the season object.
        temp = new Float(0);
  }

Scheduler Event Handling

When the date of an event in the event list is reached, the handleScheduler() method is invoked. Example 15-16 shows an example of how the events can be handled. This example uses four events:

Example 15-16. Scheduler Event Handling
public void handleScheduler(SchedulerEvent event) {
   if (event instanceof EventSpring) {
      temp = new Float(15);
      state = "Spring weather";
   }
   else if (event instanceof EventSummer) {
      temp = new Float(30);
      state = "Summer weather";
   }
   else if (event instanceof EventAutumn) {
      temp = new Float(15);
      state = "Autumn weather";
   }
   else if (event instanceof EventWinter) {
      temp = new Float(0);
      state = "Winter weather";
   }
   else
      return;
}


[ Previous ][ Home ][ Next ]
Alarm Clock Service[ Up ]Monitoring Service