Java Code Examples for javax.management.MBeanServer#unregisterMBean()

The following examples show how to use javax.management.MBeanServer#unregisterMBean() . 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: TestAbandonedObjectPool.java    From commons-pool with Apache License 2.0 6 votes vote down vote up
@After
public void tearDown() throws Exception {
    final String poolName = pool.getJmxName().toString();
    pool.clear();
    pool.close();
    pool = null;

    final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    final Set<ObjectName> result = mbs.queryNames(new ObjectName(
            "org.apache.commoms.pool2:type=GenericObjectPool,*"), null);
    // There should be no registered pools at this point
    final int registeredPoolCount = result.size();
    final StringBuilder msg = new StringBuilder("Current pool is: ");
    msg.append(poolName);
    msg.append("  Still open pools are: ");
    for (final ObjectName name : result) {
        // Clean these up ready for the next test
        msg.append(name.toString());
        msg.append(" created via\n");
        msg.append(mbs.getAttribute(name, "CreationStackTrace"));
        msg.append('\n');
        mbs.unregisterMBean(name);
    }
    Assert.assertEquals(msg.toString(), 0, registeredPoolCount);
}
 
Example 2
Source File: FlumeEmbeddedAppenderTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@After
public void teardown() throws Exception {
    System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
    ctx.reconfigure();
    primary.stop();
    alternate.stop();
    final File file = new File("target/file-channel");
    deleteFiles(file);
    final MBeanServer server = ManagementFactory.getPlatformMBeanServer();
    final Set<ObjectName> names = server.queryNames(new ObjectName("org.apache.flume.*:*"), null);
    for (final ObjectName name : names) {
        try {
            server.unregisterMBean(name);
        } catch (final Exception ex) {
            System.out.println("Unable to unregister " + name.toString());
        }
    }
}
 
Example 3
Source File: ClientBroadcastStream.java    From red5-server-common with Apache License 2.0 5 votes vote down vote up
protected void unregisterJMX() {
    if (registerJMX) {
        if (StringUtils.isNotEmpty(publishedName) && !"false".equals(publishedName)) {
            MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
            try {
                ObjectName oName = new ObjectName(String.format("org.red5.server:type=ClientBroadcastStream,scope=%s,publishedName=%s", getScope().getName(), publishedName));
                mbs.unregisterMBean(oName);
            } catch (Exception e) {
                log.warn("Exception unregistering", e);
            }
        }
    }
}
 
Example 4
Source File: RepositoryWildcardTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static int mbeanDeletion(MBeanServer mbs, String name)
    throws Exception {
    int error = 0;
    try {
        System.out.println("Test: unregisterMBean(" + name + ")");
        mbs.unregisterMBean(ObjectName.getInstance(name));
        error++;
        System.out.println("Didn't get expected exception!");
        System.out.println("Test failed!");
    } catch (InstanceNotFoundException e) {
        System.out.println("Got expected exception = " + e.toString());
        System.out.println("Test passed!");
    }
    return error;
}
 
Example 5
Source File: RepositoryWildcardTest.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static int mbeanDeletion(MBeanServer mbs, String name)
    throws Exception {
    int error = 0;
    try {
        System.out.println("Test: unregisterMBean(" + name + ")");
        mbs.unregisterMBean(ObjectName.getInstance(name));
        error++;
        System.out.println("Didn't get expected exception!");
        System.out.println("Test failed!");
    } catch (InstanceNotFoundException e) {
        System.out.println("Got expected exception = " + e.toString());
        System.out.println("Test passed!");
    }
    return error;
}
 
Example 6
Source File: UnregisterMBeanExceptionTest.java    From openjdk-jdk8u-backup 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 7
Source File: JCacheJmxSupport.java    From cache2k with Apache License 2.0 5 votes vote down vote up
public void disableStatistics(Cache c) {
  MBeanServer mbs = mBeanServer;
  String _name = createStatisticsObjectName(c);
  try {
    mbs.unregisterMBean(new ObjectName(_name));
  } catch (InstanceNotFoundException ignore) {
  } catch (Exception e) {
    throw new IllegalStateException("Error unregister JMX bean, name='" + _name + "'", e);
  }
}
 
