public void export() { try { // 创建一个MBeanServer MBeanServer mbs = MBeanServerFactory.createMBeanServer(DOMAIN); // MBeanServer mbs = MBeanServerFactory.createMBeanServer();//不能在jconsole中使用 // MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();//可在jconsole中使用 // 用MBeanServer注册LoginStatsMBean // MBeanServer.registerMBean(Object,ObjectName)方法使用的参数有两个:一个是MBean实现的一个实例;另一个是类型ObjectName的一个对象-它用于唯一地标识该MBean mbs.registerMBean(new Status(), new ObjectName(MBeanName)); // 存取该JMX服务的URL: JMXServiceURL url = new JMXServiceURL("rmi", HOST, JMX_PORT, "/jndi/rmi://" + HOST + ":" + 1099 + "/app"); // start()和stop()来启动和停止 JMXConnectorServer JMXConnectorServer jmxServer = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs); serviceUrl = url.toString(); // 在RMI上注册 LocateRegistry.createRegistry(JMX_PORT); jmxServer.start(); // 创建适配器,用于能够通过浏览器访问MBean // HtmlAdaptorServer adapter = new HtmlAdaptorServer(); // adapter.setPort(9797); // mbs.registerMBean(adapter, new ObjectName( // "MyappMBean:name=htmladapter,port=9797")); // adapter.start(); } catch (Exception e) { e.printStackTrace(); } }
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 static void main(String[] args) throws Exception { Class thisClass = MethodResultTest.class; Class exoticClass = Exotic.class; String exoticClassName = Exotic.class.getName(); ClassLoader testClassLoader = thisClass.getClassLoader(); if (!(testClassLoader instanceof URLClassLoader)) { System.out.println("TEST INVALID: Not loaded by a " + "URLClassLoader: " + testClassLoader); System.exit(1); } URLClassLoader tcl = (URLClassLoader) testClassLoader; URL[] urls = tcl.getURLs(); ClassLoader shadowLoader = new ShadowLoader( urls, testClassLoader, new String[] { exoticClassName, ExoticMBeanInfo.class.getName(), ExoticException.class.getName() }); Class cl = shadowLoader.loadClass(exoticClassName); if (cl == exoticClass) { System.out.println( "TEST INVALID: Shadow class loader loaded " + "same class as test class loader"); System.exit(1); } Thread.currentThread().setContextClassLoader(shadowLoader); ObjectName on = new ObjectName("a:b=c"); MBeanServer mbs = MBeanServerFactory.newMBeanServer(); mbs.createMBean(Thing.class.getName(), on); final String[] protos = {"rmi", "iiop", "jmxmp"}; boolean ok = true; for (int i = 0; i < protos.length; i++) { try { ok &= test(protos[i], mbs, on); System.out.println(); } catch (Exception e) { System.out.println("TEST FAILED WITH EXCEPTION:"); e.printStackTrace(System.out); ok = false; } } if (ok) System.out.println("Test passed"); else { System.out.println("TEST FAILED"); System.exit(1); } }
public static void main(String[] args) throws Exception { System.out.println( "Test that target MBean class loader is used " + "before JMX Remote API class loader"); ClassLoader jmxRemoteClassLoader = JMXServiceURL.class.getClassLoader(); if (jmxRemoteClassLoader == null) { System.out.println( "JMX Remote API loaded by bootstrap " + "class loader, this test is irrelevant"); return; } if (!(jmxRemoteClassLoader instanceof URLClassLoader)) { System.out.println("TEST INVALID: JMX Remote API not loaded by " + "URLClassLoader"); System.exit(1); } URLClassLoader jrcl = (URLClassLoader) jmxRemoteClassLoader; URL[] urls = jrcl.getURLs(); PrivateMLet mlet = new PrivateMLet(urls, null, false); Class shadowClass = mlet.loadClass(JMXServiceURL.class.getName()); if (shadowClass == JMXServiceURL.class) { System.out.println("TEST INVALID: MLet got original " + "JMXServiceURL not shadow"); System.exit(1); } MBeanServer mbs = MBeanServerFactory.newMBeanServer(); mbs.registerMBean(mlet, mletName); final String[] protos = {"rmi", "iiop", "jmxmp"}; boolean ok = true; for (int i = 0; i < protos.length; i++) { try { ok &= test(protos[i], mbs); } catch (Exception e) { System.out.println("TEST FAILED WITH EXCEPTION:"); e.printStackTrace(System.out); ok = false; } } if (ok) System.out.println("Test passed"); else { System.out.println("TEST FAILED"); System.exit(1); } }