javax.management.MBeanServerFactory Java Examples

The following examples show how to use javax.management.MBeanServerFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MBeanTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private static void testCompliant(Class<?> iface, Object bean) throws Exception {
    try {
        System.out.println("Registering a compliant MBean " +
                            iface.getName() + " ...");

        MBeanServer mbs = MBeanServerFactory.newMBeanServer();
        ObjectName on = new ObjectName("test:type=Compliant");

        mbs.registerMBean(bean, on);
        success("Registered a compliant MBean - " + iface.getName());
    } catch (Exception e) {
        Throwable t = e;
        while (t != null && !(t instanceof NotCompliantMBeanException)) {
            t = t.getCause();
        }
        if (t != null) {
            fail("MBean not registered");
        } else {
            throw e;
        }
    }
}
 
Example #2
Source File: ImmutableNotificationInfoTest.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
private static boolean test(Object mbean, boolean expectImmutable)
        throws Exception {
    MBeanServer mbs = MBeanServerFactory.newMBeanServer();
    ObjectName on = new ObjectName("a:b=c");
    mbs.registerMBean(mbean, on);
    MBeanInfo mbi = mbs.getMBeanInfo(on);
    Descriptor d = mbi.getDescriptor();
    String immutableValue = (String) d.getFieldValue("immutableInfo");
    boolean immutable = ("true".equals(immutableValue));
    if (immutable != expectImmutable) {
        System.out.println("FAILED: " + mbean.getClass().getName() +
                " -> " + immutableValue);
        return false;
    } else {
        System.out.println("OK: " + mbean.getClass().getName());
        return true;
    }
}
 
Example #3
Source File: JMSTestBase.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Override
@Before
public void setUp() throws Exception {
   super.setUp();

   mbeanServer = MBeanServerFactory.createMBeanServer();

   Configuration config = createDefaultConfig(true).setSecurityEnabled(useSecurity()).
      addConnectorConfiguration("invm", new TransportConfiguration(INVM_CONNECTOR_FACTORY)).
      setTransactionTimeoutScanPeriod(100);
   config.getConnectorConfigurations().put("netty", new TransportConfiguration(NETTY_CONNECTOR_FACTORY));
   server = addServer(ActiveMQServers.newActiveMQServer(config, mbeanServer, usePersistence()));
   jmsServer = new JMSServerManagerImpl(server);
   namingContext = new InVMNamingContext();
   jmsServer.setRegistry(new JndiBindingRegistry(namingContext));
   jmsServer.start();

   registerConnectionFactory();
}
 
Example #4
Source File: MLetCommand.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    if (System.getSecurityManager() == null)
        throw new IllegalStateException("No security manager installed!");

    System.out.println("java.security.policy=" +
                       System.getProperty("java.security.policy"));

    // Instantiate the MBean server
    //
    System.out.println("Create the MBean server");
    MBeanServer mbs = MBeanServerFactory.createMBeanServer();
    // Register the MLetMBean
    //
    System.out.println("Create MLet MBean");
    ObjectName mlet = new ObjectName("MLetTest:name=MLetMBean");
    mbs.createMBean("javax.management.loading.MLet", mlet);
    // Test OK!
    //
    System.out.println("Bye! Bye!");
}
 
Example #5
Source File: MBeanFallbackTest.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
private static void testPrivate(Class<?> iface, Object bean) throws Exception {
    try {
        System.out.println("Registering a private MBean " +
                            iface.getName() + " ...");

        MBeanServer mbs = MBeanServerFactory.newMBeanServer();
        ObjectName on = new ObjectName("test:type=Compliant");

        mbs.registerMBean(bean, on);
        success("Registered a private MBean - " + iface.getName());
    } catch (Exception e) {
        Throwable t = e;
        while (t != null && !(t instanceof NotCompliantMBeanException)) {
            t = t.getCause();
        }
        if (t != null) {
            fail("MBean not registered");
        } else {
            throw e;
        }
    }
}
 
Example #6
Source File: JMXProxyFallbackTest.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
private static void testPrivate(Class<?> iface) throws Exception {
    try {
        System.out.println("Creating a proxy for private M(X)Bean " +
                            iface.getName() + " ...");

        MBeanServer mbs = MBeanServerFactory.newMBeanServer();
        ObjectName on = new ObjectName("test:type=Proxy");

        JMX.newMBeanProxy(mbs, on, iface);
        success("Created a proxy for private M(X)Bean - " + iface.getName());
    } catch (Exception e) {
        Throwable t = e;
        while (t != null && !(t instanceof NotCompliantMBeanException)) {
            t = t.getCause();
        }
        if (t != null) {
            fail("Proxy not created");
        } else {
            throw e;
        }
    }
}
 
