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

The following examples show how to use javax.management.MBeanServer#isRegistered() . 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: JmxTaskProcessingSimulator.java    From javasimon with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private static SimonManagerMXBeanImpl register() {
	MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
	try {
		ObjectName name = new ObjectName("org.javasimon.jmx.example:type=Simon");
		if (mbs.isRegistered(name)) {
			mbs.unregisterMBean(name);
		}
		SimonManagerMXBeanImpl simon = new SimonManagerMXBeanImpl(SimonManager.manager());
		mbs.registerMBean(simon, name);
		System.out.println("SimonManagerMXBean registerd under name: " + name);
		return simon;
	} catch (JMException e) {
		System.out.println("SimonManagerMXBean registration failed!\n" + e);
	}
	return null;
}
 
Example 2
Source File: Scope.java    From red5-server-common with Apache License 2.0 6 votes vote down vote up
protected void registerJMX() {
    // register with jmx
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    try {
        String cName = this.getClass().getName();
        if (cName.indexOf('.') != -1) {
            cName = cName.substring(cName.lastIndexOf('.')).replaceFirst("[\\.]", "");
        }
        oName = new ObjectName(String.format("org.red5.server:type=%s,name=%s", cName, name));
        // don't reregister
        if (!mbs.isRegistered(oName)) {
            mbs.registerMBean(new StandardMBean(this, ScopeMXBean.class, true), oName);
        }
    } catch (Exception e) {
        log.warn("Error on jmx registration", e);
    }
}
 
Example 3
Source File: Utils.java    From luxun with Apache License 2.0 6 votes vote down vote up
/**
 * Register the given mbean with the platform mbean server,
 * unregistering any mbean that was there before. Note, this method
 * will not throw an exception if the registration fails (since there
 * is nothing you can do and it isn't fatal), instead it just returns
 * false indicating the registration failed.
 * 
 * @param mbean The object to register as an mbean
 * @param name The name to register this mbean with
 * @returns true if the registration succeeded
 */