Example 8
Source File: PreRegisterNameTest.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    for (Class<?> c : new Class<?>[] {
            Spume.class, Thing.class, XSpume.class, XThing.class
         }) {
        for (ObjectName n : new ObjectName[] {null, new ObjectName("a:b=c")}) {
            System.out.println("Class " + c.getName() + " with name " + n +
                    "...");
            ObjectName realName = new ObjectName("a:type=" + c.getName());
            Constructor<?> constr = c.getConstructor(ObjectName.class);
            Object mbean = constr.newInstance(realName);
            ObjectInstance oi;
            String what =
                "Registering MBean of type " + c.getName() + " under name " +
                "<" + n + ">: ";
            try {
                oi = mbs.registerMBean(mbean, n);
            } catch (Exception e) {
                e.printStackTrace();
                fail(what + " got " + e);
                continue;
            }
            ObjectName registeredName = oi.getObjectName();
            if (!registeredName.equals(realName))
                fail(what + " registered as " + registeredName);
            if (!mbs.isRegistered(realName)) {
                fail(what + " not registered as expected");
            }
            mbs.unregisterMBean(registeredName);
        }
    }
    System.err.flush();
    if (failures == 0)
        System.out.println("TEST PASSED");
    else
        throw new Exception("TEST FAILED: " + failure);
}
 
Example 9
Source File: ReloadableEntityManagerFactory.java    From tomee with Apache License 2.0 5 votes vote down vote up
public void unregister() throws OpenEJBException {
    if (objectName != null) {
        final MBeanServer server = LocalMBeanServer.get();
        try {
            server.unregisterMBean(objectName);
        } catch (final Exception e) {
            throw new OpenEJBException("can't unregister the mbean for the entity manager factory " + getPUname(), e);
        }
    }
}
 
Example 10
Source File: RepositoryWildcardTest.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private static int mbeanDeletion(MBeanServer mbs, String name)
    throws Exception {
    int error = 0;
    try {
        System.out.println("Test: unregisterMBean(" + name + ")");
        mbs.unregisterMBean(ObjectName.getInstance(name));
        error++;
        System.out.println("Didn't get expected exception!");
        System.out.println("Test failed!");
    } catch (InstanceNotFoundException e) {
        System.out.println("Got expected exception = " + e.toString());
        System.out.println("Test passed!");
    }
    return error;
}
 
Example 11
Source File: UnitTestCommand.java    From camel-spring-boot with Apache License 2.0 5 votes vote down vote up
private void disableJmx(Set<String> disabledJmx) throws Exception {
    logger.info("Disabling JMX names: {}", disabledJmx);
    for (MBeanServer server : getMBeanServers()) {
        for (String jmxName : disabledJmx) {
            logger.info("Disabling JMX query {}", jmxName);

            ObjectName oName = new ObjectName(jmxName);
            Set<ObjectName> names = new HashSet<>(server.queryNames(oName, null));
            for (ObjectName name : names) {
                logger.info("Disabled JMX name {}", name);
                server.unregisterMBean(name);
            }
        }
    }
}
 