Example #7
Source File: MBeanTest.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void testNonCompliant(Class<?> iface, Object bean) throws Exception {
    try {
        System.out.println("Registering a non-compliant MBean " +
                            iface.getName() + " ...");

        MBeanServer mbs = MBeanServerFactory.newMBeanServer();
        ObjectName on = new ObjectName("test:type=NonCompliant");

        mbs.registerMBean(bean, on);

        fail("Registered a non-compliant MBean - " + iface.getName());
    } catch (Exception e) {
        Throwable t = e;
        while (t != null && !(t instanceof NotCompliantMBeanException)) {
            t = t.getCause();
        }
        if (t != null) {
            success("MBean not registered");
        } else {
            throw e;
        }
    }
}
 
Example #8
Source File: MBeanFallbackTest.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void testPrivate(Class<?> iface, Object bean) throws Exception {
    try {
        System.out.println("Registering a private MBean " +
                            iface.getName() + " ...");

        MBeanServer mbs = MBeanServerFactory.newMBeanServer();
        ObjectName on = new ObjectName("test:type=Compliant");

        mbs.registerMBean(bean, on);
        success("Registered a private MBean - " + iface.getName());
    } catch (Exception e) {
        Throwable t = e;
        while (t != null && !(t instanceof NotCompliantMBeanException)) {
            t = t.getCause();
        }
        if (t != null) {
            fail("MBean not registered");
        } else {
            throw e;
        }
    }
}
 
Example #9
Source File: ListenerScaleTest.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    MBeanServer mbs = MBeanServerFactory.newMBeanServer();
    Sender sender = new Sender();
    mbs.registerMBean(sender, testObjectName);
    JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
    JMXConnectorServer cs =
        JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
    cs.start();
    JMXServiceURL addr = cs.getAddress();
    JMXConnector cc = JMXConnectorFactory.connect(addr);
    try {
        test(mbs, cs, cc);
    } finally {
        cc.close();
        cs.stop();
    }
}
 
Example #10
Source File: ImmutableNotificationInfoTest.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private static boolean test(Object mbean, boolean expectImmutable)
        throws Exception {
    MBeanServer mbs = MBeanServerFactory.newMBeanServer();
    ObjectName on = new ObjectName("a:b=c");
    mbs.registerMBean(mbean, on);
    MBeanInfo mbi = mbs.getMBeanInfo(on);
    Descriptor d = mbi.getDescriptor();
    String immutableValue = (String) d.getFieldValue("immutableInfo");
    boolean immutable = ("true".equals(immutableValue));
    if (immutable != expectImmutable) {
        System.out.println("FAILED: " + mbean.getClass().getName() +
                " -> " + immutableValue);
        return false;
    } else {
        System.out.println("OK: " + mbean.getClass().getName());
        return true;
    }
}
 
Example #11
Source File: ListenerScaleTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    MBeanServer mbs = MBeanServerFactory.newMBeanServer();
    Sender sender = new Sender();
    mbs.registerMBean(sender, testObjectName);
    JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
    JMXConnectorServer cs =
        JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
    cs.start();
    JMXServiceURL addr = cs.getAddress();
    JMXConnector cc = JMXConnectorFactory.connect(addr);
    try {
        test(mbs, cs, cc);
    } finally {
        cc.close();
        cs.stop();
    }
}
 
Example #12
Source File: ExceptionDiagnosisTest.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
private static void testCaseProb() throws Exception {
    MBeanServer mbs = MBeanServerFactory.newMBeanServer();
    ObjectName name = new ObjectName("a:b=c");
    mbs.registerMBean(new CaseProbImpl(), name);
    CaseProbMXBean proxy = JMX.newMXBeanProxy(mbs, name, CaseProbMXBean.class);
    try {
        CaseProb prob = proxy.getCaseProb();
        fail("No exception from proxy method getCaseProb");
    } catch (IllegalArgumentException e) {
        String messageChain = messageChain(e);
        if (messageChain.contains("URLPath")) {
            System.out.println("Message chain contains URLPath as required: "
                    + messageChain);
        } else {
            fail("Exception chain for CaseProb does not mention property" +
                    " URLPath differing only in case");
            System.out.println("Full stack trace:");
            e.printStackTrace(System.out);
        }
    }
}
 
