Developing Manageable Applications with JMX
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 javax.management.timer.Timer
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:
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:
For general instructions on creating a JMX listener, see Creating a Notification Listener in Developing Custom Management Utilities in JMX.
weblogic.management.timer.TimerMBean
to emit javax.management.timer.TimerNotification
notifications at a specific time or at a recurring interval. See TimerNotification
in the J2SE 5.0 API Specification.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.
For general instructions, see Configuring a Notification Filter and Registering a Notification Listener and Filter in Developing Custom Management Utilities in JMX.
To configure a Timer
MBean instance to emit a notification:
See Connect to an MBean Server in Developing Custom Management Utilities in JMX.
See javax.management.ObjectName
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"
Use javax.management.MBeanServerConnection.createMBean(String
classname
ObjectName
name
)
method where:
classname
is weblogic.management.timer.Timer
name
is the object name that you created for the timer MBean instance.Note: The time MBean that you create runs in the Java agent on WebLogic Server (it does not run in a client JVM even if you create the timer MBean from a remote JMX client).
Invoke the MBean's addNotification
operation. Table 5-1 describes each parameter of the addNotification
operation. For more information, see weblogic.management.timer.Timer
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.
When the time that you specify arrives, the timer service emits the TimerNotification
object along with a reference to the handback object.
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:
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.
The code in Listing 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:
Timer
class, not the JMX client.javax.management.Notification
.Listing 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(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();
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();
}
}
}
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:
removeAllNotifications()
, which removes all notifications that are registered with the timer MBean instance.removeNotification(java.lang.Integer id)
, which removes the notification whose ID matches the ID you specify. The addNotification
method returns this ID when you invoke it. removeNotifications(java.lang.String type)
, which removes all notifications whose type corresponds to the type that you specify.For more information, see weblogic.management.timer.Timer
in the WebLogic Server API Reference.