|
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.
Notifications that occur only once.
Notifications that are repeated with a defined period and/or number of occurrences.
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.
Select Project -> Settings option. In the Settings Tree Structure, select JMX Standard Services -> Timer. This displays the dialog for the Timer Notification on the right side.
Select Send Past Notification option to send notification in the list that has the elapsed dates and time when the timer is started.
Select the Add option. This displays the Entry Parameters for the Timer Notification. Enter the following details:
Timer Type: The type may be either User Defined or Standard. The Standard Timer types are :
jmx.mbean.created
jmx.mbean.deleted
Timer Message: Specify the notification message to be sent in the notification.
Date: Specify the Date and time at which the notification is to be sent.
Time Interval: Specify the Time interval for sending the notification. The default time interval is 5000 milliseconds, which means, the notification will be sent every five seconds, starting from the Date and Time specified in the Date option.
No.of Occurences: The number of times the notification has to be sent during the time interval. The default number of occurrences is 5, which means, the notification will be sent five times, with a time interval of five seconds, starting from the Date and Time specified in the Date option.
Click the OK button to add the defined Notification to the Notification Table.
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();
timer.addNotification("Register","Testing
timer service",new String(),new Date(1029244438000L) |
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.
The removeNotification(integer) method takes the notification identifier as parameter and removes the corresponding notification from the list. If the specified notification identifier does not correspond to any notifications in the list, the methods returns an Error.
The removeNotifications(type) method takes the notification type as parameter and removes the corresponding notification from the list. If the specified notification identifier/type does not correspond to any notifications in the list, the method returns an Error.
The timer's removeAllNotifications() method empties the timer's list of notifications.
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.
|