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

https://bitbucket.org/gabral6_gmailcom/hazelcast · Java · 156 lines · 109 code · 21 blank · 26 comment · 14 complexity · 7886189a56b691c4bcb87bbc9357a62e 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.ISet;
  18. import com.hazelcast.core.ItemEvent;
  19. import com.hazelcast.core.ItemListener;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. /**
  23. * MBean for Set
  24. *
  25. * @author Marco Ferrante, DISI - University of Genoa
  26. */
  27. @JMXDescription("A distributed set")
  28. public class SetMBean extends AbstractMBean<ISet<?>> {
  29. @SuppressWarnings("unchecked")
  30. protected ItemListener listener;
  31. private StatisticsCollector receivedStats = null;
  32. private StatisticsCollector servedStats = null;
  33. public SetMBean(ISet<?> managedObject, ManagementService managementService) {
  34. super(managedObject, managementService);
  35. }
  36. @Override
  37. public ObjectNameSpec getNameSpec() {
  38. return getParentName().getNested("Set", getName());
  39. }
  40. @SuppressWarnings("unchecked")
  41. @Override
  42. public void postRegister(Boolean registrationDone) {
  43. super.postRegister(registrationDone);
  44. if (!registrationDone) {
  45. return;
  46. }
  47. if (managementService.showDetails()) {
  48. receivedStats = ManagementService.newStatisticsCollector();
  49. servedStats = ManagementService.newStatisticsCollector();
  50. listener = new ItemListener() {
  51. public void itemAdded(ItemEvent itemEvent) {
  52. receivedStats.addEvent();
  53. addItem(itemEvent.getItem());
  54. }
  55. public void itemRemoved(ItemEvent itemEvent) {
  56. servedStats.addEvent();
  57. removeItem(itemEvent.getItem());
  58. }
  59. };
  60. getManagedObject().addItemListener(listener, false);
  61. // Add existing entries
  62. for (Object item : getManagedObject()) {
  63. addItem(item);
  64. }
  65. }
  66. }
  67. @SuppressWarnings("unchecked")
  68. @Override
  69. public void preDeregister() throws Exception {
  70. if (listener != null) {
  71. getManagedObject().removeItemListener(listener);
  72. listener = null;
  73. }
  74. if (receivedStats != null) {
  75. receivedStats.destroy();
  76. receivedStats = null;
  77. }
  78. if (servedStats != null) {
  79. servedStats.destroy();
  80. servedStats = null;
  81. }
  82. super.preDeregister();
  83. }
  84. protected void addItem(Object item) {
  85. // Manage items?
  86. }
  87. protected void removeItem(Object item) {
  88. // Manage items?
  89. }
  90. /**
  91. * Resets statistics
  92. */
  93. @JMXOperation("resetStats")
  94. public void resetStats() {
  95. if (receivedStats != null)
  96. receivedStats.reset();
  97. if (servedStats != null)
  98. servedStats.reset();
  99. }
  100. @JMXOperation("clear")
  101. @JMXDescription("Clear set")
  102. public void clear() {
  103. getManagedObject().clear();
  104. }
  105. @JMXAttribute("Name")
  106. @JMXDescription("Registration name of the list")
  107. public String getName() {
  108. return getManagedObject().getName();
  109. }
  110. @JMXAttribute("Size")
  111. @JMXDescription("Current size")
  112. public int getSize() {
  113. return getManagedObject().size();
  114. }
  115. @SuppressWarnings("unchecked")
  116. @JMXAttribute("Items")
  117. @JMXDescription("Current items")
  118. public List<?> getItems() {
  119. ArrayList result = new ArrayList();
  120. for (Object item : getManagedObject()) {
  121. result.add(item);
  122. }
  123. return result;
  124. }
  125. @JMXAttribute("ObjectAdded")
  126. @JMXDescription("Object added to the util since the start time")
  127. public long getItemsReceived() {
  128. return receivedStats.getTotal();
  129. }
  130. @JMXAttribute("ObjectRemoved")
  131. @JMXDescription("Object removed from the util since the start time")
  132. public long getItemsServed() {
  133. return servedStats.getTotal();
  134. }
  135. }