Example #13
Source File: RuntimeExceptionTest.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test the monitor notifications.
 */
public int monitorNotifications() throws Exception {

    server = MBeanServerFactory.newMBeanServer();

    MBeanServerForwarderInvocationHandler mbsfih =
        (MBeanServerForwarderInvocationHandler)
        Proxy.getInvocationHandler(server);

    mbsfih.setGetAttributeException(
        new RuntimeException("Test RuntimeException"));

    domain = server.getDefaultDomain();

    obsObjName = ObjectName.getInstance(domain + ":type=ObservedObject");
    server.registerMBean(new ObservedObject(), obsObjName);

    echo(">>> ----------------------------------------");
    int error = counterMonitorNotification();
    echo(">>> ----------------------------------------");
    error += gaugeMonitorNotification();
    echo(">>> ----------------------------------------");
    error += stringMonitorNotification();
    echo(">>> ----------------------------------------");
    return error;
}
 
Example #14
Source File: MLetCommand.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    if (System.getSecurityManager() == null)
        throw new IllegalStateException("No security manager installed!");

    System.out.println("java.security.policy=" +
                       System.getProperty("java.security.policy"));

    // Instantiate the MBean server
    //
    System.out.println("Create the MBean server");
    MBeanServer mbs = MBeanServerFactory.createMBeanServer();
    // Register the MLetMBean
    //
    System.out.println("Create MLet MBean");
    ObjectName mlet = new ObjectName("MLetTest:name=MLetMBean");
    mbs.createMBean("javax.management.loading.MLet", mlet);
    // Test OK!
    //
    System.out.println("Bye! Bye!");
}
 
Example #15
Source File: ReplicatedTree.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
public ReplicatedTree(String groupname, String props, long state_fetch_timeout, boolean jmx) throws Exception {
        if(groupname != null)
            this.groupname=groupname;
        if(props != null)
            this.props=props;
//        this.jmx=jmx; GemStoneAddition
        this.state_fetch_timeout=state_fetch_timeout;
        channel=new JChannel(this.props);
        channel.connect(this.groupname);
        if(jmx) {
            ArrayList servers=MBeanServerFactory.findMBeanServer(null);
            if(servers == null || servers.size() == 0) {
                throw new Exception("No MBeanServers found;" +
                                    "\nJmxTest needs to be run with an MBeanServer present, or inside JDK 5");
            }
//            MBeanServer server=(MBeanServer)servers.get(0); GemStoneAddition
//            JmxConfigurator.registerChannel(channel, server, "JGroups:channel=" + channel.getChannelName() , true);
        }
        start();
    }
 
Example #16
Source File: ImmutableNotificationInfoTest.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private static boolean test(Object mbean, boolean expectImmutable)
        throws Exception {
    MBeanServer mbs = MBeanServerFactory.newMBeanServer();
    ObjectName on = new ObjectName("a:b=c");
    mbs.registerMBean(mbean, on);
    MBeanInfo mbi = mbs.getMBeanInfo(on);
    Descriptor d = mbi.getDescriptor();
    String immutableValue = (String) d.getFieldValue("immutableInfo");
    boolean immutable = ("true".equals(immutableValue));
    if (immutable != expectImmutable) {
        System.out.println("FAILED: " + mbean.getClass().getName() +
                " -> " + immutableValue);
        return false;
    } else {
        System.out.println("OK: " + mbean.getClass().getName());
        return true;
    }
}
 
Example #17
Source File: ExceptionDiagnosisTest.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void testCaseProb() throws Exception {
    MBeanServer mbs = MBeanServerFactory.newMBeanServer();
    ObjectName name = new ObjectName("a:b=c");
    mbs.registerMBean(new CaseProbImpl(), name);
    CaseProbMXBean proxy = JMX.newMXBeanProxy(mbs, name, CaseProbMXBean.class);
    try {
        CaseProb prob = proxy.getCaseProb();
        fail("No exception from proxy method getCaseProb");
    } catch (IllegalArgumentException e) {
        String messageChain = messageChain(e);
        if (messageChain.contains("URLPath")) {
            System.out.println("Message chain contains URLPath as required: "
                    + messageChain);
        } else {
            fail("Exception chain for CaseProb does not mention property" +
                    " URLPath differing only in case");
            System.out.println("Full stack trace:");
            e.printStackTrace(System.out);
        }
    }
}
 
