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 java.util.Map;
020
021import javax.management.openmbean.CompositeData;
022import javax.management.openmbean.OpenDataException;
023import javax.jms.JMSException;
024
025import org.apache.activemq.broker.ConnectionContext;
026import org.apache.activemq.broker.region.Queue;
027import org.apache.activemq.broker.region.QueueMessageReference;
028import org.apache.activemq.command.ActiveMQDestination;
029import org.apache.activemq.command.Message;
030import org.apache.activemq.util.BrokerSupport;
031
032/**
033 * Provides a JMX Management view of a Queue.
034 */
035public class QueueView extends DestinationView implements QueueViewMBean {
036    public QueueView(ManagedRegionBroker broker, Queue destination) {
037        super(broker, destination);
038    }
039
040    public CompositeData getMessage(String messageId) throws OpenDataException {
041        CompositeData result = null;
042        QueueMessageReference ref = ((Queue)destination).getMessage(messageId);
043
044        if (ref != null) {
045                Message rc = ref.getMessage();
046                if (rc == null) {
047                    return null;
048                }
049                result = OpenTypeSupport.convert(rc);
050        }
051
052        return result;
053    }
054
055    public void purge() throws Exception {
056        ((Queue)destination).purge();
057    }
058
059    public boolean removeMessage(String messageId) throws Exception {
060        return ((Queue)destination).removeMessage(messageId);
061    }
062
063    public int removeMatchingMessages(String selector) throws Exception {
064        return ((Queue)destination).removeMatchingMessages(selector);
065    }
066
067    public int removeMatchingMessages(String selector, int maximumMessages) throws Exception {
068        return ((Queue)destination).removeMatchingMessages(selector, maximumMessages);
069    }
070
071    public boolean copyMessageTo(String messageId, String destinationName) throws Exception {
072        ConnectionContext context = BrokerSupport.getConnectionContext(broker.getContextBroker());
073        ActiveMQDestination toDestination = ActiveMQDestination.createDestination(destinationName, ActiveMQDestination.QUEUE_TYPE);
074        return ((Queue)destination).copyMessageTo(context, messageId, toDestination);
075    }
076
077    public int copyMatchingMessagesTo(String selector, String destinationName) throws Exception {
078        ConnectionContext context = BrokerSupport.getConnectionContext(broker.getContextBroker());
079        ActiveMQDestination toDestination = ActiveMQDestination.createDestination(destinationName, ActiveMQDestination.QUEUE_TYPE);
080        return ((Queue)destination).copyMatchingMessagesTo(context, selector, toDestination);
081    }
082
083    public int copyMatchingMessagesTo(String selector, String destinationName, int maximumMessages) throws Exception {
084        ConnectionContext context = BrokerSupport.getConnectionContext(broker.getContextBroker());
085        ActiveMQDestination toDestination = ActiveMQDestination.createDestination(destinationName, ActiveMQDestination.QUEUE_TYPE);
086        return ((Queue)destination).copyMatchingMessagesTo(context, selector, toDestination, maximumMessages);
087    }
088
089    public boolean moveMessageTo(String messageId, String destinationName) throws Exception {
090        ConnectionContext context = BrokerSupport.getConnectionContext(broker.getContextBroker());
091        ActiveMQDestination toDestination = ActiveMQDestination.createDestination(destinationName, ActiveMQDestination.QUEUE_TYPE);
092        return ((Queue)destination).moveMessageTo(context, messageId, toDestination);
093    }
094
095    public int moveMatchingMessagesTo(String selector, String destinationName) throws Exception {
096        ConnectionContext context = BrokerSupport.getConnectionContext(broker.getContextBroker());
097        ActiveMQDestination toDestination = ActiveMQDestination.createDestination(destinationName, ActiveMQDestination.QUEUE_TYPE);
098        return ((Queue)destination).moveMatchingMessagesTo(context, selector, toDestination);
099    }
100
101    public int moveMatchingMessagesTo(String selector, String destinationName, int maximumMessages) throws Exception {
102        ConnectionContext context = BrokerSupport.getConnectionContext(broker.getContextBroker());
103        ActiveMQDestination toDestination = ActiveMQDestination.createDestination(destinationName, ActiveMQDestination.QUEUE_TYPE);
104        return ((Queue)destination).moveMatchingMessagesTo(context, selector, toDestination, maximumMessages);
105    }
106
107    public int retryMessages() throws Exception {
108        ConnectionContext context = BrokerSupport.getConnectionContext(broker.getContextBroker());
109        return ((Queue)destination).retryMessages(context, Integer.MAX_VALUE);
110    }
111
112    /**
113     * Moves a message back to its original destination
114     */
115    public boolean retryMessage(String messageId) throws Exception {
116        Queue queue = (Queue) destination;
117        QueueMessageReference ref = queue.getMessage(messageId);
118        Message rc = ref.getMessage();
119        if (rc != null) {
120            ActiveMQDestination originalDestination = rc.getOriginalDestination();
121            if (originalDestination != null) {
122                ConnectionContext context = BrokerSupport.getConnectionContext(broker.getContextBroker());
123                return queue.moveMessageTo(context, ref, originalDestination);
124            }
125            else {
126                throw new JMSException("No original destination for message: "+ messageId);
127            }
128        }
129        else {
130            throw new JMSException("Could not find message: "+ messageId);
131        }
132    }
133
134    public int cursorSize() {
135        Queue queue = (Queue) destination;
136        if (queue.getMessages() != null){
137            return queue.getMessages().size();
138        }
139        return 0;
140    }
141
142
143    public boolean doesCursorHaveMessagesBuffered() {
144       Queue queue = (Queue) destination;
145       if (queue.getMessages() != null){
146           return queue.getMessages().hasMessagesBufferedToDeliver();
147       }
148       return false;
149
150    }
151
152
153    public boolean doesCursorHaveSpace() {
154        Queue queue = (Queue) destination;
155        if (queue.getMessages() != null){
156            return queue.getMessages().hasSpace();
157        }
158        return false;
159    }
160
161
162    public long getCursorMemoryUsage() {
163        Queue queue = (Queue) destination;
164        if (queue.getMessages() != null &&  queue.getMessages().getSystemUsage() != null){
165            return queue.getMessages().getSystemUsage().getMemoryUsage().getUsage();
166        }
167        return 0;
168    }
169
170    public int getCursorPercentUsage() {
171        Queue queue = (Queue) destination;
172        if (queue.getMessages() != null &&  queue.getMessages().getSystemUsage() != null){
173            return queue.getMessages().getSystemUsage().getMemoryUsage().getPercentUsage();
174        }
175        return 0;
176    }
177
178    public boolean isCursorFull() {
179        Queue queue = (Queue) destination;
180        if (queue.getMessages() != null){
181            return queue.getMessages().isFull();
182        }
183        return false;
184    }
185
186    public boolean isCacheEnabled() {
187        Queue queue = (Queue) destination;
188        if (queue.getMessages() != null){
189            return queue.getMessages().isCacheEnabled();
190        }
191        return false;
192    }
193
194    /**
195     * @return a Map of groupNames and ConsumerIds
196     */
197    @Override
198    public Map<String, String> getMessageGroups() {
199        Queue queue = (Queue) destination;
200        return queue.getMessageGroupOwners().getGroups();
201    }
202
203    /**
204     * @return the message group type implementation (simple,bucket,cached)
205     */
206    @Override
207    public String getMessageGroupType() {
208        Queue queue = (Queue) destination;
209        return queue.getMessageGroupOwners().getType();
210    }
211
212    /**
213     * remove a message group = has the effect of rebalancing group
214     */
215    @Override
216    public void removeMessageGroup(@MBeanInfo("groupName") String groupName) {
217        Queue queue = (Queue) destination;
218        queue.getMessageGroupOwners().removeGroup(groupName);
219    }
220
221    /**
222     * remove all the message groups - will rebalance all message groups across consumers
223     */
224    @Override
225    public void removeAllMessageGroups() {
226        Queue queue = (Queue) destination;
227        queue.getMessageGroupOwners().removeAll();
228    }
229}