| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 
 |  
import java.util.Arrays;
import java.util.Properties;
import java.util.Set;
import javax.management.ObjectName;
import javax.management.j2ee.ManagementHome;
import javax.management.j2ee.Management;
import javax.management.j2ee.statistics.JVMStats;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
 
public class HeapSizeRetriever {
 
	public static void main(String args[]) {
		try {
			Properties props = new Properties();
			props.setProperty(Context.PROVIDER_URL, "iiop://glenan:9999");
			Context ic = new InitialContext(props);
			Object obj = ic.lookup("ejb/mgmt/MEJB");
			ManagementHome mejbHome = (ManagementHome) PortableRemoteObject
					.narrow(obj, ManagementHome.class);
			Management mejb = mejbHome.create();
			ObjectName jvmQuery = new ObjectName("*:j2eeType=JVM,*");
			Set s = mejb.queryNames(jvmQuery, null);
			ObjectName jvmMBean = (ObjectName) s.iterator().next();
			boolean hasStats = ((Boolean) mejb.getAttribute(jvmMBean,
					"statisticsProvider")).booleanValue();
			if (hasStats) {
				JVMStats stats = (JVMStats) mejb
						.getAttribute(jvmMBean, "stats");
				String[] statisticNames = stats.getStatisticNames();
				if (Arrays.asList(statisticNames).contains("heapSize")) {
					System.out.println("Heap size: " + stats.getHeapSize());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
} | 
Partager