2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard;
10 import com.google.common.cache.Cache;
11 import com.google.common.cache.CacheBuilder;
12 import java.util.concurrent.Callable;
13 import java.util.concurrent.ExecutionException;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
18 * @author Basheeruddin syedbahm@cisco.com
21 public class ShardMBeanFactory {
23 private static final Logger LOG = LoggerFactory.getLogger(ShardMBeanFactory.class);
25 private static final Cache<String,ShardStats> shardMBeansCache =
26 CacheBuilder.newBuilder().weakValues().build();
28 public static ShardStats getShardStatsMBean(final String shardName, final String mxBeanType) {
29 final String finalMXBeanType = mxBeanType != null ? mxBeanType : "DistDataStore";
31 return shardMBeansCache.get(shardName, new Callable<ShardStats>() {
33 public ShardStats call() throws Exception {
34 ShardStats shardStatsMBeanImpl = new ShardStats(shardName, finalMXBeanType);
35 shardStatsMBeanImpl.registerMBean();
36 return shardStatsMBeanImpl;
39 } catch(ExecutionException e) {
40 LOG.error(String.format("Could not create MXBean for shard: %s", shardName), e);
41 // Just return an instance that isn't registered.
42 return new ShardStats(shardName, finalMXBeanType);