001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.activemq.store;
019
020import java.util.concurrent.ConcurrentHashMap;
021import java.util.concurrent.ConcurrentMap;
022
023import org.apache.activemq.management.CountStatisticImpl;
024import org.apache.activemq.management.SizeStatisticImpl;
025
026public class MessageStoreSubscriptionStatistics extends AbstractMessageStoreStatistics {
027
028    private final ConcurrentMap<String, SubscriptionStatistics> subStatistics
029        = new ConcurrentHashMap<>();
030
031    /**
032     * @param enabled
033     */
034    public MessageStoreSubscriptionStatistics(boolean enabled) {
035        super(enabled, "The number of messages or this subscription in the message store",
036                "Size of messages contained by this subscription in the message store");
037    }
038
039    /**
040     * Total count for all subscriptions
041     */
042    @Override
043    public CountStatisticImpl getMessageCount() {
044        return this.messageCount;
045    }
046
047    /**
048     * Total size for all subscriptions
049     */
050    @Override
051    public SizeStatisticImpl getMessageSize() {
052        return this.messageSize;
053    }
054
055    public CountStatisticImpl getMessageCount(String subKey) {
056        return getOrInitStatistics(subKey).getMessageCount();
057    }
058
059    public SizeStatisticImpl getMessageSize(String subKey) {
060        return getOrInitStatistics(subKey).getMessageSize();
061    }
062
063    public void removeSubscription(String subKey) {
064        SubscriptionStatistics subStats = subStatistics.remove(subKey);
065        //Subtract from the parent
066        if (subStats != null) {
067           getMessageCount().subtract(subStats.getMessageCount().getCount());
068           getMessageSize().addSize(-subStats.getMessageSize().getTotalSize());
069        }
070    }
071
072    @Override
073    public void reset() {
074        super.reset();
075        subStatistics.clear();
076    }
077
078    private SubscriptionStatistics getOrInitStatistics(String subKey) {
079        SubscriptionStatistics subStats = subStatistics.get(subKey);
080
081        if (subStats == null) {
082            final SubscriptionStatistics stats = new SubscriptionStatistics();
083            subStats = subStatistics.putIfAbsent(subKey, stats);
084            if (subStats == null) {
085                subStats = stats;
086            }
087        }
088
089        return subStats;
090    }
091
092    private class SubscriptionStatistics extends AbstractMessageStoreStatistics {
093
094        public SubscriptionStatistics() {
095            this(MessageStoreSubscriptionStatistics.this.enabled);
096        }
097
098        /**
099         * @param enabled
100         */
101        public SubscriptionStatistics(boolean enabled) {
102            super(enabled, "The number of messages or this subscription in the message store",
103                    "Size of messages contained by this subscription in the message store");
104            this.setParent(MessageStoreSubscriptionStatistics.this);
105        }
106    }
107
108}