How to start timer mbean?
pepper_fiend Feb 22, 2006 6:34 PMEnvironment:
JBossAS 4.0.3SP1, default server
Sun JDK 1.4.2_10
Windows XP Sp2
I am trying to use the javax.management.timer.Timer class found in jboss-j2ee.jar. The Timer object reports that it is active, but I do not get any notifications. However, as soon as I point JAVA_HOME to Sun JDK 1.5.x, everything works just fine. I imagine it has to do with the Timer object having moved to the J2SE in that version.
I've read some old posts about having to start up a timer mbean using a timer-service.xml file. So, I created one and put it in the default/deploy/ directory and restarted the server. It didn't output any messages about picking up the file and my code still doesn't work.
What am I doing wrong?
Here's my timer-service.xml file:
<?xml version="1.0" encoding="UTF-8"?> <server> <mbean code="javax.management.timer.Timer" name="jboss:service=Timer" /> </server>
And here's my sample web application. I do get the "starting up" and "shutting down" messages, but not "timer fired".
import java.util.Date;
import javax.management.Notification;
import javax.management.NotificationListener;
import javax.management.timer.Timer;
import javax.servlet.http.HttpServlet;
import org.apache.log4j.Logger;
public class StartupServlet extends HttpServlet implements NotificationListener
{
private static final long serialVersionUID = 1L;
private static final Logger LOG = Logger.getLogger(StartupServlet.class);
private Timer timer;
public void init()
{
LOG.info("starting up");
timer = new Timer();
long interval = 5000; // milliseconds
Date timerTriggerAt = new Date(System.currentTimeMillis() + interval);
timer.addNotification(null, null, null, timerTriggerAt, interval);
// Register this class as a listener
timer.addNotificationListener(this, null, null);
// Now start it.
timer.start();
}
public void destroy()
{
LOG.info("shutting down");
timer.removeAllNotifications();
timer.stop();
}
public void handleNotification(Notification arg0, Object arg1)
{
LOG.info("timer fired");
}
}