Gracefully shutdown example servers (#6512)

This commit is contained in:
Dounan Shi 2019-12-13 16:19:45 -08:00 committed by sanjaypujare
parent 1f64ac94a8
commit 9e02cf089e
10 changed files with 74 additions and 25 deletions

View File

@ -107,6 +107,10 @@ public abstract class Server {
* After this call returns, this server has released the listening socket(s) and may be reused by
* another server.
*
* <p>Note that this method will not wait for preexisting calls to finish before returning.
* {@link #awaitTermination()} or {@link #awaitTermination(long, TimeUnit)} needs to be called to
* wait for existing calls to finish.
*
* @return {@code this} object
* @since 1.0.0
*/

View File

@ -28,6 +28,7 @@ import io.grpc.examples.helloworld.HelloRequest;
import io.grpc.stub.ServerCalls.UnaryMethod;
import io.grpc.stub.StreamObserver;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
/**
@ -59,15 +60,19 @@ public class HelloJsonServer {
public void run() {
// Use stderr here since the logger may have been reset by its JVM shutdown hook.
System.err.println("*** shutting down gRPC server since JVM is shutting down");
HelloJsonServer.this.stop();
try {
HelloJsonServer.this.stop();
} catch (InterruptedException e) {
e.printStackTrace(System.err);
}
System.err.println("*** server shut down");
}
});
}
private void stop() {
private void stop() throws InterruptedException {
if (server != null) {
server.shutdown();
server.shutdown().awaitTermination(30, TimeUnit.SECONDS);
}
}

View File

@ -17,6 +17,7 @@
package io.grpc.examples.experimental;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import io.grpc.Metadata;
@ -33,7 +34,7 @@ import io.grpc.stub.StreamObserver;
/**
* Server that manages startup/shutdown of a {@code Greeter} server
* with an interceptor to enable compression for all responses.
* with an interceptor to enable compression for all responses.
*/
public class CompressingHelloWorldServerAllMethods {
private static final Logger logger = Logger.getLogger(CompressingHelloWorldServerAllMethods.class.getName());
@ -62,15 +63,19 @@ public class CompressingHelloWorldServerAllMethods {
public void run() {
// Use stderr here since the logger may have been reset by its JVM shutdown hook.
System.err.println("*** shutting down gRPC server since JVM is shutting down");
CompressingHelloWorldServerAllMethods.this.stop();
try {
CompressingHelloWorldServerAllMethods.this.stop();
} catch (InterruptedException e) {
e.printStackTrace(System.err);
}
System.err.println("*** server shut down");
}
});
}
private void stop() {
private void stop() throws InterruptedException {
if (server != null) {
server.shutdown();
server.shutdown().awaitTermination(30, TimeUnit.SECONDS);
}
}

View File

@ -17,6 +17,7 @@
package io.grpc.examples.experimental;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import io.grpc.Server;
@ -49,15 +50,19 @@ public class CompressingHelloWorldServerPerMethod {
public void run() {
// Use stderr here since the logger may have been reset by its JVM shutdown hook.
System.err.println("*** shutting down gRPC server since JVM is shutting down");
CompressingHelloWorldServerPerMethod.this.stop();
try {
CompressingHelloWorldServerPerMethod.this.stop();
} catch (InterruptedException e) {
e.printStackTrace(System.err);
}
System.err.println("*** server shut down");
}
});
}
private void stop() {
private void stop() throws InterruptedException {
if (server != null) {
server.shutdown();
server.shutdown().awaitTermination(30, TimeUnit.SECONDS);
}
}

View File

@ -24,6 +24,7 @@ import io.grpc.examples.helloworld.HelloReply;
import io.grpc.examples.helloworld.HelloRequest;
import io.grpc.stub.StreamObserver;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
/**
@ -48,15 +49,19 @@ public class CustomHeaderServer {
public void run() {
// Use stderr here since the logger may have been reset by its JVM shutdown hook.
System.err.println("*** shutting down gRPC server since JVM is shutting down");
CustomHeaderServer.this.stop();
try {
CustomHeaderServer.this.stop();
} catch (InterruptedException e) {
e.printStackTrace(System.err);
}
System.err.println("*** server shut down");
}
});
}
private void stop() {
private void stop() throws InterruptedException {
if (server != null) {
server.shutdown();
server.shutdown().awaitTermination(30, TimeUnit.SECONDS);
}
}

View File

@ -29,6 +29,7 @@ import io.grpc.examples.helloworld.HelloRequest;
import io.grpc.stub.StreamObserver;
import java.io.IOException;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
/**
@ -53,15 +54,19 @@ public class HedgingHelloWorldServer {
public void run() {
// Use stderr here since the logger may have been reset by its JVM shutdown hook.
System.err.println("*** shutting down gRPC server since JVM is shutting down");
HedgingHelloWorldServer.this.stop();
try {
HedgingHelloWorldServer.this.stop();
} catch (InterruptedException e) {
e.printStackTrace(System.err);
}
System.err.println("*** server shut down");
}
});
}
private void stop() {
private void stop() throws InterruptedException {
if (server != null) {
server.shutdown();
server.shutdown().awaitTermination(30, TimeUnit.SECONDS);
}
}

View File

@ -20,6 +20,7 @@ import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.stub.StreamObserver;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
/**
@ -43,15 +44,19 @@ public class HelloWorldServer {
public void run() {
// Use stderr here since the logger may have been reset by its JVM shutdown hook.
System.err.println("*** shutting down gRPC server since JVM is shutting down");
HelloWorldServer.this.stop();
try {
HelloWorldServer.this.stop();
} catch (InterruptedException e) {
e.printStackTrace(System.err);
}
System.err.println("*** server shut down");
}
});
}
private void stop() {
private void stop() throws InterruptedException {
if (server != null) {
server.shutdown();
server.shutdown().awaitTermination(30, TimeUnit.SECONDS);
}
}

View File

@ -23,6 +23,7 @@ import io.grpc.stub.ServerCallStreamObserver;
import io.grpc.stub.StreamObserver;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Logger;
@ -135,8 +136,13 @@ public class ManualFlowControlServer {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
logger.info("Shutting down");
server.shutdown();
// Use stderr here since the logger may have been reset by its JVM shutdown hook.
System.err.println("Shutting down");
try {
server.shutdown().awaitTermination(30, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace(System.err);
}
}
});
server.awaitTermination();

View File

@ -36,6 +36,7 @@ import java.util.Collections;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -73,16 +74,20 @@ public class RouteGuideServer {
public void run() {
// Use stderr here since the logger may have been reset by its JVM shutdown hook.
System.err.println("*** shutting down gRPC server since JVM is shutting down");
RouteGuideServer.this.stop();
try {
RouteGuideServer.this.stop();
} catch (InterruptedException e) {
e.printStackTrace(System.err);
}
System.err.println("*** server shut down");
}
});
}
/** Stop serving requests and shutdown resources. */
public void stop() {
public void stop() throws InterruptedException {
if (server != null) {
server.shutdown();
server.shutdown().awaitTermination(30, TimeUnit.SECONDS);
}
}

View File

@ -86,7 +86,11 @@ public class RouteGuideServerTest {
@After
public void tearDown() {
server.stop();
try {
server.stop();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Test