diff --git a/examples/src/main/java/io/grpc/examples/header/CustomHeaderServer.java b/examples/src/main/java/io/grpc/examples/header/CustomHeaderServer.java index 26a53411afa..b56f490efde 100644 --- a/examples/src/main/java/io/grpc/examples/header/CustomHeaderServer.java +++ b/examples/src/main/java/io/grpc/examples/header/CustomHeaderServer.java @@ -74,12 +74,22 @@ private void stop() { } } + /** + * Await termination on the main thread since the grpc library uses daemon threads. + */ + private void blockUntilShutdown() throws InterruptedException { + if (server != null) { + server.awaitTermination(); + } + } + /** * Main launches the server from the command line. */ public static void main(String[] args) throws Exception { final CustomHeaderServer server = new CustomHeaderServer(); server.start(); + server.blockUntilShutdown(); } private class GreeterImpl implements GreeterGrpc.Greeter { diff --git a/examples/src/main/java/io/grpc/examples/helloworld/HelloWorldServer.java b/examples/src/main/java/io/grpc/examples/helloworld/HelloWorldServer.java index 78eb8e6f6ff..a7a1a06b9ee 100644 --- a/examples/src/main/java/io/grpc/examples/helloworld/HelloWorldServer.java +++ b/examples/src/main/java/io/grpc/examples/helloworld/HelloWorldServer.java @@ -69,12 +69,22 @@ private void stop() { } } + /** + * Await termination on the main thread since the grpc library uses daemon threads. + */ + private void blockUntilShutdown() throws InterruptedException { + if (server != null) { + server.awaitTermination(); + } + } + /** * Main launches the server from the command line. */ public static void main(String[] args) throws Exception { final HelloWorldServer server = new HelloWorldServer(); server.start(); + server.blockUntilShutdown(); } private class GreeterImpl implements GreeterGrpc.Greeter { diff --git a/examples/src/main/java/io/grpc/examples/routeguide/RouteGuideServer.java b/examples/src/main/java/io/grpc/examples/routeguide/RouteGuideServer.java index 02ee4d218b6..021a843c928 100644 --- a/examples/src/main/java/io/grpc/examples/routeguide/RouteGuideServer.java +++ b/examples/src/main/java/io/grpc/examples/routeguide/RouteGuideServer.java @@ -103,9 +103,22 @@ public void stop() { } } + /** + * Await termination on the main thread since the grpc library uses daemon threads. + */ + private void blockUntilShutdown() throws InterruptedException { + if (grpcServer != null) { + grpcServer.awaitTermination(); + } + } + + /** + * Main method. This comment makes the linter happy. + */ public static void main(String[] args) throws Exception { RouteGuideServer server = new RouteGuideServer(8980); server.start(); + server.blockUntilShutdown(); } /**