Following the code snippets of Andreas Schaefer and Peter Antman, I attach a
full example MBean, which prints a message to the JBoss log every second,
and is easily adapted. Improvements to the code are welcome.

Peter

*** jboss.conf ***
In jboss.conf (JBoss 2.0), add:

<!-- TimedMessage test MBean -->
<MLET CODE = "test.TimedMessage" ARCHIVE="relevantJar.jar"
CODEBASE="../../lib/ext/">
</MLET>

<MLET CODE = "javax.management.timer.Timer" ARCHIVE="jmxri.jar"
NAME="Timer:service=Timer" CODEBASE="../../lib/">
</MLET>

*** test/TimedMessage.java ***
package test;

import java.util.Date;
import java.util.Set;

import javax.management.MBeanRegistration;
import javax.management.MBeanServer;
import javax.management.Notification;
import javax.management.NotificationListener;
import javax.management.ObjectInstance;
import javax.management.ObjectName;
import javax.management.timer.Timer;

import org.jboss.logging.Log;
import org.jboss.util.ServiceMBeanSupport;
import org.jboss.util.MBeanProxy;

/**
 *   Test timed message mbean.
 *
 *   @see TimedMessageMBean
 *   @version $Revision: 1.4 $
 */
public class TimedMessage
   extends ServiceMBeanSupport
   implements TimedMessageMBean, MBeanRegistration, NotificationListener
{
   // Constants -----------------------------------------------------

   // Attributes ----------------------------------------------------
   MBeanServer server;

   // Static --------------------------------------------------------

   // Constructors --------------------------------------------------
   public TimedMessage()
   {
   }

   // Public --------------------------------------------------------
   // Settings

   public ObjectName getObjectName(MBeanServer server, ObjectName name)
      throws javax.management.MalformedObjectNameException
   {
      this.server = server;
      return name == null ? new ObjectName(OBJECT_NAME) : name;
   }

   public String getName()
   {
      return "TimedMessage";
   }

   public void startService()
      throws Exception
   {
        Set lBeans = server.queryMBeans(
            new ObjectName("Timer:service=Timer"),
            null);

        if( !lBeans.isEmpty() ) {
            ObjectInstance lTimerBean = (ObjectInstance)
lBeans.iterator().next();

            //- use addNotification() on the Timer to set what you are
interested in:
            server.invoke( lTimerBean.getObjectName(), "addNotification",
                new Object[] { "test", "testing", null, new
java.util.Date(new java.util.Date().getTime() + Timer.ONE_SECOND ),new
Long(Timer.ONE_SECOND) },
                new String[] { "java.lang.String", "java.lang.String",
"java.lang.Object", "java.util.Date", "long"});

            //- add yourself as a Notification Listener:
            server.addNotificationListener(
                lTimerBean.getObjectName(),
                this,
                null,
                null);

            log.log("Hooking into: " + lTimerBean.getObjectName());

        } else {
            log.log("Failed to hook into timer.");
        }

        log.log("TimedMessage started");
    }

    // NotificationListener implementation ---------------------------
    public void handleNotification(
        Notification n,
        java.lang.Object handback) {

        log.log("TimedMessage notified: " + new Date().getTime());
    }
}


*** test/TimedMessageMBean.java ***
package test;

/**
 *   <description>
 *
 *   @see <related>
 *   @version $Revision: 1.2 $
 */
public interface TimedMessageMBean
   extends org.jboss.util.ServiceMBean
{
   // Constants -----------------------------------------------------
   public static final String OBJECT_NAME = ":service=TimedMessage";

   // Public --------------------------------------------------------
}



--
--------------------------------------------------------------
To subscribe:        [EMAIL PROTECTED]
To unsubscribe:      [EMAIL PROTECTED]
Problems?:           [EMAIL PROTECTED]

Reply via email to