Skip to content
Snippets Groups Projects
Commit 3b362437 authored by Hallvard Trætteberg's avatar Hallvard Trætteberg
Browse files

Forenklet oppstart av serveren (la til venting).

parent 5311f654
No related branches found
No related tags found
No related merge requests found
Pipeline #50566 passed
...@@ -28,7 +28,7 @@ public class FxApp extends Application { ...@@ -28,7 +28,7 @@ public class FxApp extends Application {
// json of initial data // json of initial data
serverArgs.add(args.get(1)); 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 String fxml = (baseUri != null ? "FxAppUsingRest.fxml" : "FxApp.fxml");
final FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(fxml)); final FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(fxml));
...@@ -48,7 +48,9 @@ public class FxApp extends Application { ...@@ -48,7 +48,9 @@ public class FxApp extends Application {
@Override @Override
public void stop() throws Exception { public void stop() throws Exception {
restServer.shutdown(); if (restServer != null) {
restServer.shutdown();
}
super.stop(); super.stop();
} }
......
...@@ -33,13 +33,8 @@ public class FxAppUsingRestTest extends AbstractFxAppTest { ...@@ -33,13 +33,8 @@ public class FxAppUsingRestTest extends AbstractFxAppTest {
currentServer = LatLongGrizzlyApp.startServer(new String[] { currentServer = LatLongGrizzlyApp.startServer(new String[] {
serverUrlString, serverUrlString,
"[[63.1, 11.2], [63.2, 11.0]]" "[[63.1, 11.2], [63.2, 11.0]]"
}); }, 5);
// wait for server to start } catch (final IOException e) {
int tryCount = 10;
while (tryCount-- > 0 && dataAccess.getAllLatLongs().isEmpty()) {
Thread.sleep(500);
}
} catch (final IOException | InterruptedException e) {
throw new IllegalStateException("Couldn't setup server"); throw new IllegalStateException("Couldn't setup server");
} }
} }
......
package simpleex.restserver; package simpleex.restserver;
import java.io.IOException; import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URI; import java.net.URI;
import java.net.URL;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.glassfish.grizzly.http.server.HttpServer; import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory; import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig; import org.glassfish.jersey.server.ResourceConfig;
import simpleex.restapi.LatLongsService;
public class LatLongGrizzlyApp { public class LatLongGrizzlyApp {
private static URI BASE_URI = URI.create("http://localhost:8080/"); 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 URI baseUri = (args.length >= 1 ? URI.create(args[0]) : BASE_URI);
final ResourceConfig resourceConfig = (args.length >= 2 ? new LatLongConfig(args[1]) : new LatLongConfig()); 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 { public static void stopServer(final HttpServer server) throws IOException {
...@@ -24,7 +50,7 @@ public class LatLongGrizzlyApp { ...@@ -24,7 +50,7 @@ public class LatLongGrizzlyApp {
public static void main(final String[] args) throws IOException { public static void main(final String[] args) throws IOException {
try { try {
final HttpServer server = startServer(args); final HttpServer server = startServer(args, -1);
Runtime.getRuntime().addShutdownHook(new Thread(server::shutdownNow)); Runtime.getRuntime().addShutdownHook(new Thread(server::shutdownNow));
Thread.currentThread().join(); Thread.currentThread().join();
} catch (final InterruptedException ex) { } catch (final InterruptedException ex) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment