5 Using the WebLogic Server JMX Timer Service
This chapter includes the following sections:
- Overview of the WebLogic Server JMX Timer Service
A JMX timer service can be configured to emit notifications, and a listener to respond to the notifications with a specified action. - Creating the Timer Service: Main Steps
You construct and manage instances of the timer service for each JMX client. 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. - Configuring a Timer MBean to Emit Notifications
This section describes how to configure a timer MBean to emit notifications. - Creating Date Objects
The constructor for thejava.util.Date
object initializes the object to represent the time at which you created theDate
object measured to the nearest millisecond. - Example: Generating a Notification Every Five Minutes After 9 AM
This section provides an example to create, register, and configure a timer MBean. - Removing Notifications
This section describes when the timer MBean removes notifications.
Overview of the WebLogic Server JMX Timer Service
A JMX timer service can be configured to emit notifications, and a listener to respond to the notifications with a specified action.
For example, you want a JMX monitor to run between 9am and 9pm each day. You configure the JMX timer service to emit a notification daily at 9am, which triggers a JMX listener to start your monitor. The timer service emits another notification at 9pm, which triggers the listener to stop the monitor MBean.
The JDK includes an implementation of the JMX timer service (see
javax.management.timer.Timer
in the Java SE 17 API
Specification at https://docs.oracle.com/en/java/javase/17/docs/api/java.management/javax/management/timer/Timer.html
);
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.
Parent topic: Using the WebLogic Server JMX Timer Service
Creating the Timer Service: Main Steps
You construct and manage instances of the timer service for each JMX client. 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 create the WebLogic Server timer service:
-
Create a JMX listener class in your application.
For general instructions on creating a JMX listener, see Creating a Notification Listener in Developing Custom Management Utilities Using JMX for Oracle WebLogic Server.
-
Create a class that does the following:
-
Configures an instance of
weblogic.management.timer.TimerMBean
to emitjavax.management.timer.TimerNotification
notifications at a specific time or at a recurring interval. SeeTimerNotification
in the Java SE 17 API Specification athttps://docs.oracle.com/en/java/javase/17/docs/api/java.management/javax/management/timer/TimerNotification.html
.For each notification that you configure, include a
String
in the notification'sType
attribute that identifies the event that caused the timer to emit the notification. -
Registers your listener and an optional filter with the timer MBean that you configured.
-
Starts the timer in the timer MBean that you configured.
See Configuring a Notification Filter and Registering a Notification Listener and Filter in Developing Custom Management Utilities Using JMX for Oracle WebLogic Server.
-
Unregisters the timer MBean and closes its connection to the MBean server when it finishes using the timer service.
-
-
Package and deploy the listener and other JMX classes to WebLogic Server. See Packaging and Deploying Listeners on WebLogic Server in Developing Custom Management Utilities Using JMX for Oracle WebLogic Server.
Parent topic: Using the WebLogic Server JMX Timer Service
Configuring a Timer MBean to Emit Notifications
This section describes how to configure a timer MBean to emit notifications.
Perform the following steps to configure a Timer
MBean instance to
emit a notification:
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
Parameter | Description |
---|---|
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 |
java.lang.String message |
Specifies the value of the |
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 |
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 ( |
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 ( 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 |
Parent topic: Using the WebLogic Server JMX Timer Service
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.
Perform the following steps to specify a different time or date:
- Create an instance of
java.util.Calendar
. - Configure the fields in the
Calendar
object to represent the time or date. - Invoke the
Calendar
object'sgetTime()
method, which returns aDate
object that represents the time in theCalendar
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 Java SE 17 API Specification at
https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Calendar.html
.
Parent topic: Using the WebLogic Server JMX Timer Service
Example: Generating a Notification Every Five Minutes After 9 AM
This section provides an example to create, register, and configure a timer MBean.
The code in Example 5-1 creates an instance of weblogic.management.timer.Timer
that emits a notification every 5 minutes after 9am.
Note the following about the code:
-
It creates and registers the timer MBean in the WebLogic Server Runtime MBean Server, under the assumption that the JMX client runs alongside applications that are deployed on multiple server instances. In this case, your JMX client would register a timer MBean in each Runtime MBean Server in the domain.
-
Even though it creates an instance of the WebLogic Server timer MBean, the class does not import WebLogic Server classes. Only the MBean server needs access to the WebLogic Server
Timer
class, not the JMX client. -
Any generic JMX listener can be used to listen for timer notifications, because all timer notifications extend
javax.management.Notification
.
Example 5-1 Create, Register, and Configure a Timer MBean
import java.util.Hashtable; import java.io.IOException; import java.net.MalformedURLException; import javax.management.MBeanServerConnection; import javax.management.ObjectName; import javax.management.MalformedObjectNameException; import javax.management.remote.JMXConnector; import javax.management.remote.JMXConnectorFactory; import javax.management.remote.JMXServiceURL; import javax.naming.Context; import javax.management.NotificationFilterSupport; 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( "com.bea:Name=RuntimeService,Type=weblogic.management.mbeanservers.ru ntime.RuntimeServiceMBean"); }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 = "weblogic.management.mbeanservers.runtime"; 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); h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, "weblogic.management.remote"); 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 ObjectName("mycompany:Name=myDailyTimer,Type=weblogicTimer"); String classname = "weblogic.management.timer.Timer"; 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, "addNotification", new Object[] { "mycompany.timer.notification.after9am" , "After 9am!", myData, morning, new Long(300000) }, 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(); filter.enableType("mycompany.timer"); //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..........."); System.in.read(); } catch(Exception e) { System.out.println("Exception: " + e); e.printStackTrace(); } } }
Parent topic: Using the WebLogic Server JMX Timer Service
Removing Notifications
This section describes when the timer MBean removes notifications.
The timer MBean removes notifications from its list when either of the following occurs:
-
A non-repeating notification is emitted.
-
A repeating notification exhausts its number of occurrences.
The timer MBean also provides the following operations to remove notifications:
-
removeAllNotifications()
, which removes all notifications that are registered with the timer MBean instance. -
removeNotification(java.lang.Integer id)
, which removes the notification whose handback object contains the integer value that you specify. TheaddNotification
method returns this handback object when you invoke it (see Step 4 in Configuring a Timer MBean to Emit Notifications. -
removeNotifications(java.lang.String type)
, which removes all notifications whose type corresponds to the type that you specify. You define a notification's type value when you create the notification object. See Table 5-1.
See weblogic.management.timer.Timer
in the WebLogic Server API Reference.
Parent topic: Using the WebLogic Server JMX Timer Service