Skip to content
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
4 changes: 4 additions & 0 deletions api/src/main/java/io/grpc/Server.java
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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