static boolean registerMBean(Object mbean, String name) {
    try {
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        synchronized (mbs) {
            ObjectName objName = new ObjectName(name);
            if (mbs.isRegistered(objName)) {
                mbs.unregisterMBean(objName);
            }
            mbs.registerMBean(mbean, objName);
        }
        return true;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}
 
Example 4
Source File: EVCacheConnectionObserver.java    From EVCache with Apache License 2.0 6 votes vote down vote up
private void setupMonitoring(boolean shutdown) {
    try {
        final ObjectName mBeanName = ObjectName.getInstance("com.netflix.evcache:Group=" + client.getAppName()
                + ",SubGroup=pool,SubSubGroup=" + client.getServerGroupName()+ ",SubSubSubGroup=" + client.getId());
        final MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
        if (mbeanServer.isRegistered(mBeanName)) {
            if (log.isDebugEnabled()) log.debug("MBEAN with name " + mBeanName
                    + " has been registered. Will unregister the previous instance and register a new one.");
            mbeanServer.unregisterMBean(mBeanName);
        }
        if (!shutdown) {
            mbeanServer.registerMBean(this, mBeanName);
        }
    } catch (Exception e) {
        if (log.isWarnEnabled()) log.warn(e.getMessage(), e);
    }
}
 
Example 5
Source File: JMXModule.java    From olat with Apache License 2.0 6 votes vote down vote up
@Override
public void init() {
	if (!enableJMXsupport) {
		log.info("JMX support disabled.");
		return;
	} else {
		log.info("JMX support enabled.");
	}
	// expose MBeans
	final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
	try {
		final ObjectName name = new ObjectName(JMXHelper.buildRegisteredObjectName(jmxBeanClass.getClass(), WebappHelper.getServletContextPath()));
		if (!mbs.isRegistered(name)) {
			mbs.registerMBean(jmxBeanClass, name);
		}
	} catch (final Exception e) {
		throw new StartupException("Error instantiating JMX bean: ", e);
	}
}
 
Example 6
Source File: ViburMonitoring.java    From vibur-dbcp with Apache License 2.0 5 votes vote down vote up
static void unregisterMBean(ViburDBCPDataSource dataSource) {
    try {
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        ObjectName objectName = new ObjectName(dataSource.getJmxName());
        if (mbs.isRegistered(objectName)) {
            mbs.unregisterMBean(objectName);
        }
        else {
            logger.debug(dataSource.getJmxName() + " is not registered.");
        }
    } catch (JMException e) {
        logger.warn("Unable to unregister mBean {}", dataSource.getJmxName(), e);
    }
}
 
Example 7
Source File: Pool.java    From mariadb-connector-j with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void registerJmx() throws Exception {
  MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
  String jmxName = poolTag.replace(":", "_");
  ObjectName name = new ObjectName("org.mariadb.jdbc.pool:type=" + jmxName);

  if (!mbs.isRegistered(name)) {
    mbs.registerMBean(this, name);
  }
}
 
Example 8
Source File: JmxRegistration.java    From caffeine with Apache License 2.0 5 votes vote down vote up
/** Registers the management bean with the given object name. */
private static void register(ObjectName objectName, Object mbean) {
  MBeanServer server = ManagementFactory.getPlatformMBeanServer();
  try {
    if (!server.isRegistered(objectName)) {
      server.registerMBean(mbean, objectName);
    }
  } catch (InstanceAlreadyExistsException
      | MBeanRegistrationException | NotCompliantMBeanException e) {
    throw new CacheException("Error registering " + objectName, e);
  }
}
 
Example 9
Source File: PreRegisterNameTest.java    From jdk8u-dev-jdk 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 10
Source File: PreRegisterNameTest.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 {
    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 11
Source File: UnregisterMBeanExceptionTest.java    From jdk8u60 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 12
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 13
Source File: MonitoredApplication.java    From javasimon with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static void unregister() {
	MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
	try {
		ObjectName name = new ObjectName("org.javasimon.jmx.example:type=Simon");
		if (mbs.isRegistered(name)) {
			mbs.unregisterMBean(name);
		}
		System.out.println("SimonManagerMXBean was unregisterd");
	} catch (JMException e) {
		System.out.println("SimonManagerMXBean unregistration failed!\n" + e);
	}
}
 
Example 14
Source File: EVCacheScheduledExecutor.java    From EVCache with Apache License 2.0 5 votes vote down vote up
private void setupMonitoring(String name) {
    try {
        ObjectName mBeanName = ObjectName.getInstance("com.netflix.evcache:Group=ThreadPool,SubGroup="+name);
        MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
        if (mbeanServer.isRegistered(mBeanName)) {
            if (log.isDebugEnabled()) log.debug("MBEAN with name " + mBeanName + " has been registered. Will unregister the previous instance and register a new one.");
            mbeanServer.unregisterMBean(mBeanName);
        }
        mbeanServer.registerMBean(this, mBeanName);
    } catch (Exception e) {
        if (log.isDebugEnabled()) log.debug("Exception", e);
    }

}
 
Example 15
Source File: UnregisterMBeanExceptionTest.java    From openjdk-8-source 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 16
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 17
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 18
Source File: RunningStateJmx.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public static void registerMBean(ProcessStateNotifier processStateNotifier, SuspendController suspendController, RunningModeControl runningModeControl, boolean isServer) {
    try {
        final ObjectName name = new ObjectName(OBJECT_NAME);
        final MBeanServer server = ManagementFactory.getPlatformMBeanServer();
        final RunningStateJmxMBean mbean = new RunningStateJmx(name, runningModeControl, isServer);
        if (server.isRegistered(name)) {
            server.unregisterMBean(name);
        }
        server.registerMBean(mbean, name);
        registerStateListener(mbean, processStateNotifier);
        if (suspendController != null) {
            suspendController.addListener(new OperationListener() {
                @Override
                public void suspendStarted() {
                    mbean.setRunningState(mbean.getRunningState(), RunningState.SUSPENDING);
                }

                @Override
                public void complete() {
                    mbean.setRunningState(mbean.getRunningState(), RunningState.SUSPENDED);
                }

                @Override
                public void cancelled() {
                    if(mbean.getRunningState() == RunningState.STARTING) {
                        mbean.setRunningState(RunningState.STARTING, RunningState.SUSPENDED);
                    }
                    if (mbean.getRunningMode() == RunningMode.NORMAL) {
                        mbean.setRunningState(mbean.getRunningState(), RunningState.NORMAL);
                    } else {
                        mbean.setRunningState(mbean.getRunningState(),RunningState.ADMIN_ONLY);
                    }
                }

                @Override
                public void timeout() {
                }
            });
        } else {
            mbean.setRunningState(null, RunningState.STARTING);
        }
    } catch (InstanceAlreadyExistsException | InstanceNotFoundException | MBeanRegistrationException | MalformedObjectNameException | NotCompliantMBeanException e) {
        throw new RuntimeException(e);
    }
}
 
Example 19
Source File: AbstractAllocator.java    From lite-pool with Apache License 2.0 4 votes vote down vote up
@Override
protected void doStart() throws Exception {
    final MBeanServer m = ManagementFactory.getPlatformMBeanServer();
    final ObjectName n = new ObjectName(PREFIX + "(" + this.name + ")");
    if(m.isRegistered(n)) m.unregisterMBean(n); m.registerMBean(this, n);
}
 
Example 20
Source File: JmxMBeanAgent.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
/**
   * Unregister an MBean from the MBeanServer.
   * 
   * @param aMBean
   *          the MBean to register
   * @param aMBeanServerO
   *          server to unregister from. If null, the platform MBeanServer will be used if we are
   *          running under Java 1.5. Earlier versions of Java did not have a platform MBeanServer;
   *          in that case, this method will do nothing.
   */
  public static void unregisterMBean(ManagementObject aMBean, Object aMBeanServerO) {
    MBeanServer aMBeanServer = (MBeanServer)aMBeanServerO;
    if (!jmxAvailable) // means we couldn't find the required classes and methods
    {
      return;
    }

    if (aMBeanServer == null) {
      if (platformMBeanServer != null) {
        aMBeanServer = platformMBeanServer;
      } else {
        UIMAFramework.getLogger().logrb(Level.CONFIG, JmxMBeanAgent.class.getName(),
                "unregisterMBean", LOG_RESOURCE_BUNDLE,
                "UIMA_JMX_platform_mbean_server_not_available__CONFIG");
        return;
      }
    }

    try {
      // Now unregister the MBean.
      String mbeanName = aMBean.getUniqueMBeanName();
      if (mbeanName != null) // guards against uninitialized AE instances
      {
        ObjectName objName = new ObjectName(mbeanName);
        if (aMBeanServer.isRegistered(objName)) {
          aMBeanServer.unregisterMBean(objName);
        }
//        Object objName = objectNameConstructor.newInstance(new Object[] { mbeanName });
//        if (((Boolean) isRegistered.invoke(aMBeanServer, new Object[] { objName })).booleanValue()) {
//          unregisterMBean.invoke(aMBeanServer, new Object[] { objName });
//        }
      }
    } catch (Exception e) {
      // don't fail catastrophically if we can't unregister. Just log a warning and continue.
      UIMAFramework.getLogger().logrb(Level.WARNING, JmxMBeanAgent.class.getName(),
              "unregisterMBean", LOG_RESOURCE_BUNDLE,
              "UIMA_JMX_failed_to_unregister_mbean__WARNING", e);
      return;
    }
  }