Example #18
Source File: ListenerScaleTest.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    MBeanServer mbs = MBeanServerFactory.newMBeanServer();
    Sender sender = new Sender();
    mbs.registerMBean(sender, testObjectName);
    JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
    JMXConnectorServer cs =
        JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
    cs.start();
    JMXServiceURL addr = cs.getAddress();
    JMXConnector cc = JMXConnectorFactory.connect(addr);
    try {
        test(mbs, cs, cc);
    } finally {
        cc.close();
        cs.stop();
    }
}
 
Example #19
Source File: MBeanTest.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void testCompliant(Class<?> iface, Object bean) throws Exception {
    try {
        System.out.println("Registering a compliant MBean " +
                            iface.getName() + " ...");

        MBeanServer mbs = MBeanServerFactory.newMBeanServer();
        ObjectName on = new ObjectName("test:type=Compliant");

        mbs.registerMBean(bean, on);
        success("Registered a compliant MBean - " + iface.getName());
    } catch (Exception e) {
        Throwable t = e;
        while (t != null && !(t instanceof NotCompliantMBeanException)) {
            t = t.getCause();
        }
        if (t != null) {
            fail("MBean not registered");
        } else {
            throw e;
        }
    }
}
 
Example #20
Source File: MBeanFallbackTest.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private static void testPrivate(Class<?> iface, Object bean) throws Exception {
    try {
        System.out.println("Registering a private MBean " +
                            iface.getName() + " ...");

        MBeanServer mbs = MBeanServerFactory.newMBeanServer();
        ObjectName on = new ObjectName("test:type=Compliant");

        mbs.registerMBean(bean, on);
        success("Registered a private MBean - " + iface.getName());
    } catch (Exception e) {
        Throwable t = e;
        while (t != null && !(t instanceof NotCompliantMBeanException)) {
            t = t.getCause();
        }
        if (t != null) {
            fail("MBean not registered");
        } else {
            throw e;
        }
    }
}
 
Example #21
Source File: MBeanTest.java    From groovy with Apache License 2.0 6 votes vote down vote up
public void testGetProperty() throws Exception {
    MBeanServer mbeanServer = MBeanServerFactory.createMBeanServer();
    ObjectName name = new ObjectName("groovy.test:role=TestMBean,type=Dummy");
    // use Class.forName instead of new Dummy() to allow separate compilation
    mbeanServer.registerMBean(Class.forName("groovy.jmx.Dummy").getDeclaredConstructor().newInstance(), name);

    assertEquals("JMX value of Name", "James", mbeanServer.getAttribute(name, "Name"));

    GroovyMBean object = new GroovyMBean(mbeanServer, name);

    Object value = object.getProperty("Name");
    assertEquals("Name property", "James", value);

    object.setProperty("Name", "Bob");
    assertEquals("Name property", "Bob", object.getProperty("Name"));

    // now let's look up the name via JMX to check
    assertEquals("JMX value of Name", "Bob", mbeanServer.getAttribute(name, "Name"));

    assertEquals("Location : London|Name : Bob|Size : 12", join(sort(object.listAttributeValues()), "|"));
    assertEquals("start|stop", join(sort(object.listOperationNames()), "|"));
    assertEquals("void start()", join(sort(object.describeOperation("start")), "|"));
    assertEquals("(rw) java.lang.String Location", object.describeAttribute("Location"));
}
 
Example #22
Source File: MBeanServerFactoryBeanTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void withLocateExistingAndExistingServer() {
	MBeanServer server = MBeanServerFactory.createMBeanServer();
	try {
		MBeanServerFactoryBean bean = new MBeanServerFactoryBean();
		bean.setLocateExistingServerIfPossible(true);
		bean.afterPropertiesSet();
		try {
			MBeanServer otherServer = bean.getObject();
			assertSame("Existing MBeanServer not located", server, otherServer);
		}
		finally {
			bean.destroy();
		}
	}
	finally {
		MBeanServerFactory.releaseMBeanServer(server);
	}
}
 
