20.0 Using the Timer Service

 


20.1 Timer Service : An Overview

20.2 Adding Timer Notifications

20.3 Removing Timer Notifications

20.4 Starting and Stopping the Timer Service

20.5 Running the Timer Example


 

20.1 Timer Service: An Overview

 

The Timer service enables you to send predefined notifications at specific dates and times. The notifications are sent to all objects registered to receive notifications emitted by the timer. An object is registered to receive notifications by adding a notification listener interface. The Timer class manages a list of dated notifications. A method is provided that allows you to add/remove notifications into/from the list. If any of the notifications in the list has dates that have passed, the timer either sends them immediately or discards them. This behavior is defined by setting the sendPastNotifications flag.

 

The timer service can manage notifications in two different ways given below.  This behavior is defined by the parameters passed to the timer when the notification is added into the list of notifications.

AdventNet Multi-Protocol agent has implemented timer as an MBean so that it may be registered in an agent and configured by remote applications. A specific subclass of the Notification class is defined for use by the timer MBeans. The following code snippet in the generated main file is used to register the timer service in the Multi-Protocol agent.

 

20.2  Adding Timer Notifications

 

Timer Notifications can be added to the Notification list, using the JMX Compiler UI or using API.

 

20.2.1Using JMX Compiler UI

 

Please follow the steps given below to add a Timer Notification to the Notification list.

               20.2.2 Using API

               Timer Notifications are added to the list of Notifications  by adding the relevant code directly in the
           generated main file inside registerJMXServices method. The following code snippet  creates a Timer   
           Notification with the following attributes :                                      

                                  Timer Type : Register
                               Timer Message :  Testing timer service
                               Timer Date :    Apr 11,2002
                               Timer Interval :   5 sec
                               No. of Occurrence :   2    

    javax.management.timer.Timer timer = new javax.management.timer.Timer();
    name = "Services:type=Timer";
    server.registerMBean(timer,new ObjectName(name));   
    server.addNotificationListener(new ObjectName("Services:type=Timer"),notifImpl,notifImpl,(new      Object()));   

    timer.addNotification("Register","Testing timer service",new String(),new Date(1029244438000L)
    5000,2;

    timer.setSendPastNotifications(false);
    timer.start();

 

After the above notification is registered, the Timer Service sends two notifications to all registered listeners with a time interval of five seconds from the date and time specified. The setSendPastNotifications() method, when set as true, sends notifications in the list that has elapsed
dates and time.  The default value of this method is set as false.           
    

20.3 Removing Timer Notification

 

Timer notifications are removed from the list of notifications using the timer's removeNotification() method, or removeNotifications() method, or removeAllNotifications() method. The Timer Notification contains an identifier, which uniquely identifies a notification in the timer’s list of notifications. Hence, Notifications can be removed either by passing its Id or by passing its Type.

20.4 Starting and Stopping the Timer Service

 

The timer is started using the timer Start() method and stopped using the Stop() method. If the list of notifications is empty when the timer is started, the timer waits for a notification to be added.

 

You can determine whether the timer is running or not by invoking the timer method IsActive(). This method returns true, if the timer is running.

 

20.5 Running the Timer Example

 

The Timer Example is present in the <Agent Toolkit Home>/example/jmx/timer directory. This is a simple example to illustrate the usage of Timer MBeans. Timer MBeans are used to send notifications at specific dates and times.

 

The Readme.html file  explains the usage of the example and the steps involved in running the example.

 



Copyright © 1996-2004, AdventNet Inc. All Rights Reserved.