UnRegisters the MBean if registered already. - Java javax.management

Java examples for javax.management:MBean

Description

UnRegisters the MBean if registered already.

Demo Code

/*//w  w  w.  java  2 s  . c o  m
 * Copyright (c) 2008-2015, Hazelcast, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import javax.cache.CacheException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import java.lang.management.ManagementFactory;
import java.util.Set;

public class Main{
    public static void main(String[] argv) throws Exception{
        String cacheManagerName = "java2s.com";
        String name = "java2s.com";
        boolean stats = true;
        unregisterCacheObject(cacheManagerName,name,stats);
    }
    private static MBeanServer mBeanServer = ManagementFactory
            .getPlatformMBeanServer();
    /**
     * UnRegisters the mxbean if registered already.
     * @param cacheManagerName name generated by URI and classloader.
     * @param name cache name.
     * @param stats is mxbean, a statistics mxbean.
     */
    public static void unregisterCacheObject(String cacheManagerName,
            String name, boolean stats) {
        Set<ObjectName> registeredObjectNames = null;

        ObjectName objectName = calculateObjectName(cacheManagerName, name,
                stats);
        registeredObjectNames = mBeanServer.queryNames(objectName, null);

        //should just be one
        for (ObjectName registeredObjectName : registeredObjectNames) {
            try {
                mBeanServer.unregisterMBean(registeredObjectName);
            } catch (Exception e) {
                throw new CacheException(
                        "Error unregistering object instance "
                                + registeredObjectName + " . Error was "
                                + e.getMessage(), e);
            }
        }
    }
    /**
     * Creates an object name using the scheme.
     * "javax.cache:type=Cache&lt;Statistics|Configuration&gt;,name=&lt;cacheName&gt;"
     */
    public static ObjectName calculateObjectName(String cacheManagerName,
            String name, boolean stats) {
        String cacheManagerNameSafe = mbeanSafe(cacheManagerName);
        String cacheName = mbeanSafe(name);

        try {
            String objectNameType = stats ? "Statistics" : "Configuration";
            return new ObjectName("javax.cache:type=Cache" + objectNameType
                    + ",CacheManager=" + cacheManagerNameSafe + ",Cache="
                    + cacheName);
        } catch (MalformedObjectNameException e) {
            throw new CacheException(
                    "Illegal ObjectName for Management Bean. "
                            + "CacheManager=[" + cacheManagerNameSafe
                            + "], Cache=[" + cacheName + "]", e);
        }
    }
    private static String mbeanSafe(String string) {
        return string == null ? "" : string.replaceAll(",|:|=|\n", ".");
    }
}

Related Tutorials