Example #23
Source File: ClusterConnectionControl2Test.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Override
@Before
public void setUp() throws Exception {
   super.setUp();

   String discoveryName = RandomUtil.randomString();
   String groupAddress = getUDPDiscoveryAddress();
   int groupPort = getUDPDiscoveryPort();

   Map<String, Object> acceptorParams_1 = new HashMap<>();
   acceptorParams_1.put(TransportConstants.PORT_PROP_NAME, port_1);
   TransportConfiguration acceptorConfig_0 = new TransportConfiguration(ActiveMQTestBase.NETTY_ACCEPTOR_FACTORY);

   TransportConfiguration acceptorConfig_1 = new TransportConfiguration(ActiveMQTestBase.NETTY_ACCEPTOR_FACTORY, acceptorParams_1);

   TransportConfiguration connectorConfig_1 = new TransportConfiguration(ActiveMQTestBase.NETTY_CONNECTOR_FACTORY, acceptorParams_1);
   TransportConfiguration connectorConfig_0 = new TransportConfiguration(ActiveMQTestBase.NETTY_CONNECTOR_FACTORY);

   QueueConfiguration queueConfig = new QueueConfiguration(RandomUtil.randomString()).setDurable(false);
   List<String> connectorInfos = new ArrayList<>();
   connectorInfos.add("netty");

   BroadcastGroupConfiguration broadcastGroupConfig = new BroadcastGroupConfiguration().setName(discoveryName).setBroadcastPeriod(250).setConnectorInfos(connectorInfos).setEndpointFactory(new UDPBroadcastEndpointFactory().setGroupAddress(groupAddress).setGroupPort(groupPort));

   DiscoveryGroupConfiguration discoveryGroupConfig = new DiscoveryGroupConfiguration().setName(discoveryName).setRefreshTimeout(0).setDiscoveryInitialWaitTimeout(0).setBroadcastEndpointFactory(new UDPBroadcastEndpointFactory().setGroupAddress(groupAddress).setGroupPort(groupPort));

   clusterConnectionConfig_0 = new ClusterConnectionConfiguration().setName(clusterName).setAddress(queueConfig.getAddress().toString()).setConnectorName("netty").setRetryInterval(1000).setDuplicateDetection(false).setMessageLoadBalancingType(MessageLoadBalancingType.ON_DEMAND).setMaxHops(1).setConfirmationWindowSize(1024).setDiscoveryGroupName(discoveryName);

   Configuration conf_1 = createBasicConfig().addClusterConfiguration(clusterConnectionConfig_0).addAcceptorConfiguration(acceptorConfig_1).addConnectorConfiguration("netty", connectorConfig_1).addQueueConfiguration(queueConfig).addDiscoveryGroupConfiguration(discoveryName, discoveryGroupConfig).addBroadcastGroupConfiguration(broadcastGroupConfig);

   Configuration conf_0 = createBasicConfig(1).addClusterConfiguration(clusterConnectionConfig_0).addAcceptorConfiguration(acceptorConfig_0).addConnectorConfiguration("netty", connectorConfig_0).addDiscoveryGroupConfiguration(discoveryName, discoveryGroupConfig).addBroadcastGroupConfiguration(broadcastGroupConfig);

   mbeanServer_1 = MBeanServerFactory.createMBeanServer();
   server1 = addServer(ActiveMQServers.newActiveMQServer(conf_1, mbeanServer_1, false));

   server0 = addServer(ActiveMQServers.newActiveMQServer(conf_0, mbeanServer, false));
   server0.start();
   waitForServerToStart(server0);
}
 
Example #24
Source File: RandomMXBeanTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    MBeanServer mbs = MBeanServerFactory.newMBeanServer();
    ObjectName name = new ObjectName("a:b=c");
    StupidMXBean stupid = new StupidImpl();
    mbs.registerMBean(stupid, name);
    ObjectName referName = new ObjectName("a:c=d");
    mbs.registerMBean(new ReferImpl(stupid), referName);
    System.out.println(mbs.getMBeanInfo(name));
    StupidMXBean stupid2 = (StupidMXBean)
            Proxy.newProxyInstance(StupidMXBean.class.getClassLoader(),
                new Class<?>[] {StupidMXBean.class},
                new WrapInvocationHandler(stupid));
    ObjectName stupidName2 = new ObjectName("a:d=e");
    mbs.registerMBean(stupid2, stupidName2);
    Field zero = StupidMXBean.class.getField("ZERO");
    System.out.println("Zero field = " + zero.get(null));
    test(mbs, MerlinMXBean.class);
    test(mbs, TigerMXBean.class);

    StupidMXBean proxy = JMX.newMXBeanProxy(mbs, name, StupidMXBean.class);
    System.out.println("Zero = " + proxy.getZero());
    System.out.println("One = " + proxy.identity(1));
    ReferMXBean referProxy =
            JMX.newMXBeanProxy(mbs, referName, ReferMXBean.class);
    StupidMXBean stupidProxy2 = referProxy.getStupid();
    System.out.println("Same proxy: " + (proxy == stupidProxy2));
    Method[] methods = StupidMXBean.class.getMethods();
    for (Method method : methods) {
        if (method.getParameterTypes().length == 0)
            method.invoke(proxy, new Object[0]);
    }
}
 
