001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.activemq.console.command;
019    
020    import java.net.URI;
021    import java.net.URISyntaxException;
022    import java.util.ArrayList;
023    import java.util.Iterator;
024    import java.util.List;
025    import java.util.concurrent.atomic.AtomicInteger;
026    
027    import org.apache.activemq.broker.BrokerFactory;
028    import org.apache.activemq.broker.BrokerService;
029    
030    public class StartCommand extends AbstractCommand {
031    
032        public static final String DEFAULT_CONFIG_URI = "xbean:activemq.xml";
033    
034        protected String[] helpFile = new String[] {
035            "Task Usage: Main start [start-options] [uri]",
036            "Description: Creates and starts a broker using a configuration file, or a broker URI.",
037            "",
038            "Start Options:",
039            "    -D<name>=<value>      Define a system property.",
040            "    --version             Display the version information.", 
041            "    -h,-?,--help          Display the start broker help information.",
042            "",
043            "URI:",
044            "",
045            "    XBean based broker configuration:",
046            "",
047            "        Example: Main xbean:file:activemq.xml",
048            "            Loads the xbean configuration file from the current working directory",
049            "        Example: Main xbean:activemq.xml",
050            "            Loads the xbean configuration file from the classpath",
051            "",
052            "    URI Parameter based broker configuration:",
053            "",
054            "        Example: Main broker:(tcp://localhost:61616, tcp://localhost:5000)?useJmx=true",
055            "            Configures the broker with 2 transport connectors and jmx enabled",
056            "        Example: Main broker:(tcp://localhost:61616, network:tcp://localhost:5000)?persistent=false",
057            "            Configures the broker with 1 transport connector, and 1 network connector and persistence disabled",
058            ""
059        };
060    
061        private URI configURI;
062        private List<BrokerService> brokers = new ArrayList<BrokerService>(5);
063    
064        @Override
065        public String getName() {
066            return "start";
067        }
068    
069        @Override
070        public String getOneLineDescription() {
071            return "Creates and starts a broker using a configuration file, or a broker URI.";
072        }
073    
074        /**
075         * The default task to start a broker or a group of brokers
076         * 
077         * @param brokerURIs
078         */
079        protected void runTask(List<String> brokerURIs) throws Exception {
080            try {
081                // If no config uri, use default setting
082                if (brokerURIs.isEmpty()) {
083                    setConfigUri(new URI(DEFAULT_CONFIG_URI));
084                    startBroker(getConfigUri());
085    
086                    // Set configuration data, if available, which in this case
087                    // would be the config URI
088                } else {
089                    String strConfigURI;
090    
091                    while (!brokerURIs.isEmpty()) {
092                        strConfigURI = (String)brokerURIs.remove(0);
093    
094                        try {
095                            setConfigUri(new URI(strConfigURI));
096                        } catch (URISyntaxException e) {
097                            context.printException(e);
098                            return;
099                        }
100    
101                        startBroker(getConfigUri());
102                    }
103                }
104    
105                // Prevent the main thread from exiting unless it is terminated
106                // elsewhere
107            } catch (Exception e) {
108                context.printException(new RuntimeException("Failed to execute start task. Reason: " + e, e));
109                throw new Exception(e);
110            }
111            
112            // The broker start up fine.  If this unblocks it's cause they were stopped
113            // and this would occur because of an internal error (like the DB going offline)
114            waitForShutdown();
115        }
116    
117        /**
118         * Create and run a broker specified by the given configuration URI
119         * 
120         * @param configURI
121         * @throws Exception
122         */
123        public void startBroker(URI configURI) throws Exception {
124            System.out.println("Loading message broker from: " + configURI);
125            BrokerService broker = BrokerFactory.createBroker(configURI);
126            brokers.add(broker);
127            broker.start();
128            if (!broker.waitUntilStarted()) {
129                throw new Exception(broker.getStartException());
130            }
131        }
132    
133        /**
134         * Wait for a shutdown invocation elsewhere
135         * 
136         * @throws Exception
137         */
138        protected void waitForShutdown() throws Exception {
139            final boolean[] shutdown = new boolean[] {
140                false
141            };
142            
143            Runtime.getRuntime().addShutdownHook(new Thread() {
144                public void run() {
145                    for (Iterator<BrokerService> i = brokers.iterator(); i.hasNext();) {
146                        try {
147                            BrokerService broker = i.next();
148                            broker.stop();
149                        } catch (Exception e) {
150                        }
151                    }
152                }
153            });
154            
155            final AtomicInteger brokerCounter = new AtomicInteger(brokers.size());
156            for (BrokerService bs : brokers) {
157                bs.addShutdownHook(new Runnable() {
158                    public void run() {
159                        // When the last broker lets us know he is closed....
160                        if( brokerCounter.decrementAndGet() == 0 ) {
161                            synchronized (shutdown) {
162                                shutdown[0] = true;
163                                shutdown.notify();
164                            }
165                        }
166                    }
167                });
168            }
169    
170            // Wait for any shutdown event
171            synchronized (shutdown) {
172                while (!shutdown[0]) {
173                    try {
174                        shutdown.wait();
175                    } catch (InterruptedException e) {
176                    }
177                }
178            }
179    
180        }
181    
182        /**
183         * Sets the current configuration URI used by the start task
184         * 
185         * @param uri
186         */
187        public void setConfigUri(URI uri) {
188            configURI = uri;
189        }
190    
191        /**
192         * Gets the current configuration URI used by the start task
193         * 
194         * @return current configuration URI
195         */
196        public URI getConfigUri() {
197            return configURI;
198        }
199    
200        /**
201         * Print the help messages for the browse command
202         */
203        protected void printHelp() {
204            context.printHelp(helpFile);
205        }
206    
207    }