1818
1919import com .rabbitmq .tools .Host ;
2020
21+ import java .io .BufferedReader ;
2122import java .io .File ;
2223import java .io .FileWriter ;
2324import java .io .IOException ;
25+ import java .io .InputStream ;
26+ import java .io .InputStreamReader ;
2427import java .io .PrintWriter ;
2528
2629public class Broker {
@@ -47,27 +50,37 @@ public Broker(String name, String config) {
4750 this .config = config ;
4851 }
4952
50- public void start () {
53+ public void start () throws IOException {
54+ Process pr = null ;
5155 try {
5256 writeConfig ();
5357
5458 System .out .println ("Starting broker '" + name + "'..." );
55- String pidfile = BASE + "pid" ;
5659 ProcessBuilder pb = new ProcessBuilder (SCRIPTS + "rabbitmq-server" );
57- pb .environment ().put ("RABBITMQ_PID_FILE" , pidfile );
60+ pb .environment ().put ("RABBITMQ_PID_FILE" , pidfile () );
5861 pb .environment ().put ("RABBITMQ_LOG_BASE" , BASE + "logs" );
5962 pb .environment ().put ("RABBITMQ_MNESIA_DIR" , BASE + "db" );
6063 pb .environment ().put ("RABBITMQ_PLUGINS_EXPAND_DIR" , BASE + "plugins-expand" );
6164 pb .environment ().put ("RABBITMQ_CONFIG_FILE" , BASE + "rabbitmq" );
6265
63- pb .start ();
64- Host .executeCommand (SCRIPTS + "rabbitmqctl wait " + pidfile );
66+ pr = pb .start ();
67+
68+ Host .executeCommand (SCRIPTS + "rabbitmqctl wait " + pidfile ());
6569
6670 } catch (IOException e ) {
71+ System .out .println ("Broker start failed!" );
72+ String stdout = capture (pr .getInputStream ());
73+ String stderr = capture (pr .getErrorStream ());
74+ System .out .println (stdout );
75+ System .out .println (stderr );
6776 throw new RuntimeException (e );
6877 }
6978 }
7079
80+ private String pidfile () {
81+ return BASE + "pid" ;
82+ }
83+
7184 private void writeConfig () throws IOException {
7285 new File (BASE ).mkdirs ();
7386 FileWriter outFile = new FileWriter (BASE + "rabbitmq.config" );
@@ -79,7 +92,7 @@ private void writeConfig() throws IOException {
7992 public void stop () {
8093 System .out .println ("Stopping broker '" + name + "' ..." );
8194 try {
82- Host .executeCommand (SCRIPTS + "rabbitmqctl stop" );
95+ Host .executeCommand (SCRIPTS + "rabbitmqctl stop " + pidfile () );
8396 } catch (IOException e ) {
8497 throw new RuntimeException (e );
8598 }
@@ -88,4 +101,17 @@ public void stop() {
88101 public String getName () {
89102 return name ;
90103 }
104+
105+
106+ private static String capture (InputStream is )
107+ throws IOException
108+ {
109+ BufferedReader br = new BufferedReader (new InputStreamReader (is ));
110+ String line ;
111+ StringBuilder buff = new StringBuilder ();
112+ while ((line = br .readLine ()) != null ) {
113+ buff .append (line );
114+ }
115+ return buff .toString ();
116+ }
91117}
0 commit comments