Skip navigation.

Developing Manageable Applications with JMX

  Previous Next vertical dots separating previous/next from contents/index/pdf Contents Index View as PDF   Get Adobe Reader

Using the WebLogic Server JMX Timer Service

If you need your JMX client to carry out a task at a specified time or a regular time interval, you can configure a JMX timer service. The service emits notifications at specific dates and times or at a constant interval. Then you create a listener that listens for the timer notifications and responds appropriately.

For example, you want a JMX monitor to run between 9am and 9pm each day. You can use the JMX timer service to emit a notification each day at 9am, which triggers a JMX listener to start your monitor. Then the timer service emits another notification at 9pm, which triggers your listener to stop the monitor MBean.

The JDK includes an implementation of the JMX timer service (see in the J2SE 5.0 API Specification), however listeners for this timer service run in their own thread in a server's JVM.

WebLogic Server includes an extension of the standard timer service that causes timer listeners to run in a thread that Weblogic Server manages and within the security context of a WebLogic Server user account.

The following sections describe how to use the WebLogic timer service:


Using the WebLogic Timer Service: Main Steps

Each JMX client that wants to use the timer service must construct and manage instances of the timer service as it requires (WebLogic Server does not provide a centralized timer service that all JMX clients use). Each time you restart a server instance, each JMX client must re-instantiate any timer service configurations it needs.

To use the WebLogic Server timer service:

  1. Create a JMX listener class in your application.
  2. For general instructions on creating a JMX listener, see Creating a Notification Listener in Developing Custom Management Utilities in JMX.

  3. Create a class that does the following:
    1. Configures an instance of to emit notifications at a specific time or at a recurring interval. See TimerNotification in the J2SE 5.0 API Specification.
    2. For each notification that you configure, include a String in the notification's Type attribute that identifies the event that caused the timer to emit the notification.

      See Configuring a Timer MBean to Emit Notifications.

    3. Registers your listener and an optional filter with the timer MBean that you configured.
    4. Starts the timer in the timer MBean that you configured.
    5. For general instructions, see Configuring a Notification Filter and Registering a Notification Listener and Filter in Developing Custom Management Utilities in JMX.

    6. Unregisters the timer MBean and closes it connection to the MBean server when it has finished using the timer service.
  4. Package and deploy the listener and other JMX classes. See Packaging and Deploying Listeners on WebLogic Server in Developing Custom Management Utilities in JMX.


Configuring a Timer MBean to Emit Notifications

To configure a Timer MBean instance to emit a notification:

  1. Initialize a connection to the Domain Runtime MBean Server.
  2. See Connect to an MBean Server in Developing Custom Management Utilities in JMX.

  3. Create an ObjectName for your timer MBean instance.
  4. See in the J2SE 5.0 API Specification.

    BEA recommends that your object name starts with the name of your organization and includes key properties that clearly identifies the purpose of the timer MBean instance.

    For example, "mycompany:Name=myDailyTimer,Type=weblogicTimer"

  5. Create and register the timer MBean.
  6. Use classname ObjectName name) method where:

  7. Configure the timer MBean to emit a notification.
  8. Invoke the MBean's addNotification operation. Table 5-1 describes each parameter of the addNotification operation. For more information, see in the WebLogic Server API Reference.

    The addNotification operation creates a TimerNotification object and returns an identifier (Integer) for the new TimerNotification object. You can use this identifier to retrieve information about the TimerNotification object from the timer or to remove the object from the timer's list of notifications.

  9. Repeat step 4 for each timer notification that your JMX client needs to receive.
  10. Start the timers in your timer MBean by invoking the timer MBean's start() operation.

When the time that you specify arrives, the timer service emits the TimerNotification object along with a reference to the handback object.

Table 5-1 Parameters of the addNotification Operation



java.lang.String type

A string that you use to identify the event that triggers this notification to be broadcast. For example, you can specify midnight for a notification that you configure to be broadcast each day at midnight.

java.lang.String message

Specifies the value of the TimerNotification object's message attribute.

java.lang.Object userData

Specifies the name of an object that contains whatever data you want to send to your listeners. Usually, you specify a reference to the class that registered the notification, which functions as a callback.

java.util.Date startTime

Specifies a Date object that contains the time and day at which the timer emits your notification.

See Creating Date Objects.

long period

(Optional) Specifies the interval in milliseconds between notification occurrences. Repeating notifications are not enabled if this parameter is zero or is not defined (null).

long nbOccurences

(Optional) Specifies the total number of times that the notification will occur. If the value of this parameter is zero or is not defined (null) and if the period is not zero or null, then the notification will repeat indefinitely.

