From f06c7ebd3712ae69b8bc93ab89e16499aedd8fbf Mon Sep 17 00:00:00 2001 From: Carl Mastrangelo Date: Tue, 8 Sep 2015 10:55:08 -0700 Subject: [PATCH] Update server to await termination in the main thread --- .../io/grpc/examples/header/CustomHeaderServer.java | 10 ++++++++++ .../grpc/examples/helloworld/HelloWorldServer.java | 10 ++++++++++ .../grpc/examples/routeguide/RouteGuideServer.java | 13 +++++++++++++ 3 files changed, 33 insertions(+) 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(); } /**