diff --git a/simpleexample2/fxui/src/main/java/simpleex/ui/FxApp.java b/simpleexample2/fxui/src/main/java/simpleex/ui/FxApp.java index aa0b92aa23cc31f27649bc7a4a06b279518eb036..203d8dcb955d43744bba1ab26876b9799626c571 100644 --- a/simpleexample2/fxui/src/main/java/simpleex/ui/FxApp.java +++ b/simpleexample2/fxui/src/main/java/simpleex/ui/FxApp.java @@ -28,7 +28,7 @@ public class FxApp extends Application { // json of initial data serverArgs.add(args.get(1)); } - restServer = LatLongGrizzlyApp.startServer(serverArgs.toArray(new String[serverArgs.size()])); + restServer = LatLongGrizzlyApp.startServer(serverArgs.toArray(new String[serverArgs.size()]), 5); } final String fxml = (baseUri != null ? "FxAppUsingRest.fxml" : "FxApp.fxml"); final FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(fxml)); @@ -48,7 +48,9 @@ public class FxApp extends Application { @Override public void stop() throws Exception { - restServer.shutdown(); + if (restServer != null) { + restServer.shutdown(); + } super.stop(); } diff --git a/simpleexample2/fxui/src/test/java/simpleex/ui/FxAppUsingRestTest.java b/simpleexample2/fxui/src/test/java/simpleex/ui/FxAppUsingRestTest.java index 893452e30a1532ef37dfe870f9f781dfd46c9c0c..4517690b133ec6e6efe677416cf325372da02e21 100644 --- a/simpleexample2/fxui/src/test/java/simpleex/ui/FxAppUsingRestTest.java +++ b/simpleexample2/fxui/src/test/java/simpleex/ui/FxAppUsingRestTest.java @@ -33,13 +33,8 @@ public class FxAppUsingRestTest extends AbstractFxAppTest { currentServer = LatLongGrizzlyApp.startServer(new String[] { serverUrlString, "[[63.1, 11.2], [63.2, 11.0]]" - }); - // wait for server to start - int tryCount = 10; - while (tryCount-- > 0 && dataAccess.getAllLatLongs().isEmpty()) { - Thread.sleep(500); - } - } catch (final IOException | InterruptedException e) { + }, 5); + } catch (final IOException e) { throw new IllegalStateException("Couldn't setup server"); } } diff --git a/simpleexample2/restserver/src/main/java/simpleex/restserver/LatLongGrizzlyApp.java b/simpleexample2/restserver/src/main/java/simpleex/restserver/LatLongGrizzlyApp.java index e6c14a237f02728e29525de6461505a9ef8adb66..dbd2be35db8d01dca9efd9393013fa76555c6d46 100644 --- a/simpleexample2/restserver/src/main/java/simpleex/restserver/LatLongGrizzlyApp.java +++ b/simpleexample2/restserver/src/main/java/simpleex/restserver/LatLongGrizzlyApp.java @@ -1,21 +1,47 @@ package simpleex.restserver; import java.io.IOException; +import java.net.HttpURLConnection; import java.net.URI; +import java.net.URL; import java.util.logging.Level; import java.util.logging.Logger; import org.glassfish.grizzly.http.server.HttpServer; import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory; import org.glassfish.jersey.server.ResourceConfig; +import simpleex.restapi.LatLongsService; public class LatLongGrizzlyApp { private static URI BASE_URI = URI.create("http://localhost:8080/"); - public static HttpServer startServer(final String[] args) throws IOException { + public static HttpServer startServer(final String[] args, int waitSecondsForServer) throws IOException { final URI baseUri = (args.length >= 1 ? URI.create(args[0]) : BASE_URI); final ResourceConfig resourceConfig = (args.length >= 2 ? new LatLongConfig(args[1]) : new LatLongConfig()); - return GrizzlyHttpServerFactory.createHttpServer(baseUri, resourceConfig); + final HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(baseUri, resourceConfig); + if (waitSecondsForServer < 0) { + return httpServer; + } + while (waitSecondsForServer > 0) { + try { + final URL clientUrl = new URL(baseUri + LatLongsService.LAT_LONG_SERVICE_PATH); + final HttpURLConnection connection = (HttpURLConnection) clientUrl.openConnection(); + final int responseCode = connection.getResponseCode(); + System.out.println("Trying " + clientUrl + ": " + responseCode); + connection.disconnect(); + if (responseCode == 200) { + return httpServer; + } + } catch (final Exception e) { + } + try { + Thread.sleep(1000); + waitSecondsForServer -= 1; + } catch (final InterruptedException e) { + return null; + } + } + return null; } public static void stopServer(final HttpServer server) throws IOException { @@ -24,7 +50,7 @@ public class LatLongGrizzlyApp { public static void main(final String[] args) throws IOException { try { - final HttpServer server = startServer(args); + final HttpServer server = startServer(args, -1); Runtime.getRuntime().addShutdownHook(new Thread(server::shutdownNow)); Thread.currentThread().join(); } catch (final InterruptedException ex) {