leave SigarProxy impl to the caller

This commit is contained in:
Doug MacEachern 2009-04-29 12:58:45 -07:00
parent 0eb3672642
commit 1c87169bdb
6 changed files with 37 additions and 111 deletions

View File

@ -68,7 +68,7 @@ public class Mx extends SigarCommandBase {
if (isRegistered) { if (isRegistered) {
return; return;
} }
SigarRegistry registry = new SigarRegistry(); SigarRegistry registry = new SigarRegistry(this.proxy);
try { try {
server.registerMBean(registry, null); server.registerMBean(registry, null);
SigarProcess proc = new SigarProcess(); SigarProcess proc = new SigarProcess();

View File

@ -31,7 +31,6 @@ import javax.management.ReflectionException;
import org.hyperic.sigar.Sigar; import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException; import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.SigarProxy; import org.hyperic.sigar.SigarProxy;
import org.hyperic.sigar.SigarProxyCache;
/** /**
* Base class for all Sigar JMX MBeans. Provides a skeleton which handles * Base class for all Sigar JMX MBeans. Provides a skeleton which handles
@ -49,21 +48,6 @@ public abstract class AbstractMBean implements DynamicMBean, MBeanRegistration {
public static final String MBEAN_DOMAIN = SigarInvokerJMX.DOMAIN_NAME; public static final String MBEAN_DOMAIN = SigarInvokerJMX.DOMAIN_NAME;
public static final String MBEAN_ATTR_TYPE = SigarInvokerJMX.PROP_TYPE; public static final String MBEAN_ATTR_TYPE = SigarInvokerJMX.PROP_TYPE;
protected static final short CACHED_30SEC = 0;
protected static final short CACHED_5SEC = 1;
protected static final short CACHED_500MS = 2;
protected static final short CACHELESS = 3;
protected static final short DEFAULT = CACHED_30SEC;
/**
* The Sigar implementation to be used to fetch information from the system.
*/
protected final Sigar sigarImpl;
/** /**
* The Sigar proxy cache to be used in case the data does not have to be * The Sigar proxy cache to be used in case the data does not have to be
* fetched during each call. The cache timeout is decided during * fetched during each call. The cache timeout is decided during
@ -85,59 +69,13 @@ public abstract class AbstractMBean implements DynamicMBean, MBeanRegistration {
protected MBeanServer mbeanServer; protected MBeanServer mbeanServer;
/** /**
* <p>Creates a new instance of this class. The Sigar instance is stored (and * <p>Creates a new instance of this class. The SigarProxy instance is stored (and
* accessible) via the {@link #sigarImpl} member. A second instance is * accessible) via the {@link #sigar} member.
* stored within the {@link #sigar} member which is either {@link #sigarImpl}
* or an instance of {@link SigarProxyCache} with the expiration time set to
* whatever the <code>cacheMode</code> parameter specifies.</p>
* *
* <p>The following cache modes exist:</p> * @param sigar The SigarProxy instance to use. Must not be <code>null</code>
*
* <table border = "1">
* <tr><td><b>Constant</b></td><td><b>Description</b></td></tr>
* <tr><td>{@link #CACHELESS}</td><td>No cached instance, {@link #sigar}
* <code>==</code> {@link #sigarImpl}.</td></tr>
* <tr><td>{@link #CACHED_500MS}</td><td>500 millisecond cache, for high
* frequency queries on raw data such as reading out CPU timers each
* second. Avoids reading out multiple data sets when all attributes of
* an MBean are queried in short sequence.</td></tr>
* <tr><td>{@link #CACHED_5SEC}</td><td>5 second cache, for high frequency
* queries on calculated data such as CPU percentages.</td></tr>
* <tr><td>{@link #CACHED_30SEC}</td><td>30 second cache, for normal queries
* or data readouts such as CPU model / vendor. This is the default if
* nothing (<code>0</code>) is specified.</td></tr>
* <tr><td>{@link #DEFAULT}</td><td>Same as {@link #CACHED_30SEC}.</td></tr>
* </table>
*
* <p><b>Note:</b> Only make use of the cacheless or half second mode if you
* know what you are doing. They may have impact on system performance if
* used excessively.</p>
*
* @param sigar The Sigar impl to use. Must not be <code>null</code>
* @param cacheMode The cache mode to use for {@link #sigar} or {@link #CACHELESS}
* if no separate, cached instance is to be maintained.
*/ */
protected AbstractMBean(Sigar sigar, short cacheMode) { protected AbstractMBean(SigarProxy sigar) {
// store Sigar this.sigar = sigar;
this.sigarImpl = sigar;
// create a cached instance as well
if (cacheMode == CACHELESS) {
// no cached version
this.sigar = this.sigarImpl;
} else if (cacheMode == CACHED_500MS) {
// 500ms cached version (for 1/sec queries)
this.sigar = SigarProxyCache.newInstance(this.sigarImpl, 500);
} else if (cacheMode == CACHED_5SEC) {
// 5sec cached version (for avg'd queries)
this.sigar = SigarProxyCache.newInstance(this.sigarImpl, 5000);
} else /* if (cacheMode == CACHED_30SEC) */{
// 30sec (default) cached version (for info and long term queries)
this.sigar = SigarProxyCache.newInstance(this.sigarImpl, 30000);
}
} }
/** /**

View File

@ -29,7 +29,7 @@ import javax.management.MBeanException;
import javax.management.MBeanInfo; import javax.management.MBeanInfo;
import javax.management.ReflectionException; import javax.management.ReflectionException;
import org.hyperic.sigar.Sigar; import org.hyperic.sigar.SigarProxy;
public class ReflectedMBean extends AbstractMBean { public class ReflectedMBean extends AbstractMBean {
@ -96,15 +96,15 @@ public class ReflectedMBean extends AbstractMBean {
} }
} }
protected ReflectedMBean(Sigar sigar, String type) { protected ReflectedMBean(SigarProxy sigar, String type) {
super(sigar, CACHED_5SEC); super(sigar);
this.type = type; this.type = type;
this.name = this.name =
MBEAN_DOMAIN + ":" + MBEAN_DOMAIN + ":" +
MBEAN_ATTR_TYPE + "=" + getType(); MBEAN_ATTR_TYPE + "=" + getType();
} }
protected ReflectedMBean(Sigar sigar, String type, String arg) { protected ReflectedMBean(SigarProxy sigar, String type, String arg) {
this(sigar, type); this(sigar, type);
this.name += ",Name=" + encode(arg); this.name += ",Name=" + encode(arg);
} }

View File

@ -29,6 +29,7 @@ import javax.management.ReflectionException;
import org.hyperic.sigar.Sigar; import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException; import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.SigarNotImplementedException; import org.hyperic.sigar.SigarNotImplementedException;
import org.hyperic.sigar.SigarProxy;
/** /**
* Sigar JMX MBean implementation for the <code>LoadAverage</code> information * Sigar JMX MBean implementation for the <code>LoadAverage</code> information
@ -122,16 +123,6 @@ public class SigarLoadAverage extends AbstractMBean {
*/ */
private boolean notImplemented; private boolean notImplemented;
/**
* Creates a new instance, using a new Sigar instance to fetch the data.
*
* @throws IllegalArgumentException
* If an unexpected Sigar error occurs.
*/
public SigarLoadAverage() throws IllegalArgumentException {
this(new Sigar());
}
/** /**
* Creates a new instance, using the Sigar instance specified to fetch the * Creates a new instance, using the Sigar instance specified to fetch the
* data. * data.
@ -142,8 +133,8 @@ public class SigarLoadAverage extends AbstractMBean {
* @throws IllegalArgumentException * @throws IllegalArgumentException
* If an unexpected Sigar error occurs * If an unexpected Sigar error occurs
*/ */
public SigarLoadAverage(Sigar sigar) throws IllegalArgumentException { public SigarLoadAverage(SigarProxy sigar) throws IllegalArgumentException {
super(sigar, CACHED_30SEC); super(sigar);
// all fine // all fine
this.objectName = MBEAN_DOMAIN + ":" + MBEAN_ATTR_TYPE this.objectName = MBEAN_DOMAIN + ":" + MBEAN_ATTR_TYPE
@ -164,7 +155,7 @@ public class SigarLoadAverage extends AbstractMBean {
*/ */
public double getLastMinute() { public double getLastMinute() {
try { try {
return sigarImpl.getLoadAverage()[0]; return this.sigar.getLoadAverage()[0];
} catch (SigarNotImplementedException e) { } catch (SigarNotImplementedException e) {
return NOT_IMPLEMENTED_LOAD_VALUE; return NOT_IMPLEMENTED_LOAD_VALUE;
@ -181,7 +172,7 @@ public class SigarLoadAverage extends AbstractMBean {
*/ */
public double getLastFiveMinutes() { public double getLastFiveMinutes() {
try { try {
return sigarImpl.getLoadAverage()[1]; return this.sigar.getLoadAverage()[1];
} catch (SigarNotImplementedException e) { } catch (SigarNotImplementedException e) {
return NOT_IMPLEMENTED_LOAD_VALUE; return NOT_IMPLEMENTED_LOAD_VALUE;
@ -197,7 +188,7 @@ public class SigarLoadAverage extends AbstractMBean {
*/ */
public double getLast15Minutes() { public double getLast15Minutes() {
try { try {
return sigarImpl.getLoadAverage()[2]; return this.sigar.getLoadAverage()[2];
} catch (SigarNotImplementedException e) { } catch (SigarNotImplementedException e) {
return NOT_IMPLEMENTED_LOAD_VALUE; return NOT_IMPLEMENTED_LOAD_VALUE;

View File

@ -33,6 +33,7 @@ import org.hyperic.sigar.FileSystem;
import org.hyperic.sigar.NetInterfaceConfig; import org.hyperic.sigar.NetInterfaceConfig;
import org.hyperic.sigar.Sigar; import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException; import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.SigarProxy;
/** /**
* <p>Registry of all Sigar MBeans. Can be used as a convenient way to invoke * <p>Registry of all Sigar MBeans. Can be used as a convenient way to invoke
@ -90,12 +91,8 @@ public class SigarRegistry extends AbstractMBean {
private final ArrayList managedBeans; private final ArrayList managedBeans;
/** public SigarRegistry(SigarProxy sigar) {
* Creates a new instance of this class. Will create the Sigar instance this super(sigar);
* class uses when constructing other MBeans.
*/
public SigarRegistry() {
super(new Sigar(), CACHELESS);
this.objectName = MBEAN_DOMAIN + ":" + MBEAN_ATTR_TYPE this.objectName = MBEAN_DOMAIN + ":" + MBEAN_ATTR_TYPE
+ "=" + MBEAN_TYPE; + "=" + MBEAN_TYPE;
this.managedBeans = new ArrayList(); this.managedBeans = new ArrayList();
@ -166,26 +163,26 @@ public class SigarRegistry extends AbstractMBean {
for (int i=0; i<info.length; i++) { for (int i=0; i<info.length; i++) {
String idx = String.valueOf(i); String idx = String.valueOf(i);
mbean = mbean =
new ReflectedMBean(sigarImpl, "CpuCore", idx); new ReflectedMBean(this.sigar, "CpuCore", idx);
mbean.setType("CpuList"); mbean.setType("CpuList");
registerMBean(mbean); registerMBean(mbean);
mbean = mbean =
new ReflectedMBean(sigarImpl, "CpuCoreUsage", idx); new ReflectedMBean(this.sigar, "CpuCoreUsage", idx);
mbean.setType("CpuPercList"); mbean.setType("CpuPercList");
registerMBean(mbean); registerMBean(mbean);
} }
mbean = new ReflectedMBean(sigarImpl, "Cpu"); mbean = new ReflectedMBean(this.sigar, "Cpu");
mbean.putAttributes(info[0]); mbean.putAttributes(info[0]);
registerMBean(mbean); registerMBean(mbean);
mbean = new ReflectedMBean(sigarImpl, "CpuUsage"); mbean = new ReflectedMBean(this.sigar, "CpuUsage");
mbean.setType("CpuPerc"); mbean.setType("CpuPerc");
registerMBean(mbean); registerMBean(mbean);
//FileSystem beans //FileSystem beans
try { try {
FileSystem[] fslist = sigarImpl.getFileSystemList(); FileSystem[] fslist = this.sigar.getFileSystemList();
for (int i=0; i<fslist.length; i++) { for (int i=0; i<fslist.length; i++) {
FileSystem fs = fslist[i]; FileSystem fs = fslist[i];
if (fs.getType() != FileSystem.TYPE_LOCAL_DISK) { if (fs.getType() != FileSystem.TYPE_LOCAL_DISK) {
@ -193,7 +190,7 @@ public class SigarRegistry extends AbstractMBean {
} }
String name = fs.getDirName(); String name = fs.getDirName();
mbean = mbean =
new ReflectedMBean(sigarImpl, "FileSystem", name); new ReflectedMBean(this.sigar, "FileSystem", name);
mbean.setType(mbean.getType() + "Usage"); mbean.setType(mbean.getType() + "Usage");
mbean.putAttributes(fs); mbean.putAttributes(fs);
registerMBean(mbean); registerMBean(mbean);
@ -204,18 +201,18 @@ public class SigarRegistry extends AbstractMBean {
//NetInterface beans //NetInterface beans
try { try {
String[] ifnames = sigarImpl.getNetInterfaceList(); String[] ifnames = this.sigar.getNetInterfaceList();
for (int i=0; i<ifnames.length; i++) { for (int i=0; i<ifnames.length; i++) {
String name = ifnames[i]; String name = ifnames[i];
NetInterfaceConfig ifconfig = NetInterfaceConfig ifconfig =
this.sigar.getNetInterfaceConfig(name); this.sigar.getNetInterfaceConfig(name);
try { try {
sigarImpl.getNetInterfaceStat(name); this.sigar.getNetInterfaceStat(name);
} catch (SigarException e) { } catch (SigarException e) {
continue; continue;
} }
mbean = mbean =
new ReflectedMBean(sigarImpl, "NetInterface", name); new ReflectedMBean(this.sigar, "NetInterface", name);
mbean.setType(mbean.getType() + "Stat"); mbean.setType(mbean.getType() + "Stat");
mbean.putAttributes(ifconfig); mbean.putAttributes(ifconfig);
registerMBean(mbean); registerMBean(mbean);
@ -225,22 +222,22 @@ public class SigarRegistry extends AbstractMBean {
} }
//network info bean //network info bean
mbean = new ReflectedMBean(sigarImpl, "NetInfo"); mbean = new ReflectedMBean(this.sigar, "NetInfo");
try { try {
mbean.putAttribute("FQDN", sigarImpl.getFQDN()); mbean.putAttribute("FQDN", this.sigar.getFQDN());
} catch (SigarException e) { } catch (SigarException e) {
} }
registerMBean(mbean); registerMBean(mbean);
//physical memory bean //physical memory bean
registerMBean(new ReflectedMBean(sigarImpl, "Mem")); registerMBean(new ReflectedMBean(this.sigar, "Mem"));
//swap memory bean //swap memory bean
registerMBean(new ReflectedMBean(sigarImpl, "Swap")); registerMBean(new ReflectedMBean(this.sigar, "Swap"));
//load average bean //load average bean
registerMBean(new SigarLoadAverage(sigarImpl)); registerMBean(new SigarLoadAverage(this.sigar));
//global process stats //global process stats
registerMBean(new ReflectedMBean(sigarImpl, "ProcStat")); registerMBean(new ReflectedMBean(this.sigar, "ProcStat"));
//sigar version //sigar version
registerMBean(new ReflectedMBean(sigarImpl, "SigarVersion")); registerMBean(new ReflectedMBean(this.sigar, "SigarVersion"));
} }
/** /**

View File

@ -73,10 +73,10 @@ public class TestMx extends SigarTestCase {
return; return;
} }
SigarRegistry rgy = new SigarRegistry(); SigarRegistry rgy = new SigarRegistry(getSigar());
ObjectName name = new ObjectName(rgy.getObjectName()); ObjectName name = new ObjectName(rgy.getObjectName());
if (!server.isRegistered(name)) { if (!server.isRegistered(name)) {
server.registerMBean(new SigarRegistry(), name); server.registerMBean(rgy, name);
} }
Set beans = Set beans =
server.queryNames(new ObjectName("sigar:*"), null); server.queryNames(new ObjectName("sigar:*"), null);