Add ServerImpl#awaitTerminated() method. Fixes #80

This commit is contained in:
Jakob Buchgraber 2015-02-18 15:45:33 -08:00
parent 246e8b52bb
commit 07d0917b94
2 changed files with 28 additions and 0 deletions

View File

@ -200,6 +200,15 @@ public class ServerImpl implements Server {
return terminated;
}
/**
* Waits for the server to become terminated.
*/
public synchronized void awaitTerminated() throws InterruptedException {
while(!terminated) {
wait();
}
}
/**
* Returns whether the server is terminated. Terminated servers have no running calls and
* relevant resources released (like TCP connections).

View File

@ -40,6 +40,7 @@ import io.grpc.SharedResourceHolder;
import io.grpc.transport.ServerListener;
import io.netty.channel.EventLoopGroup;
import io.netty.handler.ssl.SslContext;
import io.grpc.ServerImpl;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
@ -88,6 +89,15 @@ public final class NettyServerBuilder extends AbstractServerBuilder<NettyServerB
*
* <p>The server won't take ownership of the given EventLoopGroup. It's caller's responsibility
* to shut it down when it's desired.
*
* <p>Grpc uses non-daemon {@link Thread}s by default and thus a {@link ServerImpl} will
* continue to run even after the main thread has terminated. However, users have to be cautious
* when providing their own {@link EventLoopGroup}s.
* For example, Netty's {@link EventLoopGroup}s use daemon threads by default
* and thus an application with only daemon threads running besides the main thread will exit as
* soon as the main thread completes.
* A simple solution to this problem is to call {@link ServerImpl#awaitTerminated()} to
* keep the main thread alive until the server has terminated.
*/
public NettyServerBuilder userBossEventLoopGroup(EventLoopGroup group) {
this.userBossEventLoopGroup = group;
@ -102,6 +112,15 @@ public final class NettyServerBuilder extends AbstractServerBuilder<NettyServerB
*
* <p>The server won't take ownership of the given EventLoopGroup. It's caller's responsibility
* to shut it down when it's desired.
*
* <p>Grpc uses non-daemon {@link Thread}s by default and thus a {@link ServerImpl} will
* continue to run even after the main thread has terminated. However, users have to be cautious
* when providing their own {@link EventLoopGroup}s.
* For example, Netty's {@link EventLoopGroup}s use daemon threads by default
* and thus an application with only daemon threads running besides the main thread will exit as
* soon as the main thread completes.
* A simple solution to this problem is to call {@link ServerImpl#awaitTerminated()} to
* keep the main thread alive until the server has terminated.
*/
public NettyServerBuilder workerEventLoopGroup(EventLoopGroup group) {
this.userWorkerEventLoopGroup = group;