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 @Override 109 public void removeMessage(@MBeanInfo("messageId") String messageId) throws Exception { 110 throw new IllegalStateException("Subscription must be inactive"); 111 } 112 113 public boolean doesCursorHaveMessagesBuffered() { 114 if (durableSub != null && durableSub.getPending() != null) { 115 return durableSub.getPending().hasMessagesBufferedToDeliver(); 116 } 117 return false; 118 } 119 120 121 public boolean doesCursorHaveSpace() { 122 if (durableSub != null && durableSub.getPending() != null) { 123 return durableSub.getPending().hasSpace(); 124 } 125 return false; 126 } 127 128 /* (non-Javadoc) 129 * @see org.apache.activemq.broker.jmx.DurableSubscriptionViewMBean#getCursorMemoryUsage() 130 */ 131 public long getCursorMemoryUsage() { 132 if (durableSub != null && durableSub.getPending() != null && durableSub.getPending().getSystemUsage()!=null) { 133 return durableSub.getPending().getSystemUsage().getMemoryUsage().getUsage(); 134 } 135 return 0; 136 } 137 138 139 public int getCursorPercentUsage() { 140 if (durableSub != null && durableSub.getPending() != null && durableSub.getPending().getSystemUsage()!=null) { 141 return durableSub.getPending().getSystemUsage().getMemoryUsage().getPercentUsage(); 142 } 143 return 0; 144 } 145 146 public boolean isCursorFull() { 147 if (durableSub != null && durableSub.getPending() != null) { 148 return durableSub.getPending().isFull(); 149 } 150 return false; 151 } 152 153 @Override 154 public boolean isActive() { 155 return durableSub.isActive(); 156 } 157 158 159}