2424import java .io .InputStreamReader ;
2525import java .util .ArrayList ;
2626import java .util .List ;
27+ import java .util .function .Consumer ;
2728
2829import org .apache .activemq .artemis .api .core .TransportConfiguration ;
2930import org .apache .activemq .artemis .api .core .client .ClientSession ;
@@ -51,7 +52,11 @@ public static Process startServer(String artemisInstance, String serverName, int
5152 }
5253
5354 public static Process startServer (String artemisInstance , String serverName , int id , int timeout , File brokerProperties ) throws Exception {
54- final Process process = internalStartServer (artemisInstance , serverName , brokerProperties );
55+ return startServer (artemisInstance , serverName , id , timeout , brokerProperties , null );
56+ }
57+
58+ public static Process startServer (String artemisInstance , String serverName , int id , int timeout , File brokerProperties , Consumer <String > logCallback ) throws Exception {
59+ final Process process = internalStartServer (artemisInstance , serverName , brokerProperties , logCallback );
5560
5661 // wait for start
5762 if (timeout > 0 ) {
@@ -66,7 +71,11 @@ public static Process startServer(String artemisInstance, String serverName, Str
6671 }
6772
6873 public static Process startServer (String artemisInstance , String serverName , String uri , int timeout , File propertiesFile ) throws Exception {
69- final Process process = internalStartServer (artemisInstance , serverName , propertiesFile );
74+ return startServer (artemisInstance , serverName , uri , timeout , propertiesFile , null );
75+ }
76+
77+ public static Process startServer (String artemisInstance , String serverName , String uri , int timeout , File propertiesFile , Consumer <String > logCallback ) throws Exception {
78+ final Process process = internalStartServer (artemisInstance , serverName , propertiesFile , logCallback );
7079
7180 // wait for start
7281 if (timeout != 0 ) {
@@ -78,20 +87,30 @@ public static Process startServer(String artemisInstance, String serverName, Str
7887
7988 private static Process internalStartServer (String artemisInstance ,
8089 String serverName ) throws IOException , ClassNotFoundException {
81- return internalStartServer (artemisInstance , serverName , null );
90+ return internalStartServer (artemisInstance , serverName , null , null );
8291 }
8392 private static Process internalStartServer (String artemisInstance ,
8493 String serverName ,
8594 File propertiesFile ) throws IOException , ClassNotFoundException {
95+ return internalStartServer (artemisInstance , serverName , propertiesFile , null );
96+ }
97+ private static Process internalStartServer (String artemisInstance ,
98+ String serverName ,
99+ File propertiesFile ,
100+ Consumer <String > logCallback ) throws IOException , ClassNotFoundException {
86101
87102 if (propertiesFile != null ) {
88- return execute (artemisInstance , serverName , "run" , "--properties" , propertiesFile .getAbsolutePath ());
103+ return execute (artemisInstance , serverName , logCallback , "run" , "--properties" , propertiesFile .getAbsolutePath ());
89104 } else {
90- return execute (artemisInstance , serverName , "run" );
105+ return execute (artemisInstance , serverName , logCallback , "run" );
91106 }
92107 }
93108
94109 public static Process execute (String artemisInstance , String jobName , String ...args ) throws IOException , ClassNotFoundException {
110+ return execute (artemisInstance , jobName , null , args );
111+ }
112+
113+ public static Process execute (String artemisInstance , String jobName , Consumer <String > logCallback , String ...args ) throws IOException , ClassNotFoundException {
95114 try {
96115 boolean IS_WINDOWS = System .getProperty ("os.name" ).toLowerCase ().trim ().startsWith ("win" );
97116
@@ -117,11 +136,11 @@ public static Process execute(String artemisInstance, String jobName, String...a
117136 final Process process = builder .start ();
118137 Runtime .getRuntime ().addShutdownHook (new Thread (() -> process .destroy ()));
119138
120- ProcessLogger outputLogger = new ProcessLogger (true , process .getInputStream (), jobName , false );
139+ ProcessLogger outputLogger = new ProcessLogger (logCallback == null , process .getInputStream (), jobName , false , logCallback );
121140 outputLogger .start ();
122141
123142 // Adding a reader to System.err, so the VM won't hang on a System.err.println
124- ProcessLogger errorLogger = new ProcessLogger (true , process .getErrorStream (), jobName , true );
143+ ProcessLogger errorLogger = new ProcessLogger (logCallback == null , process .getErrorStream (), jobName , true , logCallback );
125144 errorLogger .start ();
126145 return process ;
127146 } catch (IOException e ) {
@@ -215,14 +234,18 @@ static class ProcessLogger extends Thread {
215234
216235 private final boolean sendToErr ;
217236
237+ private final Consumer <String > logCallback ;
238+
218239 ProcessLogger (final boolean print ,
219240 final InputStream is ,
220241 final String logName ,
221- final boolean sendToErr ) throws ClassNotFoundException {
242+ final boolean sendToErr ,
243+ final Consumer <String > logCallback ) throws ClassNotFoundException {
222244 this .is = is ;
223245 this .print = print ;
224246 this .logName = logName ;
225247 this .sendToErr = sendToErr ;
248+ this .logCallback = logCallback ;
226249 setDaemon (false );
227250 }
228251
@@ -240,6 +263,9 @@ public void run() {
240263 System .out .println (logName + "-out:" + line );
241264 }
242265 }
266+ if (logCallback != null ) {
267+ logCallback .accept ((sendToErr ? logName + "-err:" : logName + "-out:" ) + line );
268+ }
243269 }
244270 } catch (IOException e ) {
245271 // ok, stream closed
0 commit comments