Example #25
Source File: UnregisterMBeanExceptionTest.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

        // Instantiate the MBean server
        //
        System.out.println("Create the MBean server");
        MBeanServer mbs = MBeanServerFactory.createMBeanServer();

        // Register the MBean
        //
        System.out.println("Create a TestDynamicMBean");
        TestDynamicMBean obj = new TestDynamicMBean();
        ObjectName n = new ObjectName("d:k=v");
        System.out.println("Register a TestDynamicMBean");
        mbs.registerMBean(obj, n);
        obj.throwException = true;
        System.out.println("Unregister a TestDynamicMBean");
        try {
            mbs.unregisterMBean(n);
        } catch (Exception e) {
            throw new IllegalArgumentException("Test failed", e);
        }
        boolean isRegistered = mbs.isRegistered(n);
        System.out.println("Is MBean Registered? " + isRegistered);

        if (isRegistered) {
            throw new IllegalArgumentException(
                "Test failed: the MBean is still registered");
        } else {
            System.out.println("Test passed");
        }
    }
 
Example #26
Source File: MXBeanFallbackTest.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void testPrivateMXBean(String type, Object bean) throws Exception {
    System.out.println(type + " MXBean test...");
    MBeanServer mbs = MBeanServerFactory.newMBeanServer();
    ObjectName on = new ObjectName("test:type=" + type);
    try {
        mbs.registerMBean(bean, on);
        success("Private MXBean registered");
    } catch (NotCompliantMBeanException e) {
        failure("Failed to register the private MXBean - " +
                 bean.getClass().getInterfaces()[0].getName());
    }
}
 
Example #27
Source File: DefaultLoaderRepository.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private static Class<?> load(ClassLoader without, String className)
        throws ClassNotFoundException {
    final List<MBeanServer> mbsList = MBeanServerFactory.findMBeanServer(null);

    for (MBeanServer mbs : mbsList) {
        ClassLoaderRepository clr = mbs.getClassLoaderRepository();
        try {
            return clr.loadClassWithout(without, className);
        } catch (ClassNotFoundException e) {
            // OK : Try with next one...
        }
    }
    throw new ClassNotFoundException(className);
}
 
Example #28
Source File: RabbitMetricsBinder.java    From summerframework with Apache License 2.0 5 votes vote down vote up
private static MBeanServer getMBeanServer() {
    List<MBeanServer> mBeanServers = MBeanServerFactory.findMBeanServer(null);
    if (!mBeanServers.isEmpty()) {
        return mBeanServers.get(0);
    }
    return ManagementFactory.getPlatformMBeanServer();
}
 
Example #29
Source File: SameObjectTwoNamesTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    boolean expectException =
            (System.getProperty("jmx.mxbean.multiname") == null);
    try {
        ObjectName objectName1 = new ObjectName("test:index=1");
        ObjectName objectName2 = new ObjectName("test:index=2");
        MBeanServer mbs = MBeanServerFactory.createMBeanServer();
        MXBC_SimpleClass01 mxBeanObject = new MXBC_SimpleClass01();

        mbs.registerMBean(mxBeanObject, objectName1);

        mbs.registerMBean(mxBeanObject, objectName2);

        if (expectException) {
            throw new Exception("TEST FAILED: " +
                    "InstanceAlreadyExistsException was not thrown");
        } else
            System.out.println("Correctly got no exception with compat property");
    } catch (InstanceAlreadyExistsException e) {
        if (expectException) {
            System.out.println("Got expected InstanceAlreadyExistsException:");
            e.printStackTrace(System.out);
        } else {
            throw new Exception(
                    "TEST FAILED: Got exception even though compat property set", e);
        }
    }
    System.out.println("TEST PASSED");
}
 
Example #30
Source File: OldMBeanServerTest.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public MBeanServerConnection call() {
    MBeanServer mbs = MBeanServerFactory.newMBeanServer();
    try {
        JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
        JMXConnectorServer cs =
            JMXConnectorServerFactory.newJMXConnectorServer(
                url, null, mbs);
        cs.start();
        JMXServiceURL addr = cs.getAddress();
        connector = JMXConnectorFactory.connect(addr);
        return connector.getMBeanServerConnection();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}