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:

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.

Timers are not persistent. If you reboot your server, all timers will be lost and you will have to reinitiate them.

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.

Категории