⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmbeanattribute.java

📁 jmx codeJava源码
💻 JAVA
字号:

package book.jmx.examples;

import javax.management.*;
import javax.management.loading.*;
import javax.management.modelmbean.*;
import java.lang.reflect.*;


public class XMBeanAttribute implements XMBeanConstants {

  protected XMBean mbean           = null;
  protected Object managedResource = null;
  protected String name            = null;
  
  
  // Constructor.
  
  public XMBeanAttribute(XMBean mbean,
      ModelMBeanAttributeInfo attrInfo) throws MBeanException {
      
    this.mbean           = mbean;
    this.managedResource = mbean.resource;
    this.name            = attrInfo.getName();
  }

  
  public Object getValue() throws MBeanException,
      ReflectionException, AttributeNotFoundException {
    
    ModelMBeanInfo mbeanInfo = (ModelMBeanInfo)mbean.getMBeanInfo();
    ModelMBeanAttributeInfo attrInfo = mbeanInfo.getAttribute(name);
    
    // see if we're allowed to read this value
    if (!attrInfo.isReadable())
      throw new AttributeNotFoundException("Attribute is not readable");

      
    Descriptor desc    = attrInfo.getDescriptor();
    Descriptor mmbDesc = mbeanInfo.getMBeanDescriptor();
    
    // retrieve the relevant values from the attribute's descriptor
    long lastUpdate    = 0;
    long currTimeLimit = -1;
    Object field       = null;    
    Object result      = desc.getFieldValue(VALUE);
    Object getMethod   = desc.getFieldValue(GET_METHOD);
  
    // last update timestamp to check cache validity
    if((field = desc.getFieldValue(LAST_UPDATED_TIME_STAMP))!=null)
      lastUpdate  = Long.parseLong(field.toString());
  
    // get currencyTimeLimit from MBean descriptor first, overwrite
    // with attribute's descriptor field, if available
    if((field = mmbDesc.getFieldValue(CURRENCY_TIME_LIMIT))!=null)
      currTimeLimit = Long.parseLong(field.toString()) * 1000;
    if((field = desc.getFieldValue(CURRENCY_TIME_LIMIT))!=null)
      currTimeLimit = Long.parseLong(field.toString()) * 1000;
          
    // if getMethod is specified and cache is stale, invoke it            
    if (getMethod != null) {
      long time        = System.currentTimeMillis();
      
      if (time > lastUpdate + currTimeLimit) {
        
        result = mbean.invoke((String)getMethod, null, null);
      
        // update descriptor
        desc.setField(VALUE, result);
        desc.setField(LAST_UPDATED_TIME_STAMP, "" + time);
        mbeanInfo.setDescriptor(desc, "attribute");    
      }
    }
        
    return result;
  }

  public boolean setValue(Object value) throws MBeanException,
      ReflectionException, InstanceNotFoundException  {

    ModelMBeanInfo mbeanInfo = (ModelMBeanInfo)mbean.getMBeanInfo();
    ModelMBeanAttributeInfo attrInfo = mbeanInfo.getAttribute(name);

    // check if we're allowed to write this attribute
    if (!attrInfo.isWritable())
      throw new RuntimeException("Attribute is not writeable");
    
    Descriptor desc    = attrInfo.getDescriptor();
    Descriptor mmbDesc = mbeanInfo.getMBeanDescriptor();
    
    // retrieve the relevant descriptor values
    Object setMethod   = desc.getFieldValue(SET_METHOD);
    Object oldValue    = desc.getFieldValue(VALUE);
    Object newValue    = value;
      
    // if setMethod specified, invoke it
    if (setMethod != null) {
      mbean.invoke(
          (String)setMethod,
          new Object[] { value},
          new String[] { attrInfo.getType() }
      );
    }
        

    long persistPeriod   = 0;
    long lastUpdate      = 0;
    Object field         = null;
    String persistPolicy = mmbDesc.getFieldValue(PERSIST_POLICY).
                           toString();
    
    if ((field = desc.getFieldValue(PERSIST_POLICY))!=null)
      persistPolicy = field.toString();

    if ((field = mmbDesc.getFieldValue(PERSIST_PERIOD))!=null)
      persistPeriod = Long.parseLong(field.toString());
    
    if ((field = desc.getFieldValue(PERSIST_PERIOD))!=null)
      persistPeriod = Long.parseLong(field.toString());

    if ((field = desc.getFieldValue(LAST_UPDATED_TIME_STAMP))!=null)
      lastUpdate    = Long.parseLong(field.toString());

    // update descriptor
    desc.setField(LAST_UPDATED_TIME_STAMP, 
        "" + System.currentTimeMillis());
    desc.setField(VALUE, value);
    mbeanInfo.setDescriptor(desc, "attribute");    


    // send attribute change notification
    mbean.sendAttributeChangeNotification(
        new Attribute(name, oldValue),
        new Attribute(name, newValue)
    );
      
    // persistence
    if (persistPolicy != null) {
      if (persistPolicy.equalsIgnoreCase(ON_UPDATE)) {
        mbean.store();
        return true;
      }
      else if (persistPolicy.equalsIgnoreCase(NO_MORE_OFTEN_THAN)) {
        long interval = System.currentTimeMillis() - lastUpdate;
        if (interval > persistPeriod) {
          mbean.store();
          return true;
        }
        else
          return false;
      }
    }
        
    return false;
  }
  
}




⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -