1 18 package org.apache.activemq.console.command; 19 20 import org.apache.activemq.console.formatter.GlobalWriter; 21 import org.apache.activemq.console.util.AmqMessagesUtil; 22 import org.apache.activemq.console.util.JmxMBeansUtil; 23 24 import javax.management.ObjectInstance ; 25 import java.util.List ; 26 import java.util.StringTokenizer ; 27 import java.util.ArrayList ; 28 import java.util.Set ; 29 import java.util.HashSet ; 30 import java.util.Iterator ; 31 32 public class BrowseCommand extends AbstractJmxCommand { 33 public static final String QUEUE_PREFIX = "queue:"; 34 public static final String TOPIC_PREFIX = "topic:"; 35 36 public static final String VIEW_GROUP_HEADER = "header:"; 37 public static final String VIEW_GROUP_CUSTOM = "custom:"; 38 public static final String VIEW_GROUP_BODY = "body:"; 39 40 private final List queryAddObjects = new ArrayList (10); 41 private final List querySubObjects = new ArrayList (10); 42 private final Set groupViews = new HashSet (10); 43 private final Set queryViews = new HashSet (10); 44 45 50 protected void runTask(List tokens) throws Exception { 51 try { 52 if (tokens.isEmpty()) { 54 tokens.add("*"); 55 } 56 57 for (Iterator i=tokens.iterator(); i.hasNext();) { 59 List queueList = JmxMBeansUtil.queryMBeans(useJmxServiceUrl(), "Type=Queue,Destination=" + i.next() + ",*"); 60 61 for (Iterator j=queueList.iterator(); j.hasNext();) { 63 List messages = JmxMBeansUtil.createMessageQueryFilter(useJmxServiceUrl(), ((ObjectInstance )j.next()).getObjectName()).query(queryAddObjects); 64 GlobalWriter.printMessage(JmxMBeansUtil.filterMessagesView(messages, groupViews, queryViews)); 65 } 66 } 67 } catch (Exception e) { 68 GlobalWriter.printException(new RuntimeException ("Failed to execute browse task. Reason: " + e)); 69 throw new Exception (e); 70 } 71 } 72 73 79 protected void handleOption(String token, List tokens) throws Exception { 80 81 if (token.startsWith("--msgsel")) { 83 84 if (tokens.isEmpty() || ((String )tokens.get(0)).startsWith("-")) { 86 GlobalWriter.printException(new IllegalArgumentException ("Message selector not specified")); 87 return; 88 } 89 90 StringTokenizer queryTokens = new StringTokenizer ((String )tokens.remove(0), COMMAND_OPTION_DELIMETER); 91 while (queryTokens.hasMoreTokens()) { 92 queryAddObjects.add(queryTokens.nextToken()); 93 } 94 } 95 96 else if (token.startsWith("--xmsgsel")) { 98 99 if (tokens.isEmpty() || ((String )tokens.get(0)).startsWith("-")) { 101 GlobalWriter.printException(new IllegalArgumentException ("Message selector not specified")); 102 return; 103 } 104 105 StringTokenizer queryTokens = new StringTokenizer ((String )tokens.remove(0), COMMAND_OPTION_DELIMETER); 106 while (queryTokens.hasMoreTokens()) { 107 querySubObjects.add(queryTokens.nextToken()); 108 } 109 110 } 111 112 else if (token.startsWith("--view")) { 114 115 if (tokens.isEmpty() || ((String )tokens.get(0)).startsWith("-")) { 117 GlobalWriter.printException(new IllegalArgumentException ("Attributes to view not specified")); 118 return; 119 } 120 121 StringTokenizer viewTokens = new StringTokenizer ((String )tokens.remove(0), COMMAND_OPTION_DELIMETER); 123 while (viewTokens.hasMoreTokens()) { 124 String viewToken = viewTokens.nextToken(); 125 126 if (viewToken.equals(VIEW_GROUP_HEADER)) { 128 queryViews.add(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX + viewToken.substring(VIEW_GROUP_HEADER.length())); 129 130 } else if (viewToken.equals(VIEW_GROUP_CUSTOM)) { 132 queryViews.add(AmqMessagesUtil.JMS_MESSAGE_CUSTOM_PREFIX + viewToken.substring(VIEW_GROUP_CUSTOM.length())); 133 134 } else if (viewToken.equals(VIEW_GROUP_BODY)) { 136 queryViews.add(AmqMessagesUtil.JMS_MESSAGE_BODY_PREFIX + viewToken.substring(VIEW_GROUP_BODY.length())); 137 138 } else { 140 queryViews.add(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX + viewToken); 141 queryViews.add(AmqMessagesUtil.JMS_MESSAGE_CUSTOM_PREFIX + viewToken); 142 queryViews.add(AmqMessagesUtil.JMS_MESSAGE_BODY_PREFIX + viewToken); 143 } 144 } 145 } 146 147 else if (token.startsWith("-V")) { 149 String viewGroup = token.substring(2); 150 if (viewGroup.equals("header")) { 152 groupViews.add(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX); 153 154 } else if (viewGroup.equals("custom")) { 156 groupViews.add(AmqMessagesUtil.JMS_MESSAGE_CUSTOM_PREFIX); 157 158 } else if (viewGroup.equals("body")) { 160 groupViews.add(AmqMessagesUtil.JMS_MESSAGE_BODY_PREFIX); 161 162 } else { 164 GlobalWriter.printInfo("Unknown group view: " + viewGroup + ". Ignoring group view option."); 165 } 166 } 167 168 else { 170 super.handleOption(token, tokens); 171 } 172 } 173 174 177 protected void printHelp() { 178 GlobalWriter.printHelp(helpFile); 179 } 180 181 protected String [] helpFile = new String [] { 182 "Task Usage: Main browse [browse-options] <destinations>", 183 "Description: Display selected destination's messages.", 184 "", 185 "Browse Options:", 186 " --msgsel <msgsel1,msglsel2> Add to the search list messages matched by the query similar to", 187 " the messages selector format.", 188 " -V<header|custom|body> Predefined view that allows you to view the message header, custom", 189 " message header, or the message body.", 190 " --view <attr1>,<attr2>,... Select the specific attribute of the message to view.", 191 " --jmxurl <url> Set the JMX URL to connect to.", 192 " --version Display the version information.", 193 " -h,-?,--help Display the browse broker help information.", 194 "", 195 "Examples:", 196 " Main browse FOO.BAR", 197 " - Print the message header, custom message header, and message body of all messages in the", 198 " queue FOO.BAR", 199 "", 200 " Main browse -Vheader,body queue:FOO.BAR", 201 " - Print only the message header and message body of all messages in the queue FOO.BAR", 202 "", 203 " Main browse -Vheader --view custom:MyField queue:FOO.BAR", 204 " - Print the message header and the custom field 'MyField' of all messages in the queue FOO.BAR", 205 "", 206 " Main browse --msgsel JMSMessageID='*:10',JMSPriority>5 FOO.BAR", 207 " - Print all the message fields that has a JMSMessageID in the header field that matches the", 208 " wildcard *:10, and has a JMSPriority field > 5 in the queue FOO.BAR", 209 " * To use wildcard queries, the field must be a string and the query enclosed in ''", 210 "", 211 }; 212 } 213 | Popular Tags |