All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gemstone.gemfire.admin.jmx.internal.SystemMemberJmx Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License. You
 * may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License. See accompanying
 * LICENSE file.
 */

package com.gemstone.gemfire.admin.jmx.internal;

import java.util.concurrent.atomic.AtomicInteger;

import com.gemstone.gemfire.LogWriter;
import com.gemstone.gemfire.SystemFailure;
import com.gemstone.gemfire.admin.*;
import com.gemstone.gemfire.cache.Operation;
import com.gemstone.gemfire.i18n.LogWriterI18n;
import com.gemstone.gemfire.internal.admin.ClientMembershipMessage;
import com.gemstone.gemfire.internal.i18n.LocalizedStrings;

import javax.management.MBeanException;
import javax.management.MalformedObjectNameException;
import javax.management.Notification;
import javax.management.NotificationListener;
import javax.management.ObjectName;
import javax.management.RuntimeOperationsException;
import javax.naming.OperationNotSupportedException;

import org.apache.commons.modeler.ManagedBean;

/**
 * Defines methods that all SystemMember MBeans should
 * implement.
 *
 * @author David Whitlock
 * @since 4.0
 */
public interface SystemMemberJmx
  extends SystemMember, NotificationListener {
  /** 
   * Notification type for indicating a cache got created on a member of this 
   * distributed system.
   */
  public static final String NOTIF_CACHE_CREATED = 
    "gemfire.distributedsystem.cache.created";
  /** 
   * Notification type for indicating a cache is closed on a member of this 
   * distributed system.
   */
  public static final String NOTIF_CACHE_CLOSED = 
    "gemfire.distributedsystem.cache.closed";
  /** 
   * Notification type for indicating a region is created in a cache on a member 
   * of this distributed system.
   */
  public static final String NOTIF_REGION_CREATED = 
    "gemfire.distributedsystem.cache.region.created";
  /** 
   * Notification type for indicating a region was removed from a cache on a 
   * member of this distributed system.
   */
  public static final String NOTIF_REGION_LOST = 
    "gemfire.distributedsystem.cache.region.lost";
  
  /** Notification type for indicating client joined */
  public static final String NOTIF_CLIENT_JOINED = 
    "gemfire.distributedsystem.cache.client.joined";
  
  /** Notification type for indicating client left */
  public static final String NOTIF_CLIENT_LEFT =
    "gemfire.distributedsystem.cache.client.left";
  
  /** Notification type for indicating client crashed */
  public static final String NOTIF_CLIENT_CRASHED =
    "gemfire.distributedsystem.cache.client.crashed";

  /**
   * Gets the interval in seconds between config refreshes
   *
   * @return the current refresh interval in seconds
   */
  public int getRefreshInterval();

  /**
   * RefreshInterval is now set only through the AdminDistributedSystem property
   * refreshInterval. Attempt to set refreshInterval on SystemMemberJmx MBean
   * would result in an OperationNotSupportedException Auto-refresh is enabled
   * on demand when a call to refreshConfig is made
   *
   * @param refreshInterval
   *          the new refresh interval in seconds
   * @deprecated since 6.0 use DistributedSystemConfig.refreshInterval instead
   */
  @Deprecated
  public void setRefreshInterval(int refreshInterval) throws OperationNotSupportedException;

  /**
   * Sets the refresh interval field.
   * Sets interval in seconds between config refreshes; zero or less turns 
   * off auto refreshing.  Manual refreshing has no effect on when the next
   * scheduled refresh will occur.
   */
  public void _setRefreshInterval(int refreshInterval);

  /** 
   * Gets this member's cache.
   *
   * @return ObjectName for this member's cache
   *
   * @throws AdminException
   *         If this system member does not host a cache
   */
  public ObjectName manageCache() 
    throws AdminException, MalformedObjectNameException;

  /** 
   * Gets all active StatisticResources for this manager.
   *
   * @return array of ObjectName instances
   */
  public ObjectName[] manageStats() 
    throws AdminException, MalformedObjectNameException;

  /** 
   * Gets the active StatisticResources for this manager, based on the
   * typeName as the key
   *
   * @return ObjectName of StatisticResourceJMX instance
   */
  public ObjectName[] manageStat(String statisticsTypeName) 
    throws AdminException, MalformedObjectNameException;

  /**
   * Handles notification to refresh. Reacts by refreshing the values of this
   * GemFireManager's ConfigurationParamaters.  Any other notification is
   * ignored.
   *
   * @param notification  the JMX notification being received
   * @param hb            handback object is unused
   */
  public void handleNotification(Notification notification, Object hb);

  /**
   * Add MBean attribute definitions for each ConfigurationParameter.
   *
   * @param managed the mbean definition to add attributes to
   * @return a new instance of ManagedBean copied from managed but 
   *         with the new attributes added
   */
  public ManagedBean addDynamicAttributes(ManagedBean managed) 
    throws AdminException;


  /**
   * Implementation should handle creation of cache by extracting the details
   * from the given event object.
   * 
   * @param event
   *          event object corresponding to the creation of the cache
   */
  public void handleCacheCreate(SystemMemberCacheEvent event);

  /**
   * Implementation should handle closure of cache by extracting the details
   * from the given event object.
   * 
   * @param event
   *          event object corresponding to the closure of the cache
   */
  public void handleCacheClose(SystemMemberCacheEvent event);

  /**
   * Implementation should handle creation of region by extracting the details
   * from the given event object.
   * 
   * @param event
   *          event object corresponding to the creation of a region
   */
  public void handleRegionCreate(SystemMemberRegionEvent event);

  /**
   * Implementation should handle loss of region by extracting the details
   * from the given event object.
   * 
   * @param event
   *          event object corresponding to the loss of a region
   */
  public void handleRegionLoss(SystemMemberRegionEvent event);

  /**
   * Implementation should handle client membership changes.
   * 
   * @param clientId
   *          id of the client for whom membership change happened
   * @param eventType
   *          membership change type; one of
   *          {@link ClientMembershipMessage#JOINED},
   *          {@link ClientMembershipMessage#LEFT}, 
   *          {@link ClientMembershipMessage#CRASHED}
   */
  public void handleClientMembership(String clientId, int eventType);

  //////////////////////  Inner Classess  //////////////////////

  /**
   * A helper class that provides implementation of the
   * SystemMemberJmx interface as static methods.
   */
  public static class Helper {
    private static AtomicInteger notificationSequenceNumber = new AtomicInteger();

    public static int setAndReturnRefreshInterval(SystemMemberJmx member,
                                          int refreshInterval) {
      LogWriterI18n logWriter = member.getDistributedSystem().getLogWriter().convertToLogWriterI18n();
      int ret = refreshInterval;
      
      try {
        MBeanUtil.registerRefreshNotification(
          member, // NotificationListener
          ((ManagedResource)member).getMBeanName(), // User Data
          RefreshNotificationType.SYSTEM_MEMBER_CONFIG, 
          refreshInterval); // int

      } catch (RuntimeException e) {
        logWriter.warning(e); // dead in water, print, and then ignore
        ret = 0; // zero out to avoid more exceptions

      } catch (Error e) {
        if (SystemFailure.isJVMFailureError(e)) {
          SystemFailure.initiateFailure(e);
          // If this ever returns, rethrow the error. We're poisoned
          // now, so don't let this thread continue.
          throw e;
        }
        // Whenever you catch Error or Throwable, you must also
        // check for fatal JVM error (see above).  However, there is
        // _still_ a possibility that you are dealing with a cascading
        // error condition, so you also need to check to see if the JVM
        // is still usable:
        SystemFailure.checkFailure();
        logWriter.error(e); // dead in water, print, and then ignore
        ret = 0; // zero out to avoid more exceptions
      }
      
      return ret;
    }

    public static ObjectName manageCache(SystemMemberJmx member) 
      throws AdminException, MalformedObjectNameException {
      LogWriterI18n logWriter = member.getDistributedSystem().getLogWriter().convertToLogWriterI18n();
      boolean IthrewIt = false;
      try {
        SystemMemberCache cache = member.getCache();
        if (cache == null) {
          IthrewIt = true;
          throw new AdminException(LocalizedStrings.SystemMemberJmx_THIS_SYSTEM_MEMBER_DOES_NOT_HAVE_A_CACHE.toLocalizedString());
        }
//        Assert.assertTrue(cache != null); (cannot be null)
        SystemMemberCacheJmxImpl cacheJmx = (SystemMemberCacheJmxImpl) cache;
        return ObjectName.getInstance(cacheJmx.getMBeanName());
      } catch (AdminException e) { 
        if (!IthrewIt) {
          logWriter.warning(e); 
        }
        throw e; 
      } catch (RuntimeException e) { 
        logWriter.warning(e);
        throw e;
      } catch (Error e) { 
        if (SystemFailure.isJVMFailureError(e)) {
          SystemFailure.initiateFailure(e);
          // If this ever returns, rethrow the error. We're poisoned
          // now, so don't let this thread continue.
          throw e;
        }
        // Whenever you catch Error or Throwable, you must also
        // check for fatal JVM error (see above).  However, there is
        // _still_ a possibility that you are dealing with a cascading
        // error condition, so you also need to check to see if the JVM
        // is still usable:
        SystemFailure.checkFailure();
        logWriter.error(e); 
        throw e; 
      }
    }

    public static ObjectName[] manageStats(SystemMemberJmx member) 
      throws AdminException, MalformedObjectNameException {
      LogWriterI18n logWriter = member.getDistributedSystem().getLogWriter().convertToLogWriterI18n();
        try {
          StatisticResource[] stats = member.getStats();
          ObjectName[] onames = new ObjectName[stats.length];
          for (int i = 0; i < stats.length; i++) {
            StatisticResourceJmxImpl stat =
              (StatisticResourceJmxImpl) stats[i];
            onames[i] = ObjectName.getInstance(stat.getMBeanName());
          }
          return onames;
        } catch (AdminException e) { 
          logWriter.warning(e); 
          throw e;
        } catch (RuntimeException e) {
          logWriter.warning(e); 
          throw e; 
        } catch (Error e) { 
          if (SystemFailure.isJVMFailureError(e)) {
            SystemFailure.initiateFailure(e);
            // If this ever returns, rethrow the error. We're poisoned
            // now, so don't let this thread continue.
            throw e;
          }
          // Whenever you catch Error or Throwable, you must also
          // check for fatal JVM error (see above).  However, there is
          // _still_ a possibility that you are dealing with a cascading
          // error condition, so you also need to check to see if the JVM
          // is still usable:
          SystemFailure.checkFailure();
          logWriter.error(e); 
          throw e; 
        }
      }

    public static ObjectName[] manageStat(SystemMemberJmx member, String statisticsTypeName) 
    throws AdminException, MalformedObjectNameException {
      LogWriterI18n logWriter = member.getDistributedSystem().getLogWriter().convertToLogWriterI18n();
      try {
        StatisticResource[] stats = member.getStat(statisticsTypeName);
        if (stats==null)
          return null;
        else {
          ObjectName[] statNames = new ObjectName[stats.length]; 
          for (int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy