[jboss-cvs] JBossCache/src/org/jboss/cache/jmx ...

Brian Stansberry brian.stansberry at jboss.com
Fri Jun 29 00:08:19 EDT 2007


  User: bstansberry
  Date: 07/06/29 00:08:19

  Modified:    src/org/jboss/cache/jmx    
                        CacheNotificationBroadcaster.java
                        CacheJmxWrapperMBean.java
                        CacheNotificationListener.java CacheJmxWrapper.java
  Log:
  [JBCACHE-1090] Emit JMX Notifications the JBAS JSR-77 layer expects
  
  Revision  Changes    Path
  1.3       +5 -0      JBossCache/src/org/jboss/cache/jmx/CacheNotificationBroadcaster.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: CacheNotificationBroadcaster.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/jmx/CacheNotificationBroadcaster.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -b -r1.2 -r1.3
  --- CacheNotificationBroadcaster.java	28 Jun 2007 16:53:37 -0000	1.2
  +++ CacheNotificationBroadcaster.java	29 Jun 2007 04:08:19 -0000	1.3
  @@ -27,4 +27,9 @@
       */
      void sendNotification(Notification notification);
   
  +   /**
  +    * Gets the sequence number for the next notification.
  +    */
  +   long getNextNotificationSequenceNumber();
  +
   }
  
  
  
  1.10      +24 -3     JBossCache/src/org/jboss/cache/jmx/CacheJmxWrapperMBean.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: CacheJmxWrapperMBean.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/jmx/CacheJmxWrapperMBean.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -b -r1.9 -r1.10
  --- CacheJmxWrapperMBean.java	23 May 2007 20:23:25 -0000	1.9
  +++ CacheJmxWrapperMBean.java	29 Jun 2007 04:08:19 -0000	1.10
  @@ -28,6 +28,25 @@
    */
   public interface CacheJmxWrapperMBean extends LegacyConfiguration
   {
  +   /** The lifecycle method stop has completed */
  +   public static final int STOPPED  = 0;
  +   /** The lifecycle method stop has been invoked */
  +   public static final int STOPPING = 1;
  +   /** The lifecycle method start has been invoked */
  +   public static final int STARTING = 2;
  +   /** The lifecycle method start has completed */
  +   public static final int STARTED  = 3;
  +   /** There has been an error during some operation */
  +   public static final int FAILED  = 4;
  +   /** The lifecycle method destroy has completed */
  +   public static final int DESTROYED = 5;
  +   /** The lifecycle method create has completed */
  +   public static final int CREATED = 6;
  +   /** The MBean has been instantiated but has not completed MBeanRegistration.postRegister */
  +   public static final int UNREGISTERED = 7;
  +   /** The MBean has been instantiated and has completed MBeanRegistration.postRegister */
  +   public static final int REGISTERED = 8;
  +   
      void create() throws CacheException;
   
      void start() throws CacheException;
  @@ -44,12 +63,14 @@
      CacheStatus getCacheStatus();
      
      /**
  -    * Legacy name for {@link #getCacheStatus()}. Retained to provide
  +    * Legacy attribute to expose the {@link #getCacheStatus() cache status} 
  +    * in terms of the JBoss AS ServiceMBean values.  This interface does
  +    * not extend ServiceMBean, but this attribute is retained to provide
       * compatibility with the JBoss AS JSR-77 integration layer.
       * 
  -    * @return the current status. Will not return <code>null</code>
  +    * @return the current status, e.g. {@link #STARTED}.
       */
  -   CacheStatus getState();
  +   int getState();
   
      /**
       * Retrieves a reference to the underlying {@link Cache}
  
  
  
  1.8       +2 -3      JBossCache/src/org/jboss/cache/jmx/CacheNotificationListener.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: CacheNotificationListener.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/jmx/CacheNotificationListener.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -b -r1.7 -r1.8
  --- CacheNotificationListener.java	28 Jun 2007 16:53:37 -0000	1.7
  +++ CacheNotificationListener.java	29 Jun 2007 04:08:19 -0000	1.8
  @@ -49,7 +49,7 @@
    * events.
    *
    * @author <a href="brian.stansberry at jboss.com">Brian Stansberry</a>
  - * @version $Revision: 1.7 $
  + * @version $Revision: 1.8 $
    */
   @CacheListener
   public class CacheNotificationListener
  @@ -73,7 +73,6 @@
      private static final String NOTIFICATION_DESCR = "JBossCache event notifications";
   
      private final CacheNotificationBroadcaster broadcaster;
  -   private final AtomicLong sequence = new AtomicLong(0);
      private String serviceName;
   
      // ------------------------------------------------------------ Constructors
  @@ -200,6 +199,6 @@
   
      private long seq()
      {
  -      return sequence.getAndIncrement();
  +      return broadcaster.getNextNotificationSequenceNumber();
      }
   }
  
  
  
  1.29      +95 -6     JBossCache/src/org/jboss/cache/jmx/CacheJmxWrapper.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: CacheJmxWrapper.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/jmx/CacheJmxWrapper.java,v
  retrieving revision 1.28
  retrieving revision 1.29
  diff -u -b -r1.28 -r1.29
  --- CacheJmxWrapper.java	14 Jun 2007 16:14:48 -0000	1.28
  +++ CacheJmxWrapper.java	29 Jun 2007 04:08:19 -0000	1.29
  @@ -42,6 +42,7 @@
   import org.jgroups.jmx.JChannelFactoryMBean;
   import org.w3c.dom.Element;
   
  +import javax.management.AttributeChangeNotification;
   import javax.management.JMException;
   import javax.management.ListenerNotFoundException;
   import javax.management.MBeanNotificationInfo;
  @@ -55,6 +56,7 @@
   import javax.transaction.TransactionManager;
   import java.util.List;
   import java.util.concurrent.atomic.AtomicInteger;
  +import java.util.concurrent.atomic.AtomicLong;
   
   /**
    * Wrapper class that exposes a
  @@ -62,7 +64,7 @@
    * {@link CacheImpl}.
    *
    * @author <a href="brian.stansberry at jboss.com">Brian Stansberry</a>
  - * @version $Revision: 1.28 $
  + * @version $Revision: 1.29 $
    */
   public class CacheJmxWrapper
         extends NotificationBroadcasterSupport
  @@ -77,9 +79,12 @@
      private Configuration config;
      private boolean registerInterceptors = true;
      private final AtomicInteger listenerCount = new AtomicInteger(0);
  +   private final AtomicLong sequence = new AtomicLong(0);
      private final CacheNotificationListener cacheNotificationListener;
      private CacheStatus cacheStatus;
      private String notificationServiceName;
  +   private boolean registered;
  +   private boolean disableStateChangeNotifications;
   
      // Legacy config support
   
  @@ -147,9 +152,30 @@
         return cacheStatus;
      }
   
  -   public CacheStatus getState()
  +   public int getState()
      {
  -      return getCacheStatus();
  +      switch (cacheStatus)
  +      {
  +         case INSTANTIATED:
  +         case CREATING:
  +            return registered ? REGISTERED : UNREGISTERED;
  +         case CREATED:
  +            return CREATED;
  +         case STARTING:
  +            return STARTING;
  +         case STARTED:
  +            return STARTED;
  +         case STOPPING:
  +            return STOPPING;
  +         case STOPPED:
  +         case DESTROYING:
  +            return STOPPED;
  +         case DESTROYED:
  +            return registered ? DESTROYED : UNREGISTERED;
  +         case FAILED:
  +         default:
  +            return FAILED;
  +      }
      }
   
      public Address getLocalAddress()
  @@ -579,13 +605,17 @@
   
         try
         {
  +         int oldState = getState();
            cacheStatus = CacheStatus.STARTING;
  +         int startingState = getState();
  +         sendStateChangeNotification(oldState, startingState, getClass().getSimpleName() + " starting", null);
   
            cache.start();
   
            registerInterceptors();
   
            cacheStatus = CacheStatus.STARTED;
  +         sendStateChangeNotification(startingState, getState(), getClass().getSimpleName() + " started", null);
         }
         catch (Throwable t)
         {
  @@ -605,7 +635,11 @@
   
         try
         {
  +         int oldState = getState();
            cacheStatus = CacheStatus.STOPPING;
  +         int stoppingState = getState();
  +         sendStateChangeNotification(oldState, stoppingState, getClass().getSimpleName() + " stopping", null);
  +         
            cache.stop();
   
            if (cache.getCacheStatus() == CacheStatus.DESTROYED)
  @@ -616,6 +650,7 @@
            }
   
            cacheStatus = CacheStatus.STOPPED;
  +         sendStateChangeNotification(stoppingState, getState(), getClass().getSimpleName() + " stopped", null);
         }
         catch (Throwable t)
         {
  @@ -719,6 +754,8 @@
                  log.error("Caught exception registering cache interceptors with JMX", e);
               }
            }
  +         
  +         registered = true;
         }
      }
   
  @@ -738,6 +775,7 @@
         unregisterInterceptors();
   
         server = null;
  +      registered = false;
      }
   
      // ---------------------------------------------------------  Public methods
  @@ -798,6 +836,28 @@
         this.cacheObjectName = name;
      }
   
  +   /**
  +    * Gets whether sending of JMX notifications for this mbean's
  +    * start/stop lifecycle changes is disabled.
  +    * 
  +    * @see #setDisableStateChangeNotifications(boolean)
  +    */
  +   public boolean isDisableStateChangeNotifications()
  +   {
  +      return disableStateChangeNotifications;
  +   }
  +
  +   /**
  +    * Hook to allow PojoCacheJmxWrapper to suppress state change
  +    * notifications from this mbean in lieu of its own.
  +    * 
  +    * @param disableStateChangeNotifications
  +    */
  +   public void setDisableStateChangeNotifications(boolean disableStateChangeNotifications)
  +   {
  +      this.disableStateChangeNotifications = disableStateChangeNotifications;
  +   }
  +
      public MBeanServer getMBeanServer()
      {
         return server;
  @@ -830,6 +890,11 @@
         this.cacheNotificationListener.setServiceName(notificationServiceName);
      }
   
  +   public long getNextNotificationSequenceNumber()
  +   {
  +      return sequence.getAndIncrement();
  +   }
  +
      // ----------------------------------------------------  Superclass Overrides
   
      @Override
  @@ -989,7 +1054,10 @@
      private void handleLifecycleTransitionFailure(Throwable t)
            throws CacheException, RuntimeException, Error
      {
  +      int oldState = getState();
         cacheStatus = CacheStatus.FAILED;
  +      sendStateChangeNotification(oldState, getState(), getClass().getSimpleName() + " failed", t);
  +      
         if (t instanceof CacheException)
            throw (CacheException) t;
         else if (t instanceof RuntimeException)
  @@ -999,4 +1067,25 @@
         else
            throw new CacheException(t);
      }
  +   
  +   /**
  +    * Helper for sending out state change notifications
  +    */
  +   private void sendStateChangeNotification(int oldState, int newState, String msg, Throwable t)
  +   {
  +      if (isDisableStateChangeNotifications())
  +         return;
  +         
  +      long now = System.currentTimeMillis();
  +      
  +      AttributeChangeNotification stateChangeNotification = new AttributeChangeNotification(
  +         this,
  +         getNextNotificationSequenceNumber(), now, msg,
  +         "State", "java.lang.Integer",
  +         new Integer(oldState), new Integer(newState)
  +         );
  +      stateChangeNotification.setUserData(t);
  +      
  +      sendNotification(stateChangeNotification);      
  +   }
   }
  
  
  



More information about the jboss-cvs-commits mailing list