Example 12
Source File: UnregisterMBeanExceptionTest.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 {

        // 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 13
Source File: StatusManager.java    From das with Apache License 2.0 5 votes vote down vote up
private static void registerMBean(Object mBean, ObjectName name) throws Exception{
	MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
	if(mbs.isRegistered(name)) {
           mbs.unregisterMBean(name);
       }

	mbs.registerMBean(mBean, name);
}
 
Example 14
Source File: AnnotationTest.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 {
    System.out.println("Testing that annotations are correctly " +
                       "reflected in Descriptor entries");

    MBeanServer mbs =
        java.lang.management.ManagementFactory.getPlatformMBeanServer();
    ObjectName on = new ObjectName("a:b=c");

    Thing thing = new Thing();
    mbs.registerMBean(thing, on);
    check(mbs, on);
    mbs.unregisterMBean(on);

    ThingImpl thingImpl = new ThingImpl();
    mbs.registerMBean(thingImpl, on);
    Descriptor d = mbs.getMBeanInfo(on).getDescriptor();
    if (!d.getFieldValue("mxbean").equals("true")) {
        System.out.println("NOT OK: expected MXBean");
        failed = "Expected MXBean";
    }
    check(mbs, on);

    if (failed == null)
        System.out.println("Test passed");
    else
        throw new Exception("TEST FAILED: " + failed);
}
 
Example 15
Source File: AbstractAllocator.java    From lite-pool with Apache License 2.0 4 votes vote down vote up
@Override
protected long doStop(long timeout, TimeUnit unit) throws Exception {
    ObjectName n = new ObjectName(PREFIX + "(" + name + ")");
    MBeanServer m = ManagementFactory.getPlatformMBeanServer();
    if(m.isRegistered(n)) m.unregisterMBean(n); return timeout;
}
 
Example 16
Source File: TooManyFooTest.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
private static void test(Object child, String name, boolean mxbean)
    throws Exception {
    final ObjectName childName =
            new ObjectName("test:type=Child,name="+name);
    final MBeanServer server =
            ManagementFactory.getPlatformMBeanServer();
    server.registerMBean(child,childName);
    try {
        final MBeanInfo info = server.getMBeanInfo(childName);
        System.out.println(name+": " + info.getDescriptor());
        final int len = info.getOperations().length;
        if (len == OPCOUNT) {
            System.out.println(name+": OK, only "+OPCOUNT+
                    " operations here...");
        } else {
            final String qual = (len>OPCOUNT)?"many":"few";
            System.err.println(name+": Too "+qual+" foos! Found "+
                    len+", expected "+OPCOUNT);
            for (MBeanOperationInfo op : info.getOperations()) {
                System.err.println("public "+op.getReturnType()+" "+
                        op.getName()+"();");
            }
            throw new RuntimeException("Too " + qual +
                    " foos for "+name);
        }

        final Descriptor d = info.getDescriptor();
        final String mxstr = String.valueOf(d.getFieldValue("mxbean"));
        final boolean mxb =
                (mxstr==null)?false:Boolean.valueOf(mxstr).booleanValue();
        System.out.println(name+": mxbean="+mxb);
        if (mxbean && !mxb)
            throw new AssertionError("MXBean is not OpenMBean?");

        for (MBeanOperationInfo mboi : info.getOperations()) {

            // Sanity check
            if (mxbean && !mboi.getName().equals("foo")) {
                // The spec doesn't guarantee that the MBeanOperationInfo
                // of an MXBean will be an OpenMBeanOperationInfo, and in
                // some circumstances in our implementation it will not.
                // However, in thsi tests, for all methods but foo(),
                // it should.
                //
                if (!(mboi instanceof OpenMBeanOperationInfo))
                    throw new AssertionError("Operation "+mboi.getName()+
                            "() is not Open?");
            }

            final String exp = EXPECTED_TYPES.get(mboi.getName());

            // For MXBeans, we need to compare 'exp' with the original
            // type - because mboi.getReturnType() returns the OpenType
            //
            String type = (String)mboi.getDescriptor().
                        getFieldValue("originalType");
            if (type == null) type = mboi.getReturnType();
            if (type.equals(exp)) continue;
            System.err.println("Bad return type for "+
                    mboi.getName()+"! Found "+type+
                    ", expected "+exp);
            throw new RuntimeException("Bad return type for "+
                    mboi.getName());
        }
    } finally {
        server.unregisterMBean(childName);
    }
}
 
Example 17
Source File: TestHtmlJavaInformationsReport.java    From javamelody with Apache License 2.0 4 votes vote down vote up
/** Test.
 * @throws IOException e
 * @throws JMException e */
@Test
public void testTomcatInformations() throws IOException, JMException {
	final MBeanServer mBeanServer = MBeans.getPlatformMBeanServer();
	final List<ObjectName> mBeans = new ArrayList<ObjectName>();
	try {
		mBeans.add(mBeanServer
				.registerMBean(new ThreadPool(),
						new ObjectName("Catalina:type=ThreadPool,name=jk-8009"))
				.getObjectName());
		mBeans.add(
				mBeanServer
						.registerMBean(new GlobalRequestProcessor(),
								new ObjectName(
										"Catalina:type=GlobalRequestProcessor,name=jk-8009"))
						.getObjectName());
		TomcatInformations.initMBeans();
		final List<JavaInformations> myJavaInformationsList = Arrays
				.asList(new JavaInformations(null, true));
		final HtmlJavaInformationsReport htmlReport = new HtmlJavaInformationsReport(
				myJavaInformationsList, writer);
		htmlReport.toHtml();
		assertNotEmptyAndClear(writer);

		mBeans.add(mBeanServer
				.registerMBean(new ThreadPool(),
						new ObjectName("Catalina:type=ThreadPool,name=jk-8010"))
				.getObjectName());
		final GlobalRequestProcessor jk8010 = new GlobalRequestProcessor();
		jk8010.setrequestCount(0);
		mBeans.add(
				mBeanServer
						.registerMBean(jk8010,
								new ObjectName(
										"Catalina:type=GlobalRequestProcessor,name=jk-8010"))
						.getObjectName());
		TomcatInformations.initMBeans();
		final List<JavaInformations> myJavaInformationsList2 = Arrays
				.asList(new JavaInformations(null, true));
		final HtmlJavaInformationsReport htmlReport2 = new HtmlJavaInformationsReport(
				myJavaInformationsList2, writer);
		htmlReport2.toHtml();
		assertNotEmptyAndClear(writer);

		jk8010.setrequestCount(1000);
		final List<JavaInformations> myJavaInformationsList3 = Arrays
				.asList(new JavaInformations(null, true));
		final HtmlJavaInformationsReport htmlReport3 = new HtmlJavaInformationsReport(
				myJavaInformationsList3, writer);
		htmlReport3.toHtml();
		assertNotEmptyAndClear(writer);
	} finally {
		for (final ObjectName registeredMBean : mBeans) {
			mBeanServer.unregisterMBean(registeredMBean);
		}
		TomcatInformations.initMBeans();
	}
}
 
Example 18
Source File: ParserInfiniteLoopTest.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        boolean error = false;

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

        // Instantiate an MLet
        //
        System.out.println("Create the MLet");
        MLet mlet = new MLet();

        // Register the MLet MBean with the MBeanServer
        //
        System.out.println("Register the MLet MBean");
        ObjectName mletObjectName = new ObjectName("Test:type=MLet");
        mbs.registerMBean(mlet, mletObjectName);

        // Call getMBeansFromURL
        //
        System.out.println("Call mlet.getMBeansFromURL(<url>)");
        String testSrc = System.getProperty("test.src");
        System.out.println("test.src = " + testSrc);
        String urlCodebase;
        if (testSrc.startsWith("/")) {
            urlCodebase =
                "file:" + testSrc.replace(File.separatorChar, '/') + "/";
        } else {
            urlCodebase =
                "file:/" + testSrc.replace(File.separatorChar, '/') + "/";
        }
        String mletFile = urlCodebase + args[0];
        System.out.println("MLet File = " + mletFile);
        try {
            mlet.getMBeansFromURL(mletFile);
            System.out.println(
                "TEST FAILED: Expected ServiceNotFoundException not thrown");
            error = true;
        } catch (ServiceNotFoundException e) {
            if (e.getCause() == null) {
                System.out.println("TEST FAILED: Got unexpected null cause " +
                    "in ServiceNotFoundException");
                error = true;
            } else if (!(e.getCause() instanceof IOException)) {
                System.out.println("TEST FAILED: Got unexpected non-null " +
                    "cause in ServiceNotFoundException");
                error = true;
            } else {
                System.out.println("TEST PASSED: Got expected non-null " +
                    "cause in ServiceNotFoundException");
                error = false;
            }
            e.printStackTrace(System.out);
        }

        // Unregister the MLet MBean
        //
        System.out.println("Unregister the MLet MBean");
        mbs.unregisterMBean(mletObjectName);

        // Release MBean server
        //
        System.out.println("Release the MBean server");
        MBeanServerFactory.releaseMBeanServer(mbs);

        // End Test
        //
        System.out.println("Bye! Bye!");
        if (error) System.exit(1);
    }
 
