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.util.List;
022    import java.util.concurrent.CountDownLatch;
023    
024    import org.apache.activemq.broker.BrokerFactory;
025    import org.apache.activemq.broker.BrokerService;
026    
027    public class StartCommand extends AbstractCommand {
028    
029        public static final String DEFAULT_CONFIG_URI = "xbean:activemq.xml";
030    
031        protected String[] helpFile = new String[] {
032            "Task Usage: Main start [start-options] [uri]",
033            "Description: Creates and starts a broker using a configuration file, or a broker URI.",
034            "",
035            "Start Options:",
036            "    -D<name>=<value>      Define a system property.",
037            "    --version             Display the version information.", 
038            "    -h,-?,--help          Display the start broker help information.",
039            "",
040            "URI:",
041            "",
042            "    XBean based broker configuration:",
043            "",
044            "        Example: Main xbean:file:activemq.xml",
045            "            Loads the xbean configuration file from the current working directory",
046            "        Example: Main xbean:activemq.xml",
047            "            Loads the xbean configuration file from the classpath",
048            "",
049            "    URI Parameter based broker configuration:",
050            "",
051            "        Example: Main broker:(tcp://localhost:61616, tcp://localhost:5000)?useJmx=true",
052            "            Configures the broker with 2 transport connectors and jmx enabled",
053            "        Example: Main broker:(tcp://localhost:61616, network:tcp://localhost:5000)?persistent=false",
054            "            Configures the broker with 1 transport connector, and 1 network connector and persistence disabled",
055            ""
056        };
057    
058        @Override
059        public String getName() {
060            return "start";
061        }
062    
063        @Override
064        public String getOneLineDescription() {
065            return "Creates and starts a broker using a configuration file, or a broker URI.";
066        }
067    
068        /**
069         * The default task to start a broker or a group of brokers
070         * 
071         * @param brokerURIs
072         */
073        protected void runTask(List<String> brokerURIs) throws Exception {
074            URI configURI;
075    
076            while( true ) {
077                final BrokerService broker;
078                try {
079                    // If no config uri, use default setting
080                    if (brokerURIs.isEmpty()) {
081                        configURI = new URI(DEFAULT_CONFIG_URI);
082                    } else {
083                        configURI = new URI(brokerURIs.get(0));
084                    }
085    
086                    System.out.println("Loading message broker from: " + configURI);
087                    broker = BrokerFactory.createBroker(configURI);
088                    broker.start();
089    
090                } catch (Exception e) {
091                    context.printException(new RuntimeException("Failed to execute start task. Reason: " + e, e));
092                    throw e;
093                }
094    
095                if (!broker.waitUntilStarted()) {
096                    throw new Exception(broker.getStartException());
097                }
098    
099                // The broker started up fine.  Now lets wait for it to stop...
100                final CountDownLatch shutdownLatch = new CountDownLatch(1);
101                final Thread jvmShutdownHook = new Thread() {
102                    public void run() {
103                        try {
104                            broker.stop();
105                        } catch (Exception e) {
106                        }
107                    }
108                };
109    
110                Runtime.getRuntime().addShutdownHook(jvmShutdownHook);
111                broker.addShutdownHook(new Runnable() {
112                    public void run() {
113                        shutdownLatch.countDown();
114                    }
115                });
116    
117                // The broker has stopped..
118                shutdownLatch.await();
119                try {
120                    Runtime.getRuntime().removeShutdownHook(jvmShutdownHook);
121                } catch (Throwable e) {
122                    // may already be shutdown in progress so ignore
123                }
124    
125                if( !broker.isRestartRequested() ) {
126                    break;
127                }
128                System.out.println("Restarting broker");
129            }
130        }
131    
132        /**
133         * Print the help messages for the browse command
134         */
135        protected void printHelp() {
136            context.printHelp(helpFile);
137        }
138    
139    }