PageRenderTime 23ms CodeModel.GetById 8ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 1ms

/hazelcast/src/main/java/com/hazelcast/jmx/DataMBean.java

https://bitbucket.org/gabral6_gmailcom/hazelcast
Java | 237 lines | 179 code | 24 blank | 34 comment | 34 complexity | bc5bfa0dd9659e4e4ee1a6d57a021d1f MD5 | raw file
  1/*
  2 * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License");
  5 * you may not use this file except in compliance with the License.
  6 * You may obtain a copy of the License at
  7 *
  8 * http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS,
 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13 * See the License for the specific language governing permissions and
 14 * limitations under the License.
 15 */
 16
 17package com.hazelcast.jmx;
 18
 19import com.hazelcast.core.*;
 20
 21import javax.management.MBeanServer;
 22import javax.management.ObjectName;
 23import java.lang.management.ManagementFactory;
 24import java.util.Collection;
 25import java.util.logging.Level;
 26import java.util.logging.Logger;
 27
 28/**
 29 * Manager of data instances and collects general statistics.
 30 *
 31 * @author Marco Ferrante, DISI - University of Genova
 32 */
 33@JMXDescription("Cluster statistics")
 34public class DataMBean extends AbstractMBean<HazelcastInstance> implements InstanceListener {
 35
 36    private final static Logger logger = Logger.getLogger(DataMBean.class.getName());
 37
 38    private StatisticsCollector creationStats = null;
 39    private StatisticsCollector destructionStats = null;
 40
 41    /**
 42     * Return the instrumentation wrapper to a instance.
 43     * See http://java.sun.com/javase/technologies/core/mntr-mgmt/javamanagement/best-practices.jsp
 44     *
 45     * @param instance
 46     * @return dynamic mbean for the hazelcast instance
 47     * @throws Exception
 48     */
 49    private AbstractMBean buildMBean(Instance instance) throws Exception {
 50        if (instance.getInstanceType().isTopic()) {
 51            return new TopicMBean((ITopic) instance, managementService);
 52        }
 53        if (instance.getInstanceType().isQueue()) {
 54            return new QueueMBean((IQueue) instance, managementService);
 55        }
 56        if (instance.getInstanceType().isList()) {
 57            return new ListMBean((IList) instance, managementService);
 58        }
 59        if (instance.getInstanceType().isSet()) {
 60            return new SetMBean((ISet) instance, managementService);
 61        }
 62        if (instance.getInstanceType().isMultiMap()) {
 63            return new MultiMapMBean((MultiMap) instance, managementService);
 64        }
 65        if (instance.getInstanceType().isMap()) {
 66            return new MapMBean((IMap) instance, managementService);
 67        }
 68        if (instance.getInstanceType().isLock()) {
 69            return new LockMBean((ILock) instance, managementService);
 70        }
 71        if (instance.getInstanceType().isAtomicNumber()) {
 72            return new AtomicNumberMBean((AtomicNumber) instance, managementService);
 73        }
 74        if (instance.getInstanceType().isCountDownLatch()) {
 75            return new CountDownLatchMBean((ICountDownLatch) instance, managementService);
 76        }
 77        if (instance.getInstanceType().isSemaphore()) {
 78            return new SemaphoreMBean((ISemaphore) instance, managementService);
 79        }
 80        return null;
 81    }
 82
 83    protected DataMBean(ManagementService managementService) {
 84        super(managementService.getInstance(), managementService);
 85    }
 86
 87    @Override
 88    public ObjectNameSpec getNameSpec() {
 89        return getParentName().getNested("Statistics");
 90    }
 91
 92    public void postRegister(Boolean registrationDone) {
 93        if (registrationDone) {
 94            creationStats = ManagementService.newStatisticsCollector();
 95            destructionStats = ManagementService.newStatisticsCollector();
 96            getManagedObject().addInstanceListener(this);
 97            for (final Instance instance : getManagedObject().getInstances()) {
 98                registerInstance(instance);
 99            }
100        }
101    }
102
103    public void preDeregister() throws Exception {
104        getManagedObject().removeInstanceListener(this);
105        if (creationStats != null) {
106            creationStats.destroy();
107            creationStats = null;
108        }
109        if (destructionStats != null) {
110            destructionStats.destroy();
111            destructionStats = null;
112        }
113    }
114
115    public void postDeregister() {
116        // Required by MBeanRegistration interface
117    }
118
119    public void instanceCreated(InstanceEvent event) {
120        if (logger.isLoggable(Level.FINE)) {
121            logger.log(Level.FINE, "Received created notification {0} {1}",
122                    new String[]{event.getInstance().getInstanceType().toString(), event.getInstance().toString()});
123        }
124        if (creationStats != null) {
125            creationStats.addEvent();
126        }
127        registerInstance(event.getInstance());
128    }
129
130    public void instanceDestroyed(InstanceEvent event) {
131        if (logger.isLoggable(Level.FINE)) {
132            logger.log(Level.FINE, "Received destroyed notification " + event.getInstance().toString());
133        }
134        if (destructionStats != null) {
135            destructionStats.addEvent();
136        }
137        unregisterInstance(event.getInstance());
138    }
139
140    public void registerInstance(Object instance) {
141        try {
142            AbstractMBean mbean = buildMBean((Instance) instance);
143            if (mbean == null) {
144                logger.log(Level.FINE, "Unsupported instance type " + instance.getClass().getName());
145            } else {
146                mbean.setParentName(getParentName());
147                ObjectName name = mbean.getObjectName();
148                logger.log(Level.FINEST, "Register MBean {0}", name);
149                MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
150                //noinspection SynchronizeOnThis
151                synchronized (this) {
152                    if (!mbs.isRegistered(name)) {
153                        mbs.registerMBean(mbean, name);
154                    }
155                }
156            }
157        } catch (Exception e) {
158            logger.log(Level.FINE, "Unable to register MBean", e);
159        }
160    }
161
162    public void unregisterInstance(Object instance) {
163        try {
164            AbstractMBean mbean = buildMBean((Instance) instance);
165            if (mbean == null) {
166                logger.log(Level.FINE, "Unsupported instance type " + instance.getClass().getName());
167            } else {
168                mbean.setParentName(getParentName());
169                ObjectName name = mbean.getObjectName();
170                logger.log(Level.FINEST, "Unregister MBean {0}", name);
171                MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
172                //noinspection SynchronizeOnThis
173                synchronized (this) {
174                    if (mbs.isRegistered(name)) {
175                        mbs.unregisterMBean(name);
176                    }
177                }
178            }
179        } catch (Exception e) {
180            logger.log(Level.FINE, "Unable to unregister MBean", e);
181        }
182    }
183
184    /**
185     * Resets statistics
186     */
187    @JMXOperation("resetStats")
188    public void resetStats() {
189        if (creationStats != null)
190            creationStats.reset();
191        if (destructionStats != null)
192            destructionStats.reset();
193    }
194
195    @JMXAttribute("InstanceCount")
196    @JMXDescription("Total data structures registered")
197    public int getInstanceCount() {
198        Collection<Instance> instances = getManagedObject().getInstances();
199        return instances.size();
200    }
201
202    @JMXAttribute("InstancesCreated")
203    @JMXDescription("Total instances created since startup")
204    public long getInstancesCreated() {
205        return creationStats.getTotal();
206    }
207
208    @JMXAttribute("InstancesCreatedLast")
209    @JMXDescription("Instances created in the last second")
210    public double getInstancesCreatedAvg() {
211        return creationStats.getAverage();
212    }
213
214    @JMXAttribute("InstancesCreatedPeak")
215    @JMXDescription("Max instances created per second")
216    public double getInstancesCreatedMax() {
217        return creationStats.getMax();
218    }
219
220    @JMXAttribute("InstancesDestroyed")
221    @JMXDescription("Total instances destroyed since startup")
222    public long getInstancesDestroyed() {
223        return destructionStats.getTotal();
224    }
225
226    @JMXAttribute("InstancesDestroyedLast")
227    @JMXDescription("Instances destroyed in the last second")
228    public double getInstancesDestroyedAvg() {
229        return destructionStats.getAverage();
230    }
231
232    @JMXAttribute("InstancesDestroyedPeak")
233    @JMXDescription("Max instances destroyed per second")
234    public double getInstancesDestroyedMax() {
235        return destructionStats.getMax();
236    }
237}