1 18 package org.apache.activemq.console.command; 19 20 import org.apache.activemq.console.util.JmxMBeansUtil; 21 import org.apache.activemq.console.formatter.GlobalWriter; 22 23 import javax.management.MBeanServerConnection ; 24 import javax.management.ObjectName ; 25 import javax.management.ObjectInstance ; 26 import javax.management.remote.JMXServiceURL ; 27 import java.util.List ; 28 import java.util.Iterator ; 29 import java.util.Collection ; 30 import java.util.HashSet ; 31 32 public class ShutdownCommand extends AbstractJmxCommand { 33 private boolean isStopAllBrokers = false; 34 35 40 protected void runTask(List brokerNames) throws Exception { 41 try { 42 Collection mbeans; 43 44 if (isStopAllBrokers) { 46 mbeans = JmxMBeansUtil.getAllBrokers(useJmxServiceUrl()); 47 brokerNames.clear(); 48 } 49 50 else if (brokerNames.isEmpty()) { 52 mbeans = JmxMBeansUtil.getAllBrokers(useJmxServiceUrl()); 53 54 if (mbeans.isEmpty()) { 56 GlobalWriter.printInfo("There are no brokers to stop."); 57 return; 58 59 } else if (mbeans.size() > 1) { 61 GlobalWriter.printInfo("There are multiple brokers to stop. Please select the broker(s) to stop or use --all to stop all brokers."); 62 return; 63 64 } else { 66 Object firstBroker = mbeans.iterator().next(); 67 mbeans.clear(); 68 mbeans.add(firstBroker); 69 } 70 } 71 72 else { 74 String brokerName; 75 mbeans = new HashSet (); 76 while (!brokerNames.isEmpty()) { 77 brokerName = (String )brokerNames.remove(0); 78 Collection matchedBrokers = JmxMBeansUtil.getBrokersByName(useJmxServiceUrl(), brokerName); 79 if (matchedBrokers.isEmpty()) { 80 GlobalWriter.printInfo(brokerName + " did not match any running brokers."); 81 } else { 82 mbeans.addAll(matchedBrokers); 83 } 84 } 85 } 86 87 stopBrokers(useJmxServiceUrl(), mbeans); 89 } catch (Exception e) { 90 GlobalWriter.printException(new RuntimeException ("Failed to execute stop task. Reason: " + e)); 91 throw new Exception (e); 92 } 93 } 94 95 101 protected void stopBrokers(JMXServiceURL jmxServiceUrl, Collection brokerBeans) throws Exception { 102 MBeanServerConnection server = createJmxConnector().getMBeanServerConnection(); 103 104 ObjectName brokerObjName; 105 for (Iterator i=brokerBeans.iterator(); i.hasNext();) { 106 brokerObjName = ((ObjectInstance )i.next()).getObjectName(); 107 108 String brokerName = brokerObjName.getKeyProperty("BrokerName"); 109 GlobalWriter.print("Stopping broker: " + brokerName); 110 111 try { 112 server.invoke(brokerObjName, "terminateJVM", new Object [] {new Integer (0)}, new String [] {"int"}); 113 GlobalWriter.print("Succesfully stopped broker: " + brokerName); 114 } catch (Exception e) { 115 } 118 } 119 120 closeJmxConnector(); 121 } 122 123 129 protected void handleOption(String token, List tokens) throws Exception { 130 if (token.equals("--all")) { 132 isStopAllBrokers = true; 133 } else { 134 super.handleOption(token, tokens); 136 } 137 } 138 139 142 protected void printHelp() { 143 GlobalWriter.printHelp(helpFile); 144 } 145 146 protected String [] helpFile = new String [] { 147 "Task Usage: Main stop [stop-options] [broker-name1] [broker-name2] ...", 148 "Description: Stops a running broker.", 149 "", 150 "Stop Options:", 151 " --jmxurl <url> Set the JMX URL to connect to.", 152 " --all Stop all brokers.", 153 " --version Display the version information.", 154 " -h,-?,--help Display the stop broker help information.", 155 "", 156 "Broker Names:", 157 " Name of the brokers that will be stopped.", 158 " If omitted, it is assumed that there is only one broker running, and it will be stopped.", 159 " Use -all to stop all running brokers.", 160 "" 161 }; 162 } 163 | Popular Tags |