KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > broker > jmx > BrokerView


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.broker.jmx;
19
20 import java.util.concurrent.atomic.AtomicInteger JavaDoc;
21
22 import javax.management.ObjectName JavaDoc;
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 JavaDoc sessionIdCounter = new AtomicInteger JavaDoc(0);
39
40     public BrokerView(BrokerService brokerService, ManagedRegionBroker managedBroker) throws Exception JavaDoc {
41         this.brokerService = brokerService;
42         this.broker = managedBroker;
43     }
44     
45     public ManagedRegionBroker getBroker() {
46         return broker;
47     }
48
49     public String JavaDoc getBrokerId() {
50         return broker.getBrokerId().toString();
51     }
52     
53     public void gc() throws Exception JavaDoc {
54         brokerService.getBroker().gc();
55     }
56
57     public void start() throws Exception JavaDoc {
58         brokerService.start();
59     }
60     
61     public void stop() throws Exception JavaDoc {
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 JavaDoc[] getTopics(){
113         return broker.getTopics();
114     }
115
116     public ObjectName JavaDoc[] getQueues(){
117         return broker.getQueues();
118     }
119
120     public ObjectName JavaDoc[] getTemporaryTopics(){
121         return broker.getTemporaryTopics();
122     }
123
124     public ObjectName JavaDoc[] getTemporaryQueues(){
125         return broker.getTemporaryQueues();
126     }
127
128     public ObjectName JavaDoc[] getTopicSubscribers(){
129       return broker.getTemporaryTopicSubscribers();
130     }
131
132     public ObjectName JavaDoc[] getDurableTopicSubscribers(){
133         return broker.getDurableTopicSubscribers();
134     }
135
136     public ObjectName JavaDoc[] getQueueSubscribers(){
137        return broker.getQueueSubscribers();
138     }
139
140     public ObjectName JavaDoc[] getTemporaryTopicSubscribers(){
141         return broker.getTemporaryTopicSubscribers();
142     }
143
144     public ObjectName JavaDoc[] getTemporaryQueueSubscribers(){
145         return broker.getTemporaryQueueSubscribers();
146     }
147     
148     public ObjectName JavaDoc[] getInactiveDurableTopicSubscribers(){
149         return broker.getInactiveDurableTopicSubscribers();
150     }
151
152     public void addTopic(String JavaDoc name) throws Exception JavaDoc {
153         broker.addDestination(getConnectionContext(broker.getContextBroker()), new ActiveMQTopic(name));
154     }
155
156     public void addQueue(String JavaDoc name) throws Exception JavaDoc {
157         broker.addDestination(getConnectionContext(broker.getContextBroker()), new ActiveMQQueue(name));
158     }
159
160     public void removeTopic(String JavaDoc name) throws Exception JavaDoc {
161         broker.removeDestination(getConnectionContext(broker.getContextBroker()), new ActiveMQTopic(name), 1000);
162     }
163
164     public void removeQueue(String JavaDoc name) throws Exception JavaDoc {
165         broker.removeDestination(getConnectionContext(broker.getContextBroker()), new ActiveMQQueue(name), 1000);
166     }
167     
168     public ObjectName JavaDoc createDurableSubscriber(String JavaDoc clientId, String JavaDoc subscriberName, String JavaDoc topicName, String JavaDoc selector) throws Exception JavaDoc {
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 JavaDoc clientId, String JavaDoc subscriberName) throws Exception JavaDoc {
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     /**
201      * Returns the broker's administration connection context used for configuring the broker
202      * at startup
203      */

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     /**
214      * Factory method to create the new administration connection context object.
215      * Note this method is here rather than inside a default broker implementation to
216      * ensure that the broker reference inside it is the outer most interceptor
217      */

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