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.view;
018
019import java.util.Collections;
020import java.util.HashSet;
021import java.util.Map;
022import java.util.Set;
023
024import org.apache.activemq.broker.BrokerRegistry;
025import org.apache.activemq.broker.BrokerService;
026import org.apache.activemq.broker.region.Destination;
027import org.apache.activemq.command.ActiveMQDestination;
028import org.apache.activemq.command.ActiveMQQueue;
029import org.apache.activemq.command.ActiveMQTempQueue;
030import org.apache.activemq.command.ActiveMQTempTopic;
031import org.apache.activemq.command.ActiveMQTopic;
032import org.apache.activemq.util.LRUCache;
033
034/**
035 * A view into the running Broker
036 */
037public class MessageBrokerView  {
038    private final BrokerService brokerService;
039    private final Map<ActiveMQDestination,BrokerDestinationView> destinationViewMap = new LRUCache<ActiveMQDestination, BrokerDestinationView>();
040
041
042    /**
043     * Create a view of a running Broker
044     * @param brokerService
045     */
046    public MessageBrokerView(BrokerService brokerService){
047        this.brokerService = brokerService;
048        if (brokerService == null){
049            throw new NullPointerException("BrokerService is null");
050        }
051        if (!brokerService.isStarted()){
052            throw new IllegalStateException("BrokerService " + brokerService.getBrokerName() + " is not started");
053        }
054    }
055
056    /**
057     * Create a view of a running Broker
058     * @param brokerName
059     */
060    public MessageBrokerView(String brokerName){
061        this.brokerService = BrokerRegistry.getInstance().lookup(brokerName);
062        if (brokerService == null){
063            throw new NullPointerException("BrokerService is null");
064        }
065        if (!brokerService.isStarted()){
066            throw new IllegalStateException("BrokerService " + brokerService.getBrokerName() + " is not started");
067        }
068    }
069
070
071
072    /**
073     * @return the brokerName
074     */
075    public String getBrokerName(){
076        return brokerService.getBrokerName();
077    }
078
079    /**
080     * @return the unique id of the Broker
081     */
082    public String getBrokerId(){
083        try {
084            return brokerService.getBroker().getBrokerId().toString();
085        } catch (Exception e) {
086            return "";
087        }
088    }
089
090
091    /**
092     * @return the memory used by the Broker as a percentage
093     */
094    public int getMemoryPercentUsage() {
095        return brokerService.getSystemUsage().getMemoryUsage().getPercentUsage();
096    }
097
098
099    /**
100     * @return  the space used by the Message Store as a percentage
101     */
102
103    public int getStorePercentUsage() {
104        return brokerService.getSystemUsage().getStoreUsage().getPercentUsage();
105    }
106
107    /**
108     * @return the space used by the store for temporary messages as a percentage
109     */
110    public int getTempPercentUsage() {
111        return brokerService.getSystemUsage().getTempUsage().getPercentUsage();
112    }
113
114    /**
115     * @return the space used by the store of scheduled messages
116     */
117    public int getJobSchedulerStorePercentUsage() {
118        return brokerService.getSystemUsage().getJobSchedulerUsage().getPercentUsage();
119    }
120
121    /**
122     * @return true if the Broker isn't using an in-memory store only for messages
123     */
124    public boolean isPersistent() {
125        return brokerService.isPersistent();
126    }
127
128    public BrokerService getBrokerService(){
129        return brokerService;
130    }
131
132    /**
133     * Retrieve a set of all Destinations be used by the Broker
134     * @return  all Destinations
135     */
136    public Set<ActiveMQDestination> getDestinations(){
137        Set<ActiveMQDestination> result;
138
139        try {
140            ActiveMQDestination[] destinations =  brokerService.getBroker().getDestinations();
141            result = new HashSet<ActiveMQDestination>();
142            Collections.addAll(result, destinations);
143        }catch (Exception e){
144           result = Collections.emptySet();
145        }
146        return result;
147    }
148
149    /**
150     * Retrieve a set of all Topics be used by the Broker
151     * @return  all Topics
152     */
153
154    public Set<ActiveMQTopic> getTopics(){
155        Set<ActiveMQTopic> result = new HashSet<ActiveMQTopic>();
156        for (ActiveMQDestination destination:getDestinations()){
157            if (destination.isTopic() && !destination.isTemporary()){
158                result.add((ActiveMQTopic) destination);
159            }
160        }
161        return result;
162    }
163
164    /**
165     * Retrieve a set of all Queues be used by the Broker
166     * @return  all Queues
167     */
168
169    public Set<ActiveMQQueue> getQueues(){
170        Set<ActiveMQQueue> result = new HashSet<ActiveMQQueue>();
171        for (ActiveMQDestination destination:getDestinations()){
172            if (destination.isQueue() && !destination.isTemporary()){
173                result.add((ActiveMQQueue) destination);
174            }
175        }
176        return result;
177    }
178
179    /**
180     * Retrieve a set of all TemporaryTopics be used by the Broker
181     * @return  all TemporaryTopics
182     */
183    public Set<ActiveMQTempTopic> getTempTopics(){
184        Set<ActiveMQTempTopic> result = new HashSet<ActiveMQTempTopic>();
185        for (ActiveMQDestination destination:getDestinations()){
186            if (destination.isTopic() && destination.isTemporary()){
187                result.add((ActiveMQTempTopic) destination);
188            }
189        }
190        return result;
191    }
192
193
194    /**
195     * Retrieve a set of all TemporaryQueues be used by the Broker
196     * @return  all TemporaryQueues
197     */
198    public Set<ActiveMQTempQueue> getTempQueues(){
199        Set<ActiveMQTempQueue> result = new HashSet<ActiveMQTempQueue>();
200        for (ActiveMQDestination destination:getDestinations()){
201            if (destination.isQueue() && destination.isTemporary()){
202                result.add((ActiveMQTempQueue) destination);
203            }
204        }
205        return result;
206    }
207
208
209    /**
210     * It will be assumed the destinationName is prepended with topic:// or queue:// - but
211     * will default to a Queue
212     * @param destinationName
213     * @return the BrokerDestinationView associated with the destinationName
214     * @throws Exception
215     */
216
217    public BrokerDestinationView getDestinationView(String destinationName)   throws Exception{
218        return getDestinationView(destinationName,ActiveMQDestination.QUEUE_TYPE);
219    }
220
221    /**
222     * Get the BrokerDestinationView associated with the topic
223     * @param destinationName
224     * @return  BrokerDestinationView
225     * @throws Exception
226     */
227
228    public BrokerDestinationView getTopicDestinationView(String destinationName)   throws Exception{
229        return getDestinationView(destinationName,ActiveMQDestination.TOPIC_TYPE);
230    }
231
232    /**
233     * Get the BrokerDestinationView associated with the queue
234     * @param destinationName
235     * @return  BrokerDestinationView
236     * @throws Exception
237     */
238
239    public BrokerDestinationView getQueueDestinationView(String destinationName) throws Exception{
240        return getDestinationView(destinationName,ActiveMQDestination.QUEUE_TYPE);
241    }
242
243
244    /**
245     * Get the BrokerDestinationView associated with destination
246     * @param destinationName
247     * @param type  expects either ActiveMQDestination.QUEUE_TYPE, ActiveMQDestination.TOPIC_TYPE etc
248     * @return  BrokerDestinationView
249     * @throws Exception
250     */
251    public BrokerDestinationView getDestinationView (String destinationName, byte type)  throws Exception {
252        ActiveMQDestination activeMQDestination = ActiveMQDestination.createDestination(destinationName,type);
253        return getDestinationView(activeMQDestination);
254    }
255
256    /**
257     *  Get the BrokerDestinationView associated with destination
258     * @param activeMQDestination
259     * @return   BrokerDestinationView
260     * @throws Exception
261     */
262    public BrokerDestinationView getDestinationView (ActiveMQDestination activeMQDestination) throws Exception {
263        BrokerDestinationView view = null;
264        synchronized(destinationViewMap){
265            view = destinationViewMap.get(activeMQDestination);
266            if (view==null){
267
268                    /**
269                     * If auto destinatons are allowed (on by default) - this will create a Broker Destination
270                     * if it doesn't exist. We could query the regionBroker first to check - but this affords more
271                     * flexibility - e.g. you might want to set up a query on destination statistics before any
272                     * messaging clients have started (and hence created the destination themselves
273                     */
274                    Destination destination = brokerService.getDestination(activeMQDestination);
275                    view = new BrokerDestinationView(destination);
276                    destinationViewMap.put(activeMQDestination,view);
277
278            }
279        }
280        return view;
281    }
282
283
284
285
286}