Timer MBeans
JMX provides a standard timer service API, which can be used to generate notifications at set times or intervals. WebLogic 8.1 implements this service by extending the standard JMX timer service, enabling it to run with WebLogic execute threads and any associated security context.
To use this service, you must use an instance of the weblogic.management.timer.Timer class. The following example illustrates how to create an instance, register a listener, register a notification for when an event should be emitted, and start the timer:
Timer timer= new Timer( ); // Register a standard notification listener timer.addNotificationListener(someListener, null, "handback object"); // Start in one second Date start = new Date((new Date( )).getTime( ) + 1000L); // Repeat every minute, please notificationId = timer.addNotification("eggTimer", "someString", this, start, 3*Timer.ONE_MINUTE ); // Start the timer timer.start( );
Note that you may register any standard notification listener that we have already encountered. The type of the notification is TimerNotification. The only tricky bit is the call to the addNotification( ) method, which lets you set up the timer schedule. You can invoke this method as many times as you like to add multiple schedules to the timer. One of the method signatures looks like this:
Integer addNotification (java.lang.String type, java.lang.String message, java.lang.Object userData,java.util.Date startTime, long period, long numOccurences)
Other versions of the same method let you omit the period and/or the number of occurrences. Let's take a closer look at the arguments of this method:
- Use the type argument to identify the type of notification.
- Use the message argument to set the string that should appear in the message attribute of the TimerNotification.
- Use the userData argument to pass an object to the listeners. This can be anything that a listener needs to be able to access. In our example, we simply used this.
- Use the startTime argument to set the time and date after which notifications should start being sent.
- If you specify a period (.), this argument determines the number of milliseconds between notifications. Repeat notifications are disabled if you set this argument to 0.
- Use the numOccurrences argument to set the total number of times that the notification is fired. The MBean keeps track of the number of notifications that are yet to be fired, and you can invoke the Timer.getNbOccurrences( ) method to retrieve this information. If this value is set to 0 and you have specified a period, the notification will repeat indefinitely.
The Timer class also defines constants that make it easier for you to specify the time period: ONE_SECOND, ONE_MINUTE, ONE_HOUR, ONE_DAY, ONE_WEEK. For example, ONE_WEEK resolves to the number of milliseconds in a week.
hod, which simply removes all notifications assigned to a timer.
|
The addNotification( ) method returns an Integer identifier that can be used later to remove the notification from the timer. For example, you could write the following:
// Later timer.stop( ); timer.removeNotification(notificationId); //alternatively timer.removeNotification("eggTimer");
Alternatively, you may use the removeNotification(type) method to remove all notifications of the given type, or else you can use the removeAllNotifications( ) method, which simply removes all notifications assigned to a timer.