Skip to content

Commit

Permalink
Gracefully shutdown example servers (#6512)
Browse files Browse the repository at this point in the history
  • Loading branch information
dounan authored and sanjaypujare committed Dec 14, 2019
1 parent 1f64ac9 commit 9e02cf0
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 25 deletions.
4 changes: 4 additions & 0 deletions api/src/main/java/io/grpc/Server.java
Expand Up @@ -107,6 +107,10 @@ public List<ServerServiceDefinition> getMutableServices() {
* 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
*/
Expand Down
Expand Up @@ -28,6 +28,7 @@
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;

/**
Expand Down Expand Up @@ -59,15 +60,19 @@ private void start() throws IOException {
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);
}
}

Expand Down
Expand Up @@ -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;
Expand All @@ -33,7 +34,7 @@

/**
* 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());
Expand Down Expand Up @@ -62,15 +63,19 @@ public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call,
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);
}
}

Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -49,15 +50,19 @@ private void start() throws IOException {
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);
}
}

Expand Down
Expand Up @@ -24,6 +24,7 @@
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;

/**
Expand All @@ -48,15 +49,19 @@ private void start() throws IOException {
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);
}
}

Expand Down
Expand Up @@ -29,6 +29,7 @@
import io.grpc.stub.StreamObserver;
import java.io.IOException;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

/**
Expand All @@ -53,15 +54,19 @@ private void start() throws IOException {
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);
}
}

Expand Down
Expand Up @@ -20,6 +20,7 @@
import io.grpc.ServerBuilder;
import io.grpc.stub.StreamObserver;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

/**
Expand All @@ -43,15 +44,19 @@ private void start() throws IOException {
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);
}
}

Expand Down
Expand Up @@ -23,6 +23,7 @@
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;

Expand Down Expand Up @@ -135,8 +136,13 @@ public void onCompleted() {
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();
Expand Down
Expand Up @@ -36,6 +36,7 @@
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;

Expand Down Expand Up @@ -73,16 +74,20 @@ public void start() throws IOException {
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);
}
}

Expand Down
Expand Up @@ -86,7 +86,11 @@ public void setUp() throws Exception {

@After
public void tearDown() {
server.stop();
try {
server.stop();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

@Test
Expand Down

0 comments on commit 9e02cf0

Please sign in to comment.