Example 19
Source File: GetMBeansFromURLTest.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        boolean error = false;

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

        // Instantiate an MLet
        //
        System.out.println("Create the MLet");
        MLet mlet = new MLet();

        // Register the MLet MBean with the MBeanServer
        //
        System.out.println("Register the MLet MBean");
        ObjectName mletObjectName = new ObjectName("Test:type=MLet");
        mbs.registerMBean(mlet, mletObjectName);

        // Call getMBeansFromURL
        //
        System.out.println("Call mlet.getMBeansFromURL(<url>)");
        try {
            mlet.getMBeansFromURL("bogus://whatever");
            System.out.println("TEST FAILED: Expected " +
                               ServiceNotFoundException.class +
                               " exception not thrown.");
            error = true;
        } catch (ServiceNotFoundException e) {
            if (e.getCause() == null) {
                System.out.println("TEST FAILED: Got null cause in " +
                                   ServiceNotFoundException.class +
                                   " exception.");
                error = true;
            } else {
                System.out.println("TEST PASSED: Got non-null cause in " +
                                   ServiceNotFoundException.class +
                                   " exception.");
                error = false;
            }
            e.printStackTrace(System.out);
        }

        // Unregister the MLet MBean
        //
        System.out.println("Unregister the MLet MBean");
        mbs.unregisterMBean(mletObjectName);

        // Release MBean server
        //
        System.out.println("Release the MBean server");
        MBeanServerFactory.releaseMBeanServer(mbs);

        // End Test
        //
        System.out.println("Bye! Bye!");
        if (error) System.exit(1);
    }
 
