1 Reply Latest reply on Feb 23, 2006 8:31 PM by Brian Bailey

    How to start timer mbean?

    Brian Bailey Newbie

      Environment:
      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");
       }
      }
      


        • 1. Re: How to start timer mbean?
          Brian Bailey Newbie

          oh, duh. I found a book that explained how to do this. I needed to register an MBean instance for a javax.management.timer.Timer, invoke the start method on the MBean, invoke the addNotification method, etc. And I don't need the timer-service.xml file.

          I've never read up on JMX; perhaps I should have done so.

          As for why it worked without explicitly registering an MBean when using J2SE 1.5, I cannot say. Perhaps when using the code from my previous post, I never really created an object in the container and was only manipulating a local object.