Java examples for javax.management:MBean
Register the MBean.
/**/*from w ww . java 2s. c o m*/ * Copyright 2009, 2010 lazydog.org. * * This file is part of JPrefer. * * JPrefer is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * JPrefer is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with JPrefer. If not, see <http://www.gnu.org/licenses/>. */ import java.lang.management.ManagementFactory; import javax.management.InstanceAlreadyExistsException; import javax.management.InstanceNotFoundException; import javax.management.MalformedObjectNameException; import javax.management.MBeanRegistrationException; import javax.management.MBeanServer; import javax.management.NotCompliantMBeanException; import javax.management.ObjectName; public class Main{ public static void main(String[] argv) throws Exception{ String name = "java2s.com"; Object object = "java2s.com"; register(name,object); } /** * Register the MBean. * * @param name the MBean name. * @param object the MBean object. * * @throws MBeanUtilityException if unable to register the MBean. */ public static void register(String name, Object object) throws MBeanUtilityException { try { // Declare. MBeanServer mBeanServer; ObjectName objectName; // Get the MBean object name. objectName = new ObjectName(name); // Get the MBean server. mBeanServer = ManagementFactory.getPlatformMBeanServer(); // Check if the MBean is not registered with the MBean server. if (!mBeanServer.isRegistered(objectName)) { // Register the MBean with the MBean server. mBeanServer.registerMBean(object, objectName); } } catch (InstanceAlreadyExistsException e) { throw new MBeanUtilityException( "Unable to register the MBean, " + name + ".", e); } catch (MalformedObjectNameException e) { throw new MBeanUtilityException( "Unable to register the MBean, " + name + ".", e); } catch (MBeanRegistrationException e) { throw new MBeanUtilityException( "Unable to register the MBean, " + name + ".", e); } catch (NotCompliantMBeanException e) { throw new MBeanUtilityException( "Unable to register the MBean, " + name + ".", e); } } }