|
package coherencetest;
import java.text.DecimalFormat; import java.util.Map; import java.util.Set; import java.util.TreeMap;
import javax.management.MBeanServer; import javax.management.MBeanServerFactory; import javax.management.ObjectName;
import com.tangosol.net.CacheFactory;
import java.io.IOException;
import javax.management.MBeanServerConnection; import javax.management.remote.JMXConnector; import javax.management.remote.JMXConnectorFactory; import javax.management.remote.JMXServiceURL;
public class CalculateTheSizeOfPeopleCache {
@SuppressWarnings({ "unchecked", "rawtypes" }) private void run() throws Exception {
// Enable JMX support in this Coherence data grid session... System.setProperty("tangosol.coherence.management", "all");
// Create a sample cache just to access the data grid... CacheFactory.getCache(MBeanServerFactory.class.getName());
// Gets the JMX server from Coherence data grid... MBeanServer jmxServer = getJMXServer(); System.out.println(jmxServer.toString()); if (jmxServer != null) System.out.println("can not get jmxServer");
//MBeanServerConnection jmxServer = getJMXServer();
// Creates a internal data structure that would maintain // the statistics from each cache in the data grid... Map cacheList = new TreeMap(); Set jmxObjectList = jmxServer.queryNames(new ObjectName("Coherence:type=Cache,*"), null); if (jmxObjectList !=null) { System.out.println("can not get jmxOBjectList"); System.out.println(jmxObjectList.size()); } for (Object jmxObject : jmxObjectList) { System.out.println("Enter"); ObjectName jmxObjectName = (ObjectName) jmxObject; String cacheName = jmxObjectName.getKeyProperty("name"); if (cacheName.equals(MBeanServerFactory.class.getName())) { continue; } else { cacheList.put(cacheName, new Statistics(cacheName)); } }
// Updates the internal data structure with statistic data // retrieved from caches inside the in-memory data grid... Set<String> cacheNames = cacheList.keySet(); for (String cacheName : cacheNames) { Set resultSet = jmxServer.queryNames( new ObjectName("Coherence:type=Cache,name=" + cacheName + ",*"), null); for (Object resultSetRef : resultSet) { ObjectName objectName = (ObjectName) resultSetRef; if (objectName.getKeyProperty("tier").equals("back")) { int unit = (Integer) jmxServer.getAttribute(objectName, "Units"); int size = (Integer) jmxServer.getAttribute(objectName, "Size"); Statistics statistics = (Statistics) cacheList.get(cacheName); statistics.incrementUnit(unit); statistics.incrementSize(size); cacheList.put(cacheName, statistics); } } }
// Finally... print the objects from the internal data // structure that represents the statistics from caches... cacheNames = cacheList.keySet(); for (String cacheName : cacheNames) { Statistics estatisticas = (Statistics) cacheList.get(cacheName); System.out.println(estatisticas); }
} /* public static MBeanServerConnection getJMXServer() throws IOException { JMXServiceURL url = new JMXServiceURL("service://..."); JMXConnector jmxc = JMXConnectorFactory.connect(url, null); return jmxc.getMBeanServerConnection(); } */ public MBeanServer getJMXServer() { MBeanServer jmxServer = null; for (Object jmxServerRef : MBeanServerFactory.findMBeanServer(null)) { jmxServer = (MBeanServer) jmxServerRef; System.out.println(jmxServer.getDefaultDomain().toString()); if (jmxServer.getDefaultDomain().equals(DEFAULT_DOMAIN) || DEFAULT_DOMAIN.length() == 0) { break; } jmxServer = null; } if (jmxServer == null) { jmxServer = MBeanServerFactory.createMBeanServer(DEFAULT_DOMAIN); } return jmxServer; }
private class Statistics {
private long unit; private long size; private String cacheName;
public Statistics(String cacheName) { this.cacheName = cacheName; }
public void incrementUnit(long unit) { this.unit += unit; }
public void incrementSize(long size) { this.size += size; }
public long getUnit() { return unit; }
public long getSize() { return size; }
public double getUnitInMB() { return unit / (1024.0 * 1024.0); }
public double getAverageSize() { return size == 0 ? 0 : unit / size; }
public String toString() { StringBuffer sb = new StringBuffer(); sb.append("\nCache Statistics of '").append(cacheName).append("':\n"); sb.append(" - Total Entries of Cache -----> " + getSize()).append("\n"); sb.append(" - Used Memory (Bytes) --------> " + getUnit()).append("\n"); sb.append(" - Used Memory (MB) -----------> " + FORMAT.format(getUnitInMB())).append("\n"); sb.append(" - Object Average Size --------> " + FORMAT.format(getAverageSize())).append("\n"); return sb.toString(); }
}
public static void main(String[] args) throws Exception { new CalculateTheSizeOfPeopleCache().run(); }
public static final DecimalFormat FORMAT = new DecimalFormat("###.###"); public static final String DEFAULT_DOMAIN = "DefaultDomain"; public static final String DOMAIN_NAME = "Coherence"; //public static final String DOMAIN_NAME = "enie's cluster";
}
|