@@ -53,6 +53,7 @@ public class BmqBrokerContainer implements BmqBroker {
5353 private static final String OUTPUT_FILENAME = "output.log" ;
5454 private static final long MAX_CONTAINER_WAIT_TIME_MS = 5000 ;
5555 private static final long CONTAINER_HEALTH_CHECK_DT_MS = 100 ;
56+ private static final String BROKER_READY_MESSAGE = "Cluster (local) is available" ;
5657
5758 private final SessionOptions sessionOptions ;
5859 private final DockerClient client ;
@@ -194,22 +195,36 @@ public void start() {
194195 totalTimeMs += CONTAINER_HEALTH_CHECK_DT_MS ) {
195196 Thread .sleep (CONTAINER_HEALTH_CHECK_DT_MS );
196197
198+ // Check if container is still running
197199 InspectContainerResponse resp = client .inspectContainerCmd (containerId ).exec ();
198200 if (!resp .getState ().getRunning ()) {
199201 logger .error (
200202 "Container '{}' is not running, status = '{}'" ,
201203 containerId ,
202204 resp .getState ().getStatus ());
203205 throw new RuntimeException (
204- String .format ("Failed to start container '{}'" , containerId ));
206+ String .format ("Failed to start container '%s'" , containerId ));
207+ }
208+
209+ // Check logs for broker readiness message
210+ if (isBrokerReady ()) {
211+ logger .info ("Container '{}' broker is ready" , containerId );
212+ break ;
213+ }
214+
215+ if (totalTimeMs + CONTAINER_HEALTH_CHECK_DT_MS >= MAX_CONTAINER_WAIT_TIME_MS ) {
216+ throw new RuntimeException (
217+ String .format (
218+ "Timeout waiting for broker ready in container '%s'" ,
219+ containerId ));
205220 }
206221 }
207222 } catch (InterruptedException e ) {
208223 Thread .currentThread ().interrupt ();
209224 return ;
210225 }
211226
212- logger .info ("Container '{}' is running " , containerId );
227+ logger .info ("Container '{}' broker is ready " , containerId );
213228
214229 // For BlazingMQ broker running in container default tier should be the 'lcl-{guest
215230 // hostname}'
@@ -357,6 +372,29 @@ public void onNext(Frame item) {
357372 }
358373 }
359374
375+ private boolean isBrokerReady () {
376+ final boolean [] ready = {false };
377+ ResultCallback .Adapter <Frame > callback =
378+ new ResultCallback .Adapter <Frame >() {
379+ @ Override
380+ public void onNext (Frame item ) {
381+ if (item .toString ().contains (BROKER_READY_MESSAGE )) {
382+ ready [0 ] = true ;
383+ }
384+ }
385+ };
386+ try {
387+ client .logContainerCmd (containerId )
388+ .withStdOut (true )
389+ .withStdErr (true )
390+ .exec (callback )
391+ .awaitCompletion ();
392+ } catch (InterruptedException e ) {
393+ Thread .currentThread ().interrupt ();
394+ }
395+ return ready [0 ];
396+ }
397+
360398 private String getHostname () {
361399 // Get container hostname
362400 logger .info ("Get '{}' container hostname" , containerName );
0 commit comments