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