KickJava   Java API By Example, From Geeks To Geeks.

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


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
19 package org.apache.activemq.console.command;
20
21 import org.apache.activemq.broker.BrokerFactory;
22 import org.apache.activemq.broker.BrokerService;
23 import org.apache.activemq.console.formatter.GlobalWriter;
24
25 import java.util.List JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.net.URI JavaDoc;
29 import java.net.URISyntaxException JavaDoc;
30
31 public class StartCommand extends AbstractCommand {
32
33     public static final String JavaDoc DEFAULT_CONFIG_URI = "xbean:activemq.xml";
34
35     private URI JavaDoc configURI;
36     private List JavaDoc brokers = new ArrayList JavaDoc(5);
37
38     /**
39      * The default task to start a broker or a group of brokers
40      * @param brokerURIs
41      */

42     protected void runTask(List JavaDoc brokerURIs) throws Exception JavaDoc {
43         try {
44             // If no config uri, use default setting
45
if (brokerURIs.isEmpty()) {
46                 setConfigUri(new URI JavaDoc(DEFAULT_CONFIG_URI));
47                 startBroker(getConfigUri());
48
49             // Set configuration data, if available, which in this case would be the config URI
50
} else {
51                 String JavaDoc strConfigURI;
52
53                 while (!brokerURIs.isEmpty()) {
54                     strConfigURI = (String JavaDoc)brokerURIs.remove(0);
55
56                     try {
57                         setConfigUri(new URI JavaDoc(strConfigURI));
58                     } catch (URISyntaxException JavaDoc e) {
59                         GlobalWriter.printException(e);
60                         return;
61                     }
62
63                     startBroker(getConfigUri());
64                 }
65             }
66
67             // Prevent the main thread from exiting unless it is terminated elsewhere
68
waitForShutdown();
69         } catch (Exception JavaDoc e) {
70             GlobalWriter.printException(new RuntimeException JavaDoc("Failed to execute start task. Reason: " + e, e));
71             throw new Exception JavaDoc(e);
72         }
73     }
74
75     /**
76      * Create and run a broker specified by the given configuration URI
77      * @param configURI
78      * @throws Exception
79      */

80     public void startBroker(URI JavaDoc configURI) throws Exception JavaDoc {
81         System.out.println("Loading message broker from: " + configURI);
82         BrokerService broker = BrokerFactory.createBroker(configURI);
83         brokers.add(broker);
84
85         broker.start();
86     }
87
88     /**
89      * Wait for a shutdown invocation elsewhere
90      * @throws Exception
91      */

92     protected void waitForShutdown() throws Exception JavaDoc {
93         final boolean[] shutdown = new boolean[] {false};
94         Runtime.getRuntime().addShutdownHook(new Thread JavaDoc() {
95             public void run() {
96                 synchronized(shutdown) {
97                     shutdown[0]=true;
98                     shutdown.notify();
99                 }
100             }
101         });
102
103         // Wait for any shutdown event
104
synchronized(shutdown) {
105             while( !shutdown[0] ) {
106                 try {
107                     shutdown.wait();
108                 } catch (InterruptedException JavaDoc e) {
109                 }
110             }
111         }
112
113         // Stop each broker
114
for (Iterator JavaDoc i=brokers.iterator(); i.hasNext();) {
115             BrokerService broker = (BrokerService)i.next();
116             broker.stop();
117         }
118     }
119
120     /**
121      * Sets the current configuration URI used by the start task
122      * @param uri
123      */

124     public void setConfigUri(URI JavaDoc uri) {
125         configURI = uri;
126     }
127
128     /**
129      * Gets the current configuration URI used by the start task
130      * @return current configuration URI
131      */

132     public URI JavaDoc getConfigUri() {
133         return configURI;
134     }
135
136     /**
137      * Print the help messages for the browse command
138      */

139     protected void printHelp() {
140         GlobalWriter.printHelp(helpFile);
141     }
142
143     protected String JavaDoc[] helpFile = new String JavaDoc[] {
144         "Task Usage: Main start [start-options] [uri]",
145         "Description: Creates and starts a broker using a configuration file, or a broker URI.",
146         "",
147         "Start Options:",
148         " -D<name>=<value> Define a system property.",
149         " --version Display the version information.",
150         " -h,-?,--help Display the start broker help information.",
151         "",
152         "URI:",
153         "",
154         " XBean based broker configuration:",
155         "",
156         " Example: Main xbean:file:activemq.xml",
157         " Loads the xbean configuration file from the current working directory",
158         " Example: Main xbean:activemq.xml",
159         " Loads the xbean configuration file from the classpath",
160         "",
161         " URI Parameter based broker configuration:",
162         "",
163         " Example: Main broker:(tcp://localhost:61616, tcp://localhost:5000)?useJmx=true",
164         " Configures the broker with 2 transport connectors and jmx enabled",
165         " Example: Main broker:(tcp://localhost:61616, network:tcp://localhost:5000)?persistent=false",
166         " Configures the broker with 1 transport connector, and 1 network connector and persistence disabled",
167         ""
168     };
169 }
170
Popular Tags