Registers the provided javax.cache.management.CacheMXBean or javax.cache.management.CacheStatisticsMXBean implementations if not registered yet. - Java javax.management

Java examples for javax.management:MBean

Description

Registers the provided javax.cache.management.CacheMXBean or javax.cache.management.CacheStatisticsMXBean implementations if not registered yet.

Demo Code

/*/*w  w  w .  ja  va 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{
        Object mxbean = "java2s.com";
        String cacheManagerName = "java2s.com";
        String name = "java2s.com";
        boolean stats = true;
        registerCacheObject(mxbean,cacheManagerName,name,stats);
    }
    private static MBeanServer mBeanServer = ManagementFactory
            .getPlatformMBeanServer();
    /**
     * Registers the provided {@link javax.cache.management.CacheMXBean} or
     * {@link javax.cache.management.CacheStatisticsMXBean} implementations if not registered yet.
     * @param mxbean {@link javax.cache.management.CacheMXBean} or {@link javax.cache.management.CacheStatisticsMXBean}.
     * @param cacheManagerName name generated by URI and classloader.
     * @param name cache name.
     * @param stats is mxbean parameter, a statistics mxbean.
     */
    public static void registerCacheObject(Object mxbean,
            String cacheManagerName, String name, boolean stats) {
        //these can change during runtime, so always look it up
        ObjectName registeredObjectName = calculateObjectName(
                cacheManagerName, name, stats);
        try {
            if (!isRegistered(cacheManagerName, name, stats)) {
                mBeanServer.registerMBean(mxbean, registeredObjectName);
            }
        } catch (Exception e) {
            throw new CacheException(
                    "Error registering cache MXBeans for CacheManager "
                            + registeredObjectName + " . Error was "
                            + e.getMessage(), e);
        }
    }
    /**
     * Creates an object name using the scheme.
     * "javax.cache:type=Cache<Statistics|Configuration>,name=<cacheName>"
     */
    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);
        }
    }
    static boolean isRegistered(String cacheManagerName, String name,
            boolean stats) {
        Set<ObjectName> registeredObjectNames = null;

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

        return !registeredObjectNames.isEmpty();
    }
    private static String mbeanSafe(String string) {
        return string == null ? "" : string.replaceAll(",|:|=|\n", ".");
    }
}

Related Tutorials