If you specify this parameter, each time the Timer MBean emits the associated notification, it decrements the number of occurrences by one. You can use the timer MBean's getNbOccurrences operation to determine the number of occurrences that remain. When the number of occurrences reaches zero, the timer MBean removes the notification from its list of configured notifications.


Creating Date Objects

The constructor for the java.util.Date object initializes the object to represent the time at which you created the Date object measured to the nearest millisecond. To specify a different time or date:

  1. Create an instance of java.util.Calendar.
  2. Configure the fields in the Calendar object to represent the time or date.
  3. Invoke the Calendar object's getTime() method, which returns a Date object that represents the time in the Calendar object.

For example, the following code configures a Date object that represents midnight:

java.util.Calendar cal = java.util.Calendar.getInstance();
cal.set(java.util.Calendar.HOUR_OF_DAY, 24);
java.util.Date morning = cal.getTime();

See java.util.Calendar in the J2SE 5.0 API Specification.


Example: Generating a Notification Every Five Minutes After 9 AM

The code in Listing 5-1 creates an instance of that emits a notification every 5 minutes after 9am.

Note the following about the code:

Listing 5-1 Create, Register, and Configure a Timer MBean

import java.util.Hashtable;
import javax.naming.Context;
public class RegisterTimer  {
   private static MBeanServerConnection connection;
   private static JMXConnector connector;
   private static final ObjectName service;
   // Initialize the object name for RuntimeServiceMBean
   // so it can be used throughout the class.
   static {
      try {
         service = new ObjectName(
      }catch (MalformedObjectNameException e) {
         throw new AssertionError(e.getMessage());
    * Initialize connection to the Runtime MBean Server.
    * This MBean is the root of the runtime MBean hierarchy, and
    * each server in the domain hosts its own instance.
   public static void initConnection(String hostname, String portString,
      String username, String password) throws IOException,
      MalformedURLException {
      String protocol = "t3";
      Integer portInteger = Integer.valueOf(portString);
      int port = portInteger.intValue();
      String jndiroot = "/jndi/";
      String mserver = "";
      JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname, port,
         jndiroot + mserver);
      Hashtable h = new Hashtable();
      h.put(Context.SECURITY_PRINCIPAL, username);
      h.put(Context.SECURITY_CREDENTIALS, password);
      connector = JMXConnectorFactory.connect(serviceURL, h);
      connection = connector.getMBeanServerConnection();
   public static void main(String[] args) throws Exception {
      String hostname = args[0];
      String portString = args[1];
      String username = args[2];
      String password = args[3];
      try {
         /* Invokes a custom method that establishes a connection to the
          * Runtime MBean Server and uses an instance of
          * MBeanServerConnection to represents the connection. The custom
          * method assigns the MBeanServerConnection to a class-wide, static
          * variable named "connection".
         initConnection(hostname, portString, username, password);
         //Creates and registers the timer MBean.
         ObjectName timerON = new
         String classname = "";
         connection.createMBean(classname, timerON);
         System.out.println("===> created timer mbean "+timerON);
         // Configures the timer MBean to emit a morning notification.
         // Assigns the return value of addNotification to a variable so that
         // it will be possible to invoke other operations for this specific
         // notification.
         java.util.Calendar cal = java.util.Calendar.getInstance();
         cal.set(java.util.Calendar.HOUR_OF_DAY, 9);
         java.util.Date morning = cal.getTime();
         String myData = "Timer notification";
         Integer morningTimerID = (Integer) connection.invoke(timerON,
            new Object[] { "mycompany.timer.notification.after9am" ,
            "After 9am!", myData, morning, new Long(60000) },
            new String[] {"java.lang.String", "java.lang.String",
            "java.lang.Object", "java.util.Date", "long" });
         //Instantiates your listener class and configures a filter to
         // forward only timer messages.
         MyListener listener = new MyListener();
         NotificationFilterSupport filter = new NotificationFilterSupport();
         //Uses the MBean server's addNotificationListener method to
         //register the listener and filter with the timer MBean.
         System.out.println("===> ADD NOTIFICATION LISTENER TO "+ timerON);
         connection.addNotificationListener(timerON, listener, filter, null);
         System.out.println("\n[myListener]: Listener registered ...");
         //Starts the timer.
         connection.invoke(timerON, "start", new Object[] { }, new String[] {});
         //Keeps the remote client active.
         System.out.println("Pausing. Press Return to end...........");;
      } catch(Exception e) {
         System.out.println("Exception: " + e);


Removing Notifications

The timer MBean removes notifications from its list when either of the following occurs:

The timer MBean also provides the following operations to remove notifications:

For more information, see in the WebLogic Server API Reference.


Skip navigation bar  Back to Top Previous Next