KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.JmxMBeansUtil;
21 import org.apache.activemq.console.formatter.GlobalWriter;
22
23 import javax.management.MBeanServerConnection JavaDoc;
24 import javax.management.ObjectName JavaDoc;
25 import javax.management.ObjectInstance JavaDoc;
26 import javax.management.remote.JMXServiceURL JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.HashSet JavaDoc;
31
32 public class ShutdownCommand extends AbstractJmxCommand {
33     private boolean isStopAllBrokers = false;
34
35     /**
36      * Shuts down the specified broker or brokers
37      * @param brokerNames - names of brokers to shutdown
38      * @throws Exception
39      */

40     protected void runTask(List JavaDoc brokerNames) throws Exception JavaDoc {
41         try {
42             Collection JavaDoc mbeans;
43
44             // Stop all brokers
45
if (isStopAllBrokers) {
46                 mbeans = JmxMBeansUtil.getAllBrokers(useJmxServiceUrl());
47                 brokerNames.clear();
48             }
49
50             // Stop the default broker
51
else if (brokerNames.isEmpty()) {
52                 mbeans = JmxMBeansUtil.getAllBrokers(useJmxServiceUrl());
53
54                 // If there is no broker to stop
55
if (mbeans.isEmpty()) {
56                     GlobalWriter.printInfo("There are no brokers to stop.");
57                     return;
58
59                 // There should only be one broker to stop
60
} 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                 // Get the first broker only
65
} else {
66                     Object JavaDoc firstBroker = mbeans.iterator().next();
67                     mbeans.clear();
68                     mbeans.add(firstBroker);
69                 }
70             }
71
72             // Stop each specified broker
73
else {
74                 String JavaDoc brokerName;
75                 mbeans = new HashSet JavaDoc();
76                 while (!brokerNames.isEmpty()) {
77                     brokerName = (String JavaDoc)brokerNames.remove(0);
78                     Collection JavaDoc 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             // Stop all brokers in set
88
stopBrokers(useJmxServiceUrl(), mbeans);
89         } catch (Exception JavaDoc e) {
90             GlobalWriter.printException(new RuntimeException JavaDoc("Failed to execute stop task. Reason: " + e));
91             throw new Exception JavaDoc(e);
92         }
93     }
94
95     /**
96      * Stops the list of brokers.
97      * @param jmxServiceUrl - JMX service url to connect to
98      * @param brokerBeans - broker mbeans to stop
99      * @throws Exception
100      */

101     protected void stopBrokers(JMXServiceURL JavaDoc jmxServiceUrl, Collection JavaDoc brokerBeans) throws Exception JavaDoc {
102         MBeanServerConnection JavaDoc server = createJmxConnector().getMBeanServerConnection();
103
104         ObjectName JavaDoc brokerObjName;
105         for (Iterator JavaDoc i=brokerBeans.iterator(); i.hasNext();) {
106             brokerObjName = ((ObjectInstance JavaDoc)i.next()).getObjectName();
107
108             String JavaDoc brokerName = brokerObjName.getKeyProperty("BrokerName");
109             GlobalWriter.print("Stopping broker: " + brokerName);
110
111             try {
112                 server.invoke(brokerObjName, "terminateJVM", new Object JavaDoc[] {new Integer JavaDoc(0)}, new String JavaDoc[] {"int"});
113                 GlobalWriter.print("Succesfully stopped broker: " + brokerName);
114             } catch (Exception JavaDoc e) {
115                 // TODO: Check exceptions throwned
116
//System.out.println("Failed to stop broker: [ " + brokerName + " ]. Reason: " + e.getMessage());
117
}
118         }
119
120         closeJmxConnector();
121     }
122
123     /**
124      * Handle the --all option.
125      * @param token - option token to handle
126      * @param tokens - succeeding command arguments
127      * @throws Exception
128      */

129     protected void handleOption(String JavaDoc token, List JavaDoc tokens) throws Exception JavaDoc {
130         // Try to handle the options first
131
if (token.equals("--all")) {
132             isStopAllBrokers = true;
133         } else {
134             // Let the super class handle the option
135
super.handleOption(token, tokens);
136         }
137     }
138
139     /**
140      * Print the help messages for the browse command
141      */

142     protected void printHelp() {
143         GlobalWriter.printHelp(helpFile);
144     }
145
146     protected String JavaDoc[] helpFile = new String JavaDoc[] {
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