Example 20
Source File: GetMBeansFromURLTest.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        boolean error = false;

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

        // Instantiate an MLet
        //
        System.out.println("Create the MLet");
        MLet mlet = new MLet();

        // Register the MLet MBean with the MBeanServer
        //
        System.out.println("Register the MLet MBean");
        ObjectName mletObjectName = new ObjectName("Test:type=MLet");
        mbs.registerMBean(mlet, mletObjectName);

        // Call getMBeansFromURL
        //
        System.out.println("Call mlet.getMBeansFromURL(<url>)");
        try {
            mlet.getMBeansFromURL("bogus://whatever");
            System.out.println("TEST FAILED: Expected " +
                               ServiceNotFoundException.class +
                               " exception not thrown.");
            error = true;
        } catch (ServiceNotFoundException e) {
            if (e.getCause() == null) {
                System.out.println("TEST FAILED: Got null cause in " +
                                   ServiceNotFoundException.class +
                                   " exception.");
                error = true;
            } else {
                System.out.println("TEST PASSED: Got non-null cause in " +
                                   ServiceNotFoundException.class +
                                   " exception.");
                error = false;
            }
            e.printStackTrace(System.out);
        }

        // Unregister the MLet MBean
        //
        System.out.println("Unregister the MLet MBean");
        mbs.unregisterMBean(mletObjectName);

        // Release MBean server
        //
        System.out.println("Release the MBean server");
        MBeanServerFactory.releaseMBeanServer(mbs);

        // End Test
        //
        System.out.println("Bye! Bye!");
        if (error) System.exit(1);
    }