Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Examples await #2931

Merged
merged 2 commits into from
Apr 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions examples/cors/src/main/java/io/helidon/examples/cors/Main.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates.
* Copyright (c) 2020, 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,6 +20,7 @@
import java.util.logging.Logger;

import io.helidon.common.LogConfig;
import io.helidon.common.reactive.Single;
import io.helidon.config.Config;
import io.helidon.health.HealthSupport;
import io.helidon.health.checks.HealthChecks;
Expand Down Expand Up @@ -55,7 +56,7 @@ public static void main(final String[] args) throws IOException {
* @return the created {@link WebServer} instance
* @throws IOException if there are problems reading logging properties
*/
static WebServer startServer() throws IOException {
static Single<WebServer> startServer() throws IOException {

// load logging configuration
LogConfig.configureRuntime();
Expand All @@ -64,15 +65,13 @@ static WebServer startServer() throws IOException {
Config config = Config.create();

// Get webserver config from the "server" section of application.yaml
WebServer server = WebServer.builder(createRouting(config))
Single<WebServer> server = WebServer.builder(createRouting(config))
.config(config.get("server"))
.addMediaSupport(JsonpSupport.create())
.build();
.build()
.start();

// Try to start the server. If successful, print some info and arrange to
// print a message at shutdown. If unsuccessful, print the exception.
server.start()
.thenAccept(ws -> {
server.thenAccept(ws -> {
System.out.println(
"WEB server is up! http://localhost:" + ws.port() + "/greet");
ws.whenShutdown().thenRun(()
Expand All @@ -84,8 +83,6 @@ static WebServer startServer() throws IOException {
return null;
});

// Server threads are not daemon. No need to block. Just react.

return server;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates.
* Copyright (c) 2020, 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -55,11 +55,7 @@ public class MainTest {
public static void start() throws Exception {
// the port is only available if the server started already!
// so we need to wait
webServer = Main.startServer()
.start()
.toCompletableFuture()
.get();

webServer = Main.startServer().await();

webClient = WebClient.builder()
.baseUri("http://localhost:" + webServer.port())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.helidon.service.employee;

import io.helidon.common.LogConfig;
import io.helidon.common.reactive.Single;
import io.helidon.config.Config;
import io.helidon.health.HealthSupport;
import io.helidon.health.checks.HealthChecks;
Expand Down Expand Up @@ -49,7 +50,7 @@ public static void main(final String[] args) {
* Start the server.
* @return the created {@link WebServer} instance
*/
static WebServer startServer() {
static Single<WebServer> startServer() {

// load logging configuration
LogConfig.configureRuntime();
Expand All @@ -58,14 +59,13 @@ static WebServer startServer() {
Config config = Config.create();

// Get webserver config from the "server" section of application.yaml and JSON support registration
WebServer server = WebServer.builder(createRouting(config))
Single<WebServer> server = WebServer.builder(createRouting(config))
.config(config.get("server"))
.addMediaSupport(JsonbSupport.create())
.build();
.build()
.start();

// Try to start the server. If successful, print some info and arrange to
// print a message at shutdown. If unsuccessful, print the exception.
server.start().thenAccept(ws -> {
server.thenAccept(ws -> {
System.out.println("WEB server is up!");
System.out.println("Web client at: http://localhost:" + ws.port()
+ "/public/index.html");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2020 Oracle and/or its affiliates.
* Copyright (c) 2019, 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,6 +20,7 @@

import io.helidon.common.http.Http;
import io.helidon.common.http.MediaType;
import io.helidon.common.reactive.Single;
import io.helidon.webclient.WebClient;
import io.helidon.webserver.WebServer;

Expand All @@ -35,17 +36,7 @@ public class MainTest {

@BeforeAll
public static void startTheServer() throws Exception {
webServer = Main.startServer();

long timeout = 2000; // 2 seconds should be enough to start the server
long now = System.currentTimeMillis();

while (!webServer.isRunning()) {
Thread.sleep(100);
if ((System.currentTimeMillis() - now) > timeout) {
Assertions.fail("Failed to start webserver");
}
}
webServer = Main.startServer().await();

webClient = WebClient.builder()
.baseUri("http://localhost:" + webServer.port())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.logging.LogManager;

import io.helidon.common.LogConfig;
import io.helidon.common.reactive.Single;
import io.helidon.config.Config;
import io.helidon.examples.integrations.neo4j.se.domain.MovieRepository;
import io.helidon.health.HealthSupport;
Expand Down Expand Up @@ -61,23 +62,21 @@ public static void main(final String[] args) throws IOException {
* @return the created WebServer instance
* @throws IOException if there are problems reading logging properties
*/
public static WebServer startServer() throws IOException {
public static Single<WebServer> startServer() throws IOException {
// load logging configuration
LogConfig.configureRuntime();

// By default this will pick up application.yaml from the classpath
Config config = Config.create();

WebServer server = WebServer.builder(createRouting(config))
Single<WebServer> server = WebServer.builder(createRouting(config))
.config(config.get("server"))
.addMediaSupport(JsonpSupport.create())
.addMediaSupport(JsonbSupport.create())
.build();
.build()
.start();

// Try to start the server. If successful, print some info and arrange to
// print a message at shutdown. If unsuccessful, print the exception.
server.start()
.thenAccept(ws -> {
server.thenAccept(ws -> {
System.out.println(
"WEB server is up! http://localhost:" + ws.port() + "/api/movies");
ws.whenShutdown().thenRun(()
Expand All @@ -89,8 +88,6 @@ public static WebServer startServer() throws IOException {
return null;
});

// Server threads are not daemon. No need to block. Just react.

return server;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,7 @@ private static void startTheServer() throws Exception {

System.setProperty("neo4j.uri", embeddedDatabaseServer.boltURI().toString());

webServer = Main.startServer();

long timeout = 2000; // 2 seconds should be enough to start the server
long now = System.currentTimeMillis();

while (!webServer.isRunning()) {
Thread.sleep(100);
if ((System.currentTimeMillis() - now) > timeout) {
Assertions.fail("Failed to start webserver");
}
}
webServer = Main.startServer().await();

webClient = WebClient.builder()
.baseUri("http://localhost:" + webServer.port())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2020 Oracle and/or its affiliates.
* Copyright (c) 2019, 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,7 @@
package io.helidon.examples.openapi;

import io.helidon.common.LogConfig;
import io.helidon.common.reactive.Single;
import io.helidon.config.Config;
import io.helidon.health.HealthSupport;
import io.helidon.health.checks.HealthChecks;
Expand Down Expand Up @@ -49,7 +50,7 @@ public static void main(final String[] args) {
* Start the server.
* @return the created {@link WebServer} instance
*/
static WebServer startServer() {
static Single<WebServer> startServer() {

// load logging configuration
LogConfig.configureRuntime();
Expand All @@ -58,15 +59,13 @@ static WebServer startServer() {
Config config = Config.create();

// Get webserver config from the "server" section of application.yaml and register JSON support
WebServer server = WebServer.builder(createRouting(config))
Single<WebServer> server = WebServer.builder(createRouting(config))
.config(config.get("server"))
.addMediaSupport(JsonpSupport.create())
.build();
.build()
.start();

// Try to start the server. If successful, print some info and arrange to
// print a message at shutdown. If unsuccessful, print the exception.
server.start()
.thenAccept(ws -> {
server.thenAccept(ws -> {
System.out.println(
"WEB server is up! http://localhost:" + ws.port() + "/greet");
ws.whenShutdown().thenRun(()
Expand All @@ -77,9 +76,6 @@ static WebServer startServer() {
t.printStackTrace(System.err);
return null;
});

// Server threads are not daemon. No need to block. Just react.

return server;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2020 Oracle and/or its affiliates.
* Copyright (c) 2018, 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -52,17 +52,7 @@ public class MainTest {

@BeforeAll
public static void startTheServer() throws Exception {
webServer = Main.startServer();

long timeout = 2000; // 2 seconds should be enough to start the server
long now = System.currentTimeMillis();

while (!webServer.isRunning()) {
Thread.sleep(100);
if ((System.currentTimeMillis() - now) > timeout) {
Assertions.fail("Failed to start webserver");
}
}
webServer = Main.startServer().await();

webClient = WebClient.builder()
.baseUri("http://localhost:" + webServer.port())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates.
* Copyright (c) 2020, 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,6 +18,7 @@
import io.helidon.common.configurable.Resource;
import io.helidon.common.http.Http;
import io.helidon.common.pki.KeyConfig;
import io.helidon.common.reactive.Single;
import io.helidon.webserver.ClientAuthentication;
import io.helidon.webserver.Routing;
import io.helidon.webserver.SocketConfiguration;
Expand Down Expand Up @@ -49,20 +50,20 @@ public static void main(String[] args) {
startServer(8080, 443);
}

static WebServer startServer(int unsecured, int secured) {
static Single<WebServer> startServer(int unsecured, int secured) {
SocketConfiguration socketConf = SocketConfiguration.builder()
.name("secured")
.port(secured)
.tls(tlsConfig())
.build();
WebServer webServer = WebServer.builder()
Single<WebServer> webServer = WebServer.builder()
.port(unsecured)
.routing(createPlainRouting())
.addSocket(socketConf, createMtlsRouting())
.build();
.build()
.start();

webServer.start()
.thenAccept(ws -> {
webServer.thenAccept(ws -> {
System.out.println("WebServer is up!");
System.out.println("Unsecured: http://localhost:" + ws.port() + "/");
System.out.println("Secured: https://localhost:" + ws.port("secured") + "/");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates.
* Copyright (c) 2020, 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,6 +16,7 @@
package io.helidon.webserver.examples.mtls;

import io.helidon.common.http.Http;
import io.helidon.common.reactive.Single;
import io.helidon.config.Config;
import io.helidon.webserver.Routing;
import io.helidon.webserver.WebServer;
Expand Down Expand Up @@ -45,13 +46,14 @@ public static void main(String[] args) {
startServer(config.get("server"));
}

static WebServer startServer(Config config) {
WebServer webServer = WebServer.builder(createPlainRouting())
static Single<WebServer> startServer(Config config) {
Single<WebServer> webServer = WebServer.builder(createPlainRouting())
.config(config)
.addNamedRouting("secured", createMtlsRouting())
.build();
webServer.start()
.thenAccept(ws -> {
.build()
.start();

webServer.thenAccept(ws -> {
System.out.println("WebServer is up!");
System.out.println("Unsecured: http://localhost:" + ws.port() + "/");
System.out.println("Secured: https://localhost:" + ws.port("secured") + "/");
Expand Down
Loading