KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > console > command > PurgeCommand


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.console.command;
19
20 import org.apache.activemq.console.formatter.GlobalWriter;
21 import org.apache.activemq.console.util.JmxMBeansUtil;
22
23 import javax.management.ObjectInstance JavaDoc;
24 import javax.management.ObjectName JavaDoc;
25 import javax.management.MBeanServerConnection JavaDoc;
26 import javax.management.openmbean.CompositeData JavaDoc;
27 import javax.management.remote.JMXConnector JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.StringTokenizer JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Iterator JavaDoc;
32
33 public class PurgeCommand extends AbstractJmxCommand {
34
35     private final List JavaDoc queryAddObjects = new ArrayList JavaDoc(10);
36     private final List JavaDoc querySubObjects = new ArrayList JavaDoc(10);
37
38     /**
39      * Execute the purge command, which allows you to purge the messages in a given JMS destination
40      * @param tokens - command arguments
41      * @throws Exception
42      */

43     protected void runTask(List JavaDoc tokens) throws Exception JavaDoc {
44         try {
45             // If there is no queue name specified, let's select all
46
if (tokens.isEmpty()) {
47                 tokens.add("*");
48             }
49
50             // Iterate through the queue names
51
for (Iterator JavaDoc i=tokens.iterator(); i.hasNext();) {
52                 List JavaDoc queueList = JmxMBeansUtil.queryMBeans(useJmxServiceUrl(), "Type=Queue,Destination=" + i.next() + ",*");
53
54                 for (Iterator JavaDoc j=queueList.iterator(); j.hasNext();) {
55                     ObjectName JavaDoc queueName = ((ObjectInstance JavaDoc)j.next()).getObjectName();
56                     if (queryAddObjects.isEmpty()) {
57                         purgeQueue(queueName);
58                     } else {
59                         List JavaDoc messages = JmxMBeansUtil.createMessageQueryFilter(useJmxServiceUrl(), queueName).query(queryAddObjects);
60                         purgeMessages(queueName, messages);
61                     }
62                 }
63             }
64         } catch (Exception JavaDoc e) {
65             GlobalWriter.printException(new RuntimeException JavaDoc("Failed to execute purge task. Reason: " + e));
66             throw new Exception JavaDoc(e);
67         }
68     }
69
70     /**
71      * Purge all the messages in the queue
72      * @param queue - ObjectName of the queue to purge
73      * @throws Exception
74      */

75     public void purgeQueue(ObjectName JavaDoc queue) throws Exception JavaDoc {
76         JMXConnector JavaDoc conn = createJmxConnector();
77         MBeanServerConnection JavaDoc server = conn.getMBeanServerConnection();
78         GlobalWriter.printInfo("Purging all messages in queue: " + queue.getKeyProperty("Destination"));
79         server.invoke(queue, "purge", new Object JavaDoc[] {}, new String JavaDoc[] {});
80         conn.close();
81     }
82
83     /**
84      * Purge selected messages in the queue
85      * @param queue - ObjectName of the queue to purge the messages from
86      * @param messages - List of messages to purge
87      * @throws Exception
88      */

89     public void purgeMessages(ObjectName JavaDoc queue, List JavaDoc messages) throws Exception JavaDoc {
90         JMXConnector JavaDoc conn = createJmxConnector();
91         MBeanServerConnection JavaDoc server = conn.getMBeanServerConnection();
92
93         Object JavaDoc[] param = new Object JavaDoc[1];
94         for (Iterator JavaDoc i=messages.iterator(); i.hasNext();) {
95             CompositeData JavaDoc msg = (CompositeData JavaDoc)i.next();
96             param[0] = "" + msg.get("JMSMessageID");
97             GlobalWriter.printInfo("Removing message: " + param[0] + " from queue: " + queue.getKeyProperty("Destination"));
98             server.invoke(queue, "removeMessage", param, new String JavaDoc[] {"java.lang.String"});
99         }
100
101         conn.close();
102     }
103
104     /**
105      * Handle the --msgsel, --xmsgsel.
106      * @param token - option token to handle
107      * @param tokens - succeeding command arguments
108      * @throws Exception
109      */

110     protected void handleOption(String JavaDoc token, List JavaDoc tokens) throws Exception JavaDoc {
111         // If token is an additive message selector option
112
if (token.startsWith("--msgsel")) {
113
114             // If no message selector is specified, or next token is a new option
115
if (tokens.isEmpty() || ((String JavaDoc)tokens.get(0)).startsWith("-")) {
116                 GlobalWriter.printException(new IllegalArgumentException JavaDoc("Message selector not specified"));
117                 return;
118             }
119
120             StringTokenizer JavaDoc queryTokens = new StringTokenizer JavaDoc((String JavaDoc)tokens.remove(0), COMMAND_OPTION_DELIMETER);
121             while (queryTokens.hasMoreTokens()) {
122                 queryAddObjects.add(queryTokens.nextToken());
123             }
124         }
125
126         // If token is a substractive message selector option
127
else if (token.startsWith("--xmsgsel")) {
128
129             // If no message selector is specified, or next token is a new option
130
if (tokens.isEmpty() || ((String JavaDoc)tokens.get(0)).startsWith("-")) {
131                 GlobalWriter.printException(new IllegalArgumentException JavaDoc("Message selector not specified"));
132                 return;
133             }
134
135             StringTokenizer JavaDoc queryTokens = new StringTokenizer JavaDoc((String JavaDoc)tokens.remove(0), COMMAND_OPTION_DELIMETER);
136             while (queryTokens.hasMoreTokens()) {
137                 querySubObjects.add(queryTokens.nextToken());
138             }
139
140         }
141
142         // Let super class handle unknown option
143
else {
144             super.handleOption(token, tokens);
145         }
146     }
147
148     /**
149      * Print the help messages for the browse command
150      */

151     protected void printHelp() {
152         GlobalWriter.printHelp(helpFile);
153     }
154
155     protected String JavaDoc[] helpFile = new String JavaDoc[] {
156         "Task Usage: Main purge [browse-options] <destinations>",
157         "Description: Delete selected destination's messages that matches the message selector.",
158         "",
159         "Browse Options:",
160         " --msgsel <msgsel1,msglsel2> Add to the search list messages matched by the query similar to",
161         " the messages selector format.",
162         " --jmxurl <url> Set the JMX URL to connect to.",
163         " --version Display the version information.",
164         " -h,-?,--help Display the browse broker help information.",
165         "",
166         "Examples:",
167         " Main purge FOO.BAR",
168         " - Delete all the messages in queue FOO.BAR",
169
170         " Main purge --msgsel JMSMessageID='*:10',JMSPriority>5 FOO.*",
171         " - Delete all the messages in the destinations that matches FOO.* and has a JMSMessageID in",
172         " the header field that matches the wildcard *:10, and has a JMSPriority field > 5 in the",
173         " queue FOO.BAR",
174         " * To use wildcard queries, the field must be a string and the query enclosed in ''",
175         "",
176     };
177 }
178
Popular Tags