/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. package com.hazelcast.jmx;
  17. import com.hazelcast.core.*;
  18. import javax.management.MBeanServer;
  19. import javax.management.ObjectName;
  20. import java.lang.management.ManagementFactory;
  21. import java.util.Collection;
  22. import java.util.logging.Level;
  23. import java.util.logging.Logger;
  24. /**
  25. * Manager of data instances and collects general statistics.
  26. *
  27. * @author Marco Ferrante, DISI - University of Genova
  28. */
  29. @JMXDescription("Cluster statistics")
  30. public class DataMBean extends AbstractMBean<HazelcastInstance> implements InstanceListener {
  31. private final static Logger logger = Logger.getLogger(DataMBean.class.getName());
  32. private StatisticsCollector creationStats = null;
  33. private StatisticsCollector destructionStats = null;
  34. /**
  35. * Return the instrumentation wrapper to a instance.
  36. * See http://java.sun.com/javase/technologies/core/mntr-mgmt/javamanagement/best-practices.jsp
  37. *
  38. * @param instance
  39. * @return dynamic mbean for the hazelcast instance
  40. * @throws Exception
  41. */
  42. private AbstractMBean buildMBean(Instance instance) throws Exception {
  43. if (instance.getInstanceType().isTopic()) {
  44. return new TopicMBean((ITopic) instance, managementService);
  45. }
  46. if (instance.getInstanceType().isQueue()) {
  47. return new QueueMBean((IQueue) instance, managementService);
  48. }
  49. if (instance.getInstanceType().isList()) {
  50. return new ListMBean((IList) instance, managementService);
  51. }
  52. if (instance.getInstanceType().isSet()) {
  53. return new SetMBean((ISet) instance, managementService);
  54. }
  55. if (instance.getInstanceType().isMultiMap()) {
  56. return new MultiMapMBean((MultiMap) instance, managementService);
  57. }
  58. if (instance.getInstanceType().isMap()) {
  59. return new MapMBean((IMap) instance, managementService);
  60. }
  61. if (instance.getInstanceType().isLock()) {
  62. return new LockMBean((ILock) instance, managementService);
  63. }
  64. if (instance.getInstanceType().isAtomicNumber()) {
  65. return new AtomicNumberMBean((AtomicNumber) instance, managementService);
  66. }
  67. if (instance.getInstanceType().isCountDownLatch()) {
  68. return new CountDownLatchMBean((ICountDownLatch) instance, managementService);
  69. }
  70. if (instance.getInstanceType().isSemaphore()) {
  71. return new SemaphoreMBean((ISemaphore) instance, managementService);
  72. }
  73. return null;
  74. }
  75. protected DataMBean(ManagementService managementService) {
  76. super(managementService.getInstance(), managementService);
  77. }
  78. @Override
  79. public ObjectNameSpec getNameSpec() {
  80. return getParentName().getNested("Statistics");
  81. }
  82. public void postRegister(Boolean registrationDone) {
  83. if (registrationDone) {
  84. creationStats = ManagementService.newStatisticsCollector();
  85. destructionStats = ManagementService.newStatisticsCollector();
  86. getManagedObject().addInstanceListener(this);
  87. for (final Instance instance : getManagedObject().getInstances()) {
  88. registerInstance(instance);
  89. }
  90. }
  91. }
  92. public void preDeregister() throws Exception {
  93. getManagedObject().removeInstanceListener(this);
  94. if (creationStats != null) {
  95. creationStats.destroy();
  96. creationStats = null;
  97. }
  98. if (destructionStats != null) {
  99. destructionStats.destroy();
  100. destructionStats = null;
  101. }
  102. }
  103. public void postDeregister() {
  104. // Required by MBeanRegistration interface
  105. }
  106. public void instanceCreated(InstanceEvent event) {
  107. if (logger.isLoggable(Level.FINE)) {
  108. logger.log(Level.FINE, "Received created notification {0} {1}",
  109. new String[]{event.getInstance().getInstanceType().toString(), event.getInstance().toString()});
  110. }
  111. if (creationStats != null) {
  112. creationStats.addEvent();
  113. }
  114. registerInstance(event.getInstance());
  115. }
  116. public void instanceDestroyed(InstanceEvent event) {
  117. if (logger.isLoggable(Level.FINE)) {
  118. logger.log(Level.FINE, "Received destroyed notification " + event.getInstance().toString());
  119. }
  120. if (destructionStats != null) {
  121. destructionStats.addEvent();
  122. }
  123. unregisterInstance(event.getInstance());
  124. }
  125. public void registerInstance(Object instance) {
  126. try {
  127. AbstractMBean mbean = buildMBean((Instance) instance);
  128. if (mbean == null) {
  129. logger.log(Level.FINE, "Unsupported instance type " + instance.getClass().getName());
  130. } else {
  131. mbean.setParentName(getParentName());
  132. ObjectName name = mbean.getObjectName();
  133. logger.log(Level.FINEST, "Register MBean {0}", name);
  134. MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
  135. //noinspection SynchronizeOnThis
  136. synchronized (this) {
  137. if (!mbs.isRegistered(name)) {
  138. mbs.registerMBean(mbean, name);
  139. }
  140. }
  141. }
  142. } catch (Exception e) {
  143. logger.log(Level.FINE, "Unable to register MBean", e);
  144. }
  145. }
  146. public void unregisterInstance(Object instance) {
  147. try {
  148. AbstractMBean mbean = buildMBean((Instance) instance);
  149. if (mbean == null) {
  150. logger.log(Level.FINE, "Unsupported instance type " + instance.getClass().getName());
  151. } else {
  152. mbean.setParentName(getParentName());
  153. ObjectName name = mbean.getObjectName();
  154. logger.log(Level.FINEST, "Unregister MBean {0}", name);
  155. MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
  156. //noinspection SynchronizeOnThis
  157. synchronized (this) {
  158. if (mbs.isRegistered(name)) {
  159. mbs.unregisterMBean(name);
  160. }
  161. }
  162. }
  163. } catch (Exception e) {
  164. logger.log(Level.FINE, "Unable to unregister MBean", e);
  165. }
  166. }
  167. /**
  168. * Resets statistics
  169. */
  170. @JMXOperation("resetStats")
  171. public void resetStats() {
  172. if (creationStats != null)
  173. creationStats.reset();
  174. if (destructionStats != null)
  175. destructionStats.reset();
  176. }
  177. @JMXAttribute("InstanceCount")
  178. @JMXDescription("Total data structures registered")
  179. public int getInstanceCount() {
  180. Collection<Instance> instances = getManagedObject().getInstances();
  181. return instances.size();
  182. }
  183. @JMXAttribute("InstancesCreated")
  184. @JMXDescription("Total instances created since startup")
  185. public long getInstancesCreated() {
  186. return creationStats.getTotal();
  187. }
  188. @JMXAttribute("InstancesCreatedLast")
  189. @JMXDescription("Instances created in the last second")
  190. public double getInstancesCreatedAvg() {
  191. return creationStats.getAverage();
  192. }
  193. @JMXAttribute("InstancesCreatedPeak")
  194. @JMXDescription("Max instances created per second")
  195. public double getInstancesCreatedMax() {
  196. return creationStats.getMax();
  197. }
  198. @JMXAttribute("InstancesDestroyed")
  199. @JMXDescription("Total instances destroyed since startup")
  200. public long getInstancesDestroyed() {
  201. return destructionStats.getTotal();
  202. }
  203. @JMXAttribute("InstancesDestroyedLast")
  204. @JMXDescription("Instances destroyed in the last second")
  205. public double getInstancesDestroyedAvg() {
  206. return destructionStats.getAverage();
  207. }
  208. @JMXAttribute("InstancesDestroyedPeak")
  209. @JMXDescription("Max instances destroyed per second")
  210. public double getInstancesDestroyedMax() {
  211. return destructionStats.getMax();
  212. }
  213. }