1 18 package org.apache.activemq.broker.jmx; 19 20 import java.util.concurrent.atomic.AtomicInteger ; 21 22 import javax.management.ObjectName ; 23 24 import org.apache.activemq.broker.Broker; 25 import org.apache.activemq.broker.BrokerService; 26 import org.apache.activemq.broker.ConnectionContext; 27 import org.apache.activemq.broker.region.Subscription; 28 import org.apache.activemq.command.ActiveMQQueue; 29 import org.apache.activemq.command.ActiveMQTopic; 30 import org.apache.activemq.command.ConsumerId; 31 import org.apache.activemq.command.ConsumerInfo; 32 import org.apache.activemq.command.RemoveSubscriptionInfo; 33 34 public class BrokerView implements BrokerViewMBean { 35 36 final ManagedRegionBroker broker; 37 private final BrokerService brokerService; 38 private final AtomicInteger sessionIdCounter = new AtomicInteger (0); 39 40 public BrokerView(BrokerService brokerService, ManagedRegionBroker managedBroker) throws Exception { 41 this.brokerService = brokerService; 42 this.broker = managedBroker; 43 } 44 45 public ManagedRegionBroker getBroker() { 46 return broker; 47 } 48 49 public String getBrokerId() { 50 return broker.getBrokerId().toString(); 51 } 52 53 public void gc() throws Exception { 54 brokerService.getBroker().gc(); 55 } 56 57 public void start() throws Exception { 58 brokerService.start(); 59 } 60 61 public void stop() throws Exception { 62 brokerService.stop(); 63 } 64 65 public long getTotalEnqueueCount() { 66 return broker.getDestinationStatistics().getEnqueues().getCount(); 67 } 68 public long getTotalDequeueCount() { 69 return broker.getDestinationStatistics().getDequeues().getCount(); 70 } 71 public long getTotalConsumerCount() { 72 return broker.getDestinationStatistics().getConsumers().getCount(); 73 } 74 public long getTotalMessageCount() { 75 return broker.getDestinationStatistics().getMessages().getCount(); 76 } 77 public long getTotalMessagesCached() { 78 return broker.getDestinationStatistics().getMessagesCached().getCount(); 79 } 80 81 public int getMemoryPercentageUsed() { 82 return brokerService.getMemoryManager().getPercentUsage(); 83 } 84 public long getMemoryLimit() { 85 return brokerService.getMemoryManager().getLimit(); 86 } 87 public void setMemoryLimit(long limit) { 88 brokerService.getMemoryManager().setLimit(limit); 89 } 90 91 public void resetStatistics() { 92 broker.getDestinationStatistics().reset(); 93 } 94 95 public void enableStatistics() { 96 broker.getDestinationStatistics().setEnabled(true); 97 } 98 99 public void disableStatistics() { 100 broker.getDestinationStatistics().setEnabled(false); 101 } 102 103 public boolean isStatisticsEnabled() { 104 return broker.getDestinationStatistics().isEnabled(); 105 } 106 107 108 public void terminateJVM(int exitCode) { 109 System.exit(exitCode); 110 } 111 112 public ObjectName [] getTopics(){ 113 return broker.getTopics(); 114 } 115 116 public ObjectName [] getQueues(){ 117 return broker.getQueues(); 118 } 119 120 public ObjectName [] getTemporaryTopics(){ 121 return broker.getTemporaryTopics(); 122 } 123 124 public ObjectName [] getTemporaryQueues(){ 125 return broker.getTemporaryQueues(); 126 } 127 128 public ObjectName [] getTopicSubscribers(){ 129 return broker.getTemporaryTopicSubscribers(); 130 } 131 132 public ObjectName [] getDurableTopicSubscribers(){ 133 return broker.getDurableTopicSubscribers(); 134 } 135 136 public ObjectName [] getQueueSubscribers(){ 137 return broker.getQueueSubscribers(); 138 } 139 140 public ObjectName [] getTemporaryTopicSubscribers(){ 141 return broker.getTemporaryTopicSubscribers(); 142 } 143 144 public ObjectName [] getTemporaryQueueSubscribers(){ 145 return broker.getTemporaryQueueSubscribers(); 146 } 147 148 public ObjectName [] getInactiveDurableTopicSubscribers(){ 149 return broker.getInactiveDurableTopicSubscribers(); 150 } 151 152 public void addTopic(String name) throws Exception { 153 broker.addDestination(getConnectionContext(broker.getContextBroker()), new ActiveMQTopic(name)); 154 } 155 156 public void addQueue(String name) throws Exception { 157 broker.addDestination(getConnectionContext(broker.getContextBroker()), new ActiveMQQueue(name)); 158 } 159 160 public void removeTopic(String name) throws Exception { 161 broker.removeDestination(getConnectionContext(broker.getContextBroker()), new ActiveMQTopic(name), 1000); 162 } 163 164 public void removeQueue(String name) throws Exception { 165 broker.removeDestination(getConnectionContext(broker.getContextBroker()), new ActiveMQQueue(name), 1000); 166 } 167 168 public ObjectName createDurableSubscriber(String clientId, String subscriberName, String topicName, String selector) throws Exception { 169 ConnectionContext context = new ConnectionContext(); 170 context.setBroker(broker); 171 context.setClientId(clientId); 172 ConsumerInfo info = new ConsumerInfo(); 173 ConsumerId consumerId = new ConsumerId(); 174 consumerId.setConnectionId(clientId); 175 consumerId.setSessionId(sessionIdCounter.incrementAndGet()); 176 consumerId.setValue(0); 177 info.setConsumerId(consumerId); 178 info.setDestination(new ActiveMQTopic(topicName)); 179 info.setSubscriptionName(subscriberName); 180 info.setSelector(selector); 181 Subscription subscription = broker.addConsumer(context, info); 182 broker.removeConsumer(context, info); 183 if (subscription != null) { 184 return subscription.getObjectName(); 185 } 186 return null; 187 } 188 189 public void destroyDurableSubscriber(String clientId, String subscriberName) throws Exception { 190 RemoveSubscriptionInfo info = new RemoveSubscriptionInfo(); 191 info.setClientId(clientId); 192 info.setSubcriptionName(subscriberName); 193 ConnectionContext context = new ConnectionContext(); 194 context.setBroker(broker); 195 context.setClientId(clientId); 196 broker.removeSubscription(context, info); 197 } 198 199 200 204 public static ConnectionContext getConnectionContext(Broker broker) { 205 ConnectionContext adminConnectionContext = broker.getAdminConnectionContext(); 206 if (adminConnectionContext == null) { 207 adminConnectionContext = createAdminConnectionContext(broker); 208 broker.setAdminConnectionContext(adminConnectionContext); 209 } 210 return adminConnectionContext; 211 } 212 213 218 protected static ConnectionContext createAdminConnectionContext(Broker broker) { 219 ConnectionContext context = new ConnectionContext(); 220 context.setBroker(broker); 221 return context; 222 } 223 224 } 225 | Popular Tags |