PageRenderTime 29ms CodeModel.GetById 21ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

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