mirror of https://github.com/apache/activemq.git
https://issues.apache.org/jira/browse/AMQ-5956 - improve CLI commands error handling
This commit is contained in:
parent
c7b93d1232
commit
73f9131a62
|
@ -20,6 +20,7 @@ import java.io.BufferedReader;
|
|||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.ConnectException;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.activemq.ActiveMQConnectionMetaData;
|
||||
|
@ -159,4 +160,20 @@ public abstract class AbstractCommand implements Command {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void handleException(Throwable exception, String serviceUrl) {
|
||||
Throwable cause = exception.getCause();
|
||||
while (true) {
|
||||
Throwable next = cause.getCause();
|
||||
if (next == null) {
|
||||
break;
|
||||
}
|
||||
cause = next;
|
||||
}
|
||||
if (cause instanceof ConnectException) {
|
||||
context.printInfo("Broker not available at: " + serviceUrl);
|
||||
} else {
|
||||
context.printException(new RuntimeException("Failed to execute " + getName() + " task. Reason: " + exception));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,6 +20,7 @@ import java.io.File;
|
|||
import java.io.IOException;
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.ConnectException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
|
@ -385,6 +386,9 @@ public abstract class AbstractJmxCommand extends AbstractCommand {
|
|||
public void execute(List<String> tokens) throws Exception {
|
||||
try {
|
||||
super.execute(tokens);
|
||||
} catch (Throwable exception) {
|
||||
handleException(exception, jmxServiceUrl.toString());
|
||||
return;
|
||||
}finally {
|
||||
closeJmxConnection();
|
||||
}
|
||||
|
|
|
@ -25,6 +25,7 @@ import java.util.StringTokenizer;
|
|||
|
||||
import javax.jms.Destination;
|
||||
|
||||
import org.apache.activemq.ActiveMQConnectionFactory;
|
||||
import org.apache.activemq.command.ActiveMQQueue;
|
||||
import org.apache.activemq.command.ActiveMQTopic;
|
||||
import org.apache.activemq.console.util.AmqMessagesUtil;
|
||||
|
@ -39,11 +40,11 @@ public class AmqBrowseCommand extends AbstractAmqCommand {
|
|||
public static final String VIEW_GROUP_BODY = "body:";
|
||||
|
||||
protected String[] helpFile = new String[] {
|
||||
"Task Usage: Main browse --amqurl <broker url> [browse-options] <destinations>",
|
||||
"Task Usage: Main browse [browse-options] <destinations>",
|
||||
"Description: Display selected destination's messages.",
|
||||
"",
|
||||
"Browse Options:",
|
||||
" --amqurl <url> Set the broker URL to connect to.",
|
||||
" --amqurl <url> Set the broker URL to connect to. Default tcp://localhost:61616",
|
||||
" --msgsel <msgsel1,msglsel2> Add to the search list messages matched by the query similar to",
|
||||
" the messages selector format.",
|
||||
" --factory <className> Load className as the javax.jms.ConnectionFactory to use for creating connections.",
|
||||
|
@ -116,8 +117,7 @@ public class AmqBrowseCommand extends AbstractAmqCommand {
|
|||
|
||||
// If no broker url specified
|
||||
if (getBrokerUrl() == null) {
|
||||
context.printException(new IllegalStateException("No broker url specified. Use the --amqurl option to specify a broker url."));
|
||||
return;
|
||||
setBrokerUrl(ActiveMQConnectionFactory.DEFAULT_BROKER_BIND_URL);
|
||||
}
|
||||
|
||||
// Display the messages for each destination
|
||||
|
@ -151,9 +151,8 @@ public class AmqBrowseCommand extends AbstractAmqCommand {
|
|||
context.printMessage(AmqMessagesUtil.filterMessagesView(addMsgs, groupViews, queryViews));
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
context.printException(new RuntimeException("Failed to execute browse task. Reason: " + e));
|
||||
throw new Exception(e);
|
||||
} catch (Exception exception) {
|
||||
handleException(exception, getBrokerUrl().toString());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -97,7 +97,6 @@ public class BrowseCommand extends AbstractJmxCommand {
|
|||
* @throws Exception
|
||||
*/
|
||||
protected void runTask(List<String> tokens) throws Exception {
|
||||
try {
|
||||
// If there is no queue name specified, let's select all
|
||||
if (tokens.isEmpty()) {
|
||||
tokens.add("*");
|
||||
|
@ -113,10 +112,6 @@ public class BrowseCommand extends AbstractJmxCommand {
|
|||
context.printMessage(JmxMBeansUtil.filterMessagesView(messages, groupViews, queryViews));
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
context.printException(new RuntimeException("Failed to execute browse task. Reason: " + e));
|
||||
throw new Exception(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -66,8 +66,6 @@ public class DstatCommand extends AbstractJmxCommand {
|
|||
*/
|
||||
@Override
|
||||
protected void runTask(List<String> tokens) throws Exception {
|
||||
try {
|
||||
|
||||
if (tokens.contains("topics")) {
|
||||
displayTopicStats();
|
||||
} else if (tokens.contains("queues")) {
|
||||
|
@ -75,12 +73,6 @@ public class DstatCommand extends AbstractJmxCommand {
|
|||
} else {
|
||||
displayAllDestinations();
|
||||
}
|
||||
|
||||
// Iterate through the queue names
|
||||
} catch (Exception e) {
|
||||
context.printException(new RuntimeException("Failed to execute dstat task. Reason: " + e.getMessage(), e));
|
||||
throw new Exception(e);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
|
|
|
@ -55,14 +55,9 @@ public class ListCommand extends AbstractJmxCommand {
|
|||
* @throws Exception
|
||||
*/
|
||||
protected void runTask(List tokens) throws Exception {
|
||||
try {
|
||||
Set<String> propsView = new HashSet<String>();
|
||||
propsView.add("brokerName");
|
||||
context.printMBean(JmxMBeansUtil.filterMBeansView(JmxMBeansUtil.getAllBrokers(createJmxConnection()), propsView));
|
||||
} catch (Exception e) {
|
||||
context.printException(new RuntimeException("Failed to execute list task. Reason: " + e));
|
||||
throw new Exception(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -83,7 +83,6 @@ public class PurgeCommand extends AbstractJmxCommand {
|
|||
*/
|
||||
@Override
|
||||
protected void runTask(List<String> tokens) throws Exception {
|
||||
try {
|
||||
// If there is no queue name specified, let's select all
|
||||
if (tokens.isEmpty()) {
|
||||
tokens.add("*");
|
||||
|
@ -135,10 +134,6 @@ public class PurgeCommand extends AbstractJmxCommand {
|
|||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
context.printException(new RuntimeException("Failed to execute purge task. Reason: " + e));
|
||||
throw new Exception(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -116,7 +116,6 @@ public class QueryCommand extends AbstractJmxCommand {
|
|||
* @throws Exception
|
||||
*/
|
||||
protected void runTask(List<String> tokens) throws Exception {
|
||||
try {
|
||||
// Query for the mbeans to add
|
||||
Map<Object, List> addMBeans = JmxMBeansUtil.queryMBeansAsMap(createJmxConnection(), queryAddObjects, queryViews);
|
||||
// Query for the mbeans to sub
|
||||
|
@ -130,10 +129,6 @@ public class QueryCommand extends AbstractJmxCommand {
|
|||
} else {
|
||||
context.print(doInvoke(addMBeans.keySet(), opAndParams));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
context.printException(new RuntimeException("Failed to execute query task. Reason: " + e));
|
||||
throw new Exception(e);
|
||||
}
|
||||
}
|
||||
|
||||
private Collection doInvoke(Set<Object> mBeans, List<String> opAndParams) throws Exception {
|
||||
|
|
|
@ -69,7 +69,6 @@ public class ShutdownCommand extends AbstractJmxCommand {
|
|||
* @throws Exception
|
||||
*/
|
||||
protected void runTask(List brokerNames) throws Exception {
|
||||
try {
|
||||
Collection mbeans;
|
||||
|
||||
// Stop all brokers
|
||||
|
@ -112,10 +111,6 @@ public class ShutdownCommand extends AbstractJmxCommand {
|
|||
|
||||
// Stop all brokers in set
|
||||
stopBrokers(createJmxConnection(), mbeans);
|
||||
} catch (Exception e) {
|
||||
context.printException(new RuntimeException("Failed to execute stop task. Reason: " + e));
|
||||
throw new Exception(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -657,37 +657,7 @@ case "$1" in
|
|||
invoke_kill
|
||||
exit $?
|
||||
;;
|
||||
decrypt|encrypt|create)
|
||||
invoke_task checknotforrunning
|
||||
exit $?
|
||||
;;
|
||||
export)
|
||||
invoke_task checkfornotrunning
|
||||
exit $?
|
||||
;;
|
||||
query|bstat|dstat|purge)
|
||||
# Only check for a running broker if "--jmxurl" is part of the COMMANDLINE_ARGS
|
||||
if (echo "$COMMANDLINE_ARGS"|grep -q -- "--jmxurl");then
|
||||
invoke_task checknotforrunning
|
||||
RET="$?"
|
||||
else
|
||||
invoke_task checkforrunning
|
||||
RET="$?"
|
||||
fi
|
||||
exit $RET
|
||||
;;
|
||||
browse)
|
||||
# Only check for a running broker if "--amqurl" is part of the COMMANDLINE_ARGS
|
||||
if (echo "$COMMANDLINE_ARGS"|grep -q -- "--amqurl");then
|
||||
invoke_task checknotforrunning
|
||||
RET="$?"
|
||||
else
|
||||
invoke_task checkforrunning
|
||||
RET="$?"
|
||||
fi
|
||||
exit $RET
|
||||
;;
|
||||
*)
|
||||
invoke_task checkforrunning
|
||||
invoke_task checknotforrunning
|
||||
exit $?
|
||||
esac
|
||||
|
|
Loading…
Reference in New Issue