Java examples for javax.management:MBean
is MBean Registered
/*//from w w w.j a v a 2 s . co 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; System.out.println(isRegistered(cacheManagerName,name,stats)); } private static MBeanServer mBeanServer = ManagementFactory .getPlatformMBeanServer(); 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(); } /** * 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); } } private static String mbeanSafe(String string) { return string == null ? "" : string.replaceAll(",|:|=|\n", "."); } }