|
Problem(Abstract) |
This Web example demonstrates two different ways to use
JMX Timer events in your application using WebSphere Application Server
Version 5. These timer events can be used to do something in 30 seconds, 1
hour, or whatever you choose, and they use JMX functions to set the
notification. The main thing is to code the timer class correctly using
JMX, and to include the timer class in the "listener" section of the
web.xml file.
The source code is included in the attached Timer2Web.war file. After
loading the WAR file, to invoke the test, use the follow URL:
'http://localhost:9080/TimerWeb/InvokeTimer'
By default, it adds two listeners :
A. repeat every 24 hours forever.
B repeat every 10 seconds forever.
This test application also takes parameters to specify the timer
intervals. For example:
'http://localhost:9080/Timer2Web/InvokeTimer?intervalA=600&intervalB=10'
A. repeat every 10 minutes forever.
B repeat every 10 seconds forever.
However, if the parameters are as follows, the timer fires every 10
seconds for the first listener and 20 seconds for the second:
'http://localhost:9080/Timer2Web/InvokeTimer?intervalA=10&intervalB=20'
Below is the source code for the "TonyLifeCycleListener" and TimerServlet.
It's slightly
different than what's bundled in the attached ear file, but it's slightly
simpler code, so it makes a good example.
To use it, just add TonyLifeCycleListener as a "listener" tab (if you're
using WebSphere Studio) in the web.xml to load it - it will run intervals
every minute. Note the reflection on the JMX methods to get the timer to
work. Using the reflection is the recommended method.
Here's the source:
import java.util.Date;
import javax.management.Notification;
import javax.management.NotificationListener;
import javax.management.InstanceNotFoundException;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.naming.*;
import javax.management.*;
import javax.management.timer.*;
// Implementing NotificationListener
public final class TonyLifeCycleListener
implements ServletContextListener, NotificationListener {
//
InitialContext initContext = null;
private static final String TIMER_JNDI_NAME = "jmxtimer";
private static final String TIMER_OBJECT_NAME_JNDINAME = "jmxtimer2";
Integer notificationID;
ObjectName obj = null;
ObjectName objName = null;
MBeanServer server = null;
ObjectInstance objTimer = null;
//
private Integer notificationId;
public void contextInitialized(ServletContextEvent event) {
System.out.println(">>> LifecycleListener contextInitialized
called.");
try {
initContext = new javax.naming.InitialContext();
} catch (Exception e) {
System.out.println("Exception creating InitialContext: " + e);
return;
}
// Check if timer is already initialized
try {
obj = (ObjectName) initContext.lookup(TIMER_OBJECT_NAME_JNDINAME);
// Timer service is registered already.
System.out.println("Timer is not registered.");
return;
} catch (NameNotFoundException nnfe) {
// Timer is not registered.
System.out.println("Timer is not registered.");
} catch (Exception e) {
System.out.println(
"Exception throw when looking up timer service: " + e);
return;
}
//tje
try {
if (obj == null) {
System.out.println("Lifecycle Listener... Get the MBeanServer.");
// WebSphere Specific method of getting the MBeanServer.
//MBeanServer server =
AdminServiceFactory.getMBeanFactory().getMBeanServer();
// Generic method for getting the MBean server
server =
(MBeanServer) MBeanServerFactory.findMBeanServer(null).get(
0);
System.out.println("LifeCycleListener... Create the Timer.");
objName =
new ObjectName("JmxTimerTestDomain", "service", "timer");
System.out.println("LifeCycleListener... Object name = " + objName);
objTimer =
server.createMBean("javax.management.timer.Timer", objName);
// Call Timer.start() method.
System.out.println("LifeCycle Listener... Start Timer.");
server.invoke(objTimer.getObjectName(), "start", new Object[] {
}, new String[] {
});
// if (LOG.isDebugEnabled()) {
// StringBuffer debugMsg = new StringBuffer(100);
// debugMsg.append("Pre-register the Timer.");
// debugMsg.append(server.getDefaultDomain());
// debugMsg.append(":name=");
// debugMsg.append(this.getClass().getName());
// LOG.debug(debugMsg.toString());
// }
// NEED TO DO PreRegister MBean ???
System.out.println("LifeCycleListener... Bind Timer.");
//initContext.rebind(TIMER_OBJECT_NAME_JNDINAME, objName);
initContext.bind(TIMER_OBJECT_NAME_JNDINAME, objName);
System.out.println(
"LifecycleListener... Bound timer MBean object name successfully.");
}
server.addNotificationListener(objName, this, null, new Object());
// Adding the notification to the Timer and assigning the // ID that the
Timer returns to a variable
Date timerTriggerAt = new Date((new Date()).getTime() + 5000L);
Long period = new Long(60000);
notificationID =
(Integer) server.invoke(
objName,
"addNotification",
new Object[] {
"TimerType",
"recurring",
this,
timerTriggerAt,
period,},
new String[] {
"".getClass().getName(),
"".getClass().getName(),
"java.lang.Object",
Date.class.getName(),
long.class.getName(),});
} catch (Exception e) {
System.out.println(
"LifeCycleListener... ####ERROR#### Exception in init: " + e);
return;
}
System.out.println(">>> LifeCycleListener timer started.");
}
public void contextDestroyed(ServletContextEvent event) {
System.out.println(">>> LifeCycleListener contextDestroyed
called.");
try {
server.invoke(
objName,
"removeNotification",
new Object[] { notificationID },
new String[] { Integer.class.getName()});
// Call Timer.removeNotificationListener() method.
System.out.println(
"LifeCycleListener... Removing listener.");
server.removeNotificationListener(objName, this);
System.out.println(">>> LifeCycleListener timer stopped.");
} catch (Exception e2) {
e2.printStackTrace();
}
} /* callback method */
public void handleNotification(Notification notif, Object handback) {
System.out.println(
">>>LifeCycleListener "
+ (new Date())
+ " LifeCycleListener timer handleNotification="
+ notif
+ ", handback="
+ handback);
}
Tony Efremenko 2/4/2005
|
|
|
|
Resolving the
problem |
 |
|
|
|
|
Cross Reference information |
Segment |
Product |
Component |
Platform |
Version |
Edition |
Application Servers |
Runtimes for Java Technology |
Java SDK |
|
|
|
|
|
|