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 */
017package org.apache.activemq.broker.jmx;
018
019import javax.management.openmbean.CompositeData;
020import javax.management.openmbean.OpenDataException;
021import javax.management.openmbean.TabularData;
022
023import org.apache.activemq.broker.ConnectionContext;
024import org.apache.activemq.broker.region.DurableTopicSubscription;
025import org.apache.activemq.broker.region.Subscription;
026import org.apache.activemq.command.RemoveSubscriptionInfo;
027
028/**
029 *
030 */
031public class DurableSubscriptionView extends SubscriptionView implements DurableSubscriptionViewMBean {
032
033    protected ManagedRegionBroker broker;
034    protected String subscriptionName;
035    protected DurableTopicSubscription durableSub;
036
037    /**
038     * Constructor
039     *
040     * @param clientId
041     * @param sub
042     */
043    public DurableSubscriptionView(ManagedRegionBroker broker, String clientId, String userName, Subscription sub) {
044        super(clientId, userName, sub);
045        this.broker = broker;
046        this.durableSub=(DurableTopicSubscription) sub;
047        if (sub != null) {
048            this.subscriptionName = sub.getConsumerInfo().getSubscriptionName();
049        }
050    }
051
052    /**
053     * @return name of the durable consumer
054     */
055    public String getSubscriptionName() {
056        return subscriptionName;
057    }
058
059    /**
060     * Browse messages for this durable subscriber
061     *
062     * @return messages
063     * @throws OpenDataException
064     */
065    public CompositeData[] browse() throws OpenDataException {
066        return broker.browse(this);
067    }
068
069    /**
070     * Browse messages for this durable subscriber
071     *
072     * @return messages
073     * @throws OpenDataException
074     */
075    public TabularData browseAsTable() throws OpenDataException {
076        return broker.browseAsTable(this);
077    }
078
079    /**
080     * Destroys the durable subscription so that messages will no longer be
081     * stored for this subscription
082     */
083    public void destroy() throws Exception {
084        RemoveSubscriptionInfo info = new RemoveSubscriptionInfo();
085        info.setClientId(clientId);
086        info.setSubscriptionName(subscriptionName);
087        ConnectionContext context = new ConnectionContext();
088        context.setBroker(broker);
089        context.setClientId(clientId);
090        broker.removeSubscription(context, info);
091    }
092
093    public String toString() {
094        return "ActiveDurableSubscriptionView: " + getClientId() + ":" + getSubscriptionName();
095    }
096
097
098    public int cursorSize() {
099        if (durableSub != null && durableSub.getPending() != null) {
100            return durableSub.getPending().size();
101        }
102        return 0;
103    }
104
105
106    public boolean doesCursorHaveMessagesBuffered() {
107        if (durableSub != null && durableSub.getPending() != null) {
108            return durableSub.getPending().hasMessagesBufferedToDeliver();
109        }
110        return false;
111    }
112
113
114    public boolean doesCursorHaveSpace() {
115        if (durableSub != null && durableSub.getPending() != null) {
116            return durableSub.getPending().hasSpace();
117        }
118        return false;
119    }
120
121    /* (non-Javadoc)
122     * @see org.apache.activemq.broker.jmx.DurableSubscriptionViewMBean#getCursorMemoryUsage()
123     */
124    public long getCursorMemoryUsage() {
125        if (durableSub != null && durableSub.getPending() != null && durableSub.getPending().getSystemUsage()!=null) {
126            return durableSub.getPending().getSystemUsage().getMemoryUsage().getUsage();
127        }
128        return 0;
129    }
130
131
132    public int getCursorPercentUsage() {
133        if (durableSub != null && durableSub.getPending() != null && durableSub.getPending().getSystemUsage()!=null) {
134            return durableSub.getPending().getSystemUsage().getMemoryUsage().getPercentUsage();
135        }
136        return 0;
137    }
138
139    public boolean isCursorFull() {
140        if (durableSub != null && durableSub.getPending() != null) {
141            return durableSub.getPending().isFull();
142        }
143        return false;
144    }
145
146    @Override
147    public boolean isActive() {
148        return durableSub.isActive();
149    }
150
151
152}