1 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 ; 26 import java.util.ArrayList ; 27 import java.util.Iterator ; 28 import java.net.URI ; 29 import java.net.URISyntaxException ; 30 31 public class StartCommand extends AbstractCommand { 32 33 public static final String DEFAULT_CONFIG_URI = "xbean:activemq.xml"; 34 35 private URI configURI; 36 private List brokers = new ArrayList (5); 37 38 42 protected void runTask(List brokerURIs) throws Exception { 43 try { 44 if (brokerURIs.isEmpty()) { 46 setConfigUri(new URI (DEFAULT_CONFIG_URI)); 47 startBroker(getConfigUri()); 48 49 } else { 51 String strConfigURI; 52 53 while (!brokerURIs.isEmpty()) { 54 strConfigURI = (String )brokerURIs.remove(0); 55 56 try { 57 setConfigUri(new URI (strConfigURI)); 58 } catch (URISyntaxException e) { 59 GlobalWriter.printException(e); 60 return; 61 } 62 63 startBroker(getConfigUri()); 64 } 65 } 66 67 waitForShutdown(); 69 } catch (Exception e) { 70 GlobalWriter.printException(new RuntimeException ("Failed to execute start task. Reason: " + e, e)); 71 throw new Exception (e); 72 } 73 } 74 75 80 public void startBroker(URI configURI) throws Exception { 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 92 protected void waitForShutdown() throws Exception { 93 final boolean[] shutdown = new boolean[] {false}; 94 Runtime.getRuntime().addShutdownHook(new Thread () { 95 public void run() { 96 synchronized(shutdown) { 97 shutdown[0]=true; 98 shutdown.notify(); 99 } 100 } 101 }); 102 103 synchronized(shutdown) { 105 while( !shutdown[0] ) { 106 try { 107 shutdown.wait(); 108 } catch (InterruptedException e) { 109 } 110 } 111 } 112 113 for (Iterator i=brokers.iterator(); i.hasNext();) { 115 BrokerService broker = (BrokerService)i.next(); 116 broker.stop(); 117 } 118 } 119 120 124 public void setConfigUri(URI uri) { 125 configURI = uri; 126 } 127 128 132 public URI getConfigUri() { 133 return configURI; 134 } 135 136 139 protected void printHelp() { 140 GlobalWriter.printHelp(helpFile); 141 } 142 143 protected String [] helpFile = new String [] { 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 |