public void attemptManageC3P0Registry() { try { ObjectName name = new ObjectName(regName); C3P0RegistryManager mbean = new C3P0RegistryManager(); if (mbs.isRegistered(name)) { if (logger.isLoggable(MLevel.WARNING)) { logger.warning( "A C3P0Registry mbean is already registered. " + "This probably means that an application using c3p0 was undeployed, " + "but not all PooledDataSources were closed prior to undeployment. " + "This may lead to resource leaks over time. Please take care to close " + "all PooledDataSources."); } mbs.unregisterMBean(name); } mbs.registerMBean(mbean, name); } catch (Exception e) { if (logger.isLoggable(MLevel.WARNING)) logger.log( MLevel.WARNING, "Failed to set up C3P0RegistryManager mBean. " + "[c3p0 will still function normally, but management via JMX may not be possible.]", e); } }
private static void checkThreadInfo() throws Exception { // assume all threads stay alive long[] ids = (long[]) server.getAttribute(thread, "AllThreadIds"); Object result = server.invoke(thread, "getThreadInfo", new Object[] {ids}, new String[] {"[J"}); for (CompositeData cd : (CompositeData[]) result) { printThreadInfo(cd); } result = server.invoke( thread, "getThreadInfo", new Object[] {ids, new Integer(2)}, new String[] {"[J", "int"}); for (CompositeData cd : (CompositeData[]) result) { printThreadInfo(cd); } long id = Thread.currentThread().getId(); result = server.invoke(thread, "getThreadInfo", new Object[] {new Long(id)}, new String[] {"long"}); printThreadInfo((CompositeData) result); result = server.invoke( thread, "getThreadInfo", new Object[] {new Long(id), new Integer(2)}, new String[] {"long", "int"}); printThreadInfo((CompositeData) result); }
public static void main(String[] args) throws Exception { MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); final Boolean isNotificationSupported = AccessController.doPrivileged( new PrivilegedAction<Boolean>() { public Boolean run() { try { Class cl = Class.forName("sun.management.VMManagementImpl"); Field f = cl.getDeclaredField("gcNotificationSupport"); f.setAccessible(true); return f.getBoolean(null); } catch (ClassNotFoundException e) { return false; } catch (NoSuchFieldException e) { return false; } catch (IllegalAccessException e) { return false; } } }); if (!isNotificationSupported) { System.out.println("GC Notification not supported by the JVM, test skipped"); return; } final ObjectName gcMXBeanPattern = new ObjectName("java.lang:type=GarbageCollector,*"); Set<ObjectName> names = mbs.queryNames(gcMXBeanPattern, null); if (names.isEmpty()) throw new Exception("Test incorrect: no GC MXBeans"); number = names.size(); for (ObjectName n : names) { if (mbs.isInstanceOf(n, "javax.management.NotificationEmitter")) { listenerInvoked.put(n.getCanonicalName(), null); GcListener listener = new GcListener(); mbs.addNotificationListener(n, listener, null, null); } } // Invocation of System.gc() to trigger major GC System.gc(); // Allocation of many short living and small objects to trigger minor GC Object data[] = new Object[32]; for (int i = 0; i < 100000000; i++) { data[i % 32] = new int[8]; } int wakeup = 0; synchronized (synchronizer) { while (count != number) { synchronizer.wait(10000); wakeup++; if (wakeup > 10) break; } } for (GarbageCollectionNotificationInfo notif : listenerInvoked.values()) { checkGarbageCollectionNotificationInfoContent(notif); } System.out.println("Test passed"); }
private static void checkEnum() throws Exception { String type = (String) server.getAttribute(heapPool, "Type"); if (!type.equals("HEAP")) { throw new RuntimeException("TEST FAILED: " + " incorrect memory type for " + heapPool); } type = (String) server.getAttribute(nonHeapPool, "Type"); if (!type.equals("NON_HEAP")) { throw new RuntimeException("TEST FAILED: " + " incorrect memory type for " + nonHeapPool); } }
private static void checkPlatformMBeans() throws Exception { MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); Set<ObjectName> mbeanNames = mbs.queryNames(null, null); for (ObjectName name : mbeanNames) { if (!mbs.isInstanceOf(name, NotificationBroadcaster.class.getName())) { System.out.println(name + ": not a NotificationBroadcaster"); } else { MBeanInfo mbi = mbs.getMBeanInfo(name); check(name.toString(), mbi.getNotifications()); } } }
public void attemptUnmanageC3P0Registry() { try { ObjectName name = new ObjectName(regName); if (mbs.isRegistered(name)) { mbs.unregisterMBean(name); if (logger.isLoggable(MLevel.FINER)) logger.log(MLevel.FINER, "C3P0Registry mbean unregistered."); } else if (logger.isLoggable(MLevel.FINE)) logger.fine( "The C3P0Registry mbean was not found in the registry, so could not be unregistered."); } catch (Exception e) { if (logger.isLoggable(MLevel.WARNING)) logger.log( MLevel.WARNING, "An Exception occurred while trying to unregister the C3P0RegistryManager mBean." + e); } }
public void attemptUnmanagePooledDataSource(PooledDataSource pds) { String nameStr = getPdsObjectNameStr(pds); try { ObjectName name = new ObjectName(nameStr); if (mbs.isRegistered(name)) { mbs.unregisterMBean(name); if (logger.isLoggable(MLevel.FINER)) logger.log(MLevel.FINER, "MBean: " + nameStr + " unregistered."); } else if (logger.isLoggable(MLevel.FINE)) logger.fine( "The mbean " + nameStr + " was not found in the registry, so could not be unregistered."); } catch (Exception e) { if (logger.isLoggable(MLevel.WARNING)) logger.log( MLevel.WARNING, "An Exception occurred while unregistering mBean. [" + nameStr + "] " + e); } }
private static void checkSunGC() throws Exception { // Test com.sun.management proxy List<GarbageCollectorMXBean> gcs = getGarbageCollectorMXBeans(); for (GarbageCollectorMXBean gc : gcs) { ObjectName sunGc = new ObjectName(GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE + ",name=" + gc.getName()); CompositeData cd = (CompositeData) server.getAttribute(sunGc, "LastGcInfo"); if (cd != null) { System.out.println("GC statistic for : " + gc.getName()); printGcInfo(cd); } } }
public static void main(String[] argv) throws Exception { memory = new ObjectName(MEMORY_MXBEAN_NAME); runtime = new ObjectName(RUNTIME_MXBEAN_NAME); thread = new ObjectName(THREAD_MXBEAN_NAME); os = new ObjectName(OPERATING_SYSTEM_MXBEAN_NAME); List<MemoryPoolMXBean> pools = getMemoryPoolMXBeans(); for (MemoryPoolMXBean p : pools) { if (heapPool == null && p.getType() == MemoryType.HEAP && p.isUsageThresholdSupported() && p.isCollectionUsageThresholdSupported()) { heapPool = new ObjectName(MEMORY_POOL_MXBEAN_DOMAIN_TYPE + ",name=" + p.getName()); } if (nonHeapPool == null && p.getType() == MemoryType.NON_HEAP && p.isUsageThresholdSupported()) { nonHeapPool = new ObjectName(MEMORY_POOL_MXBEAN_DOMAIN_TYPE + ",name=" + p.getName()); } } // Check notification emitters MyListener listener = new MyListener(); server.addNotificationListener(memory, listener, null, null); server.removeNotificationListener(memory, listener); checkEnum(); checkList(); checkMap(); checkMemoryUsage(); checkThreadInfo(); checkOS(); checkSunGC(); System.out.println("Test passed."); }
private static void checkList() throws Exception { String[] args = (String[]) server.getAttribute(runtime, "InputArguments"); if (args.length < 1) { throw new RuntimeException("TEST FAILED: " + " empty input arguments"); } // check if -verbose:gc exists boolean found = false; for (String option : args) { if (option.equals(OPTION)) { found = true; break; } } if (!found) { throw new RuntimeException("TEST FAILED: " + "VM option " + OPTION + " not found"); } }
private static void checkMemoryUsage() throws Exception { // sanity check to have non-zero usage Object u1 = server.getAttribute(memory, "HeapMemoryUsage"); Object u2 = server.getAttribute(memory, "NonHeapMemoryUsage"); Object u3 = server.getAttribute(heapPool, "Usage"); Object u4 = server.getAttribute(nonHeapPool, "Usage"); if (getCommitted(u1) <= 0 || getCommitted(u2) <= 0 || getCommitted(u3) <= 0 || getCommitted(u4) <= 0) { throw new RuntimeException("TEST FAILED: " + " expected non-zero committed usage"); } server.invoke(memory, "gc", new Object[0], new String[0]); Object u5 = server.getAttribute(heapPool, "CollectionUsage"); if (getCommitted(u5) <= 0) { throw new RuntimeException("TEST FAILED: " + " expected non-zero committed collected usage"); } }
private static void checkMap() throws Exception { // Add new system properties System.setProperty(KEY1, VALUE1); System.setProperty(KEY2, VALUE2); TabularData props1 = (TabularData) server.getAttribute(runtime, "SystemProperties"); String value1 = getProperty(props1, KEY1); if (value1 == null || !value1.equals(VALUE1)) { throw new RuntimeException( "TEST FAILED: " + KEY1 + " property found" + " with value = " + value1 + " but expected to be " + VALUE1); } String value2 = getProperty(props1, KEY2); if (value2 == null || !value2.equals(VALUE2)) { throw new RuntimeException( "TEST FAILED: " + KEY2 + " property found" + " with value = " + value2 + " but expected to be " + VALUE2); } String value3 = getProperty(props1, KEY3); if (value3 != null) { throw new RuntimeException( "TEST FAILED: " + KEY3 + " property found" + " but should not exist"); } }
public void attemptManagePooledDataSource(PooledDataSource pds) { String name = null; try { name = getPdsObjectNameStr(pds); ObjectName oname = new ObjectName(name); if (mbs.isRegistered(oname)) { if (logger.isLoggable(MLevel.WARNING)) logger.warning( "You are attempting to register an mbean '" + name + "', but an mbean by that name is already registered. " + "The new mbean will replace the old one in the MBean server. " + (EXCLUDE_IDENTITY_TOKEN ? "Since you have excluded the guaranteed-unique identity token, you must take care to give each PooledDataSource a unique dataSourceName." : "This should not happen unless you have (pathologically) modified the DataSource's guaranteed-unique identityToken.")); } // PooledDataSourceManager mbean = new PooledDataSourceManager( pds ); // mbs.registerMBean(mbean, ObjectName.getInstance(name)); // if (logger.isLoggable(MLevel.FINER)) // logger.log(MLevel.FINER, "MBean: " + name + " registered."); // DynamicPooledDataSourceManagerMBean registers itself on construction (and logs its own // registration) DynamicPooledDataSourceManagerMBean mbean = new DynamicPooledDataSourceManagerMBean(pds, name, mbs); } catch (Exception e) { if (logger.isLoggable(MLevel.WARNING)) logger.log( MLevel.WARNING, "Failed to set up a PooledDataSourceManager mBean. [ " + (name == null ? pds.toString() : name) + " ] c3p0 will still function normally, but management of this DataSource by JMX may not be possible.", e); } }
private static void checkOS() throws Exception { Integer cpus = (Integer) server.getAttribute(os, "AvailableProcessors"); System.out.println("# CPUs = " + cpus); Long vmem = (Long) server.getAttribute(os, "CommittedVirtualMemorySize"); System.out.println("Committed virtual memory = " + vmem); }