Allow GrpcServerFactory to return null
This commit is contained in:
@@ -35,7 +35,8 @@ public interface GrpcServerFactory {
|
||||
* Gets a new fully configured but not started {@link Server} instance. Clients should
|
||||
* not be able to connect to the returned server until {@link Server#start()} is
|
||||
* called (which happens when the {@code GrpcServerLifecycle} is started).
|
||||
* @return a fully configured not started {@link Server}
|
||||
* @return a fully configured not started {@link Server} or null if the server has no
|
||||
* lifecycle
|
||||
* @see GrpcServerLifecycle
|
||||
*/
|
||||
Server createServer();
|
||||
|
||||
@@ -114,24 +114,28 @@ public class GrpcServerLifecycle implements SmartLifecycle {
|
||||
protected void createAndStartGrpcServer() throws IOException {
|
||||
if (this.server == null) {
|
||||
final Server localServer = this.factory.createServer();
|
||||
this.server = localServer.start();
|
||||
final String address = this.server.getListenSockets().toString();
|
||||
final int port = this.server.getPort();
|
||||
logger.info("gRPC Server started, listening on address: " + this.server.getListenSockets());
|
||||
this.eventPublisher.publishEvent(new GrpcServerStartedEvent(this, localServer, address, port));
|
||||
if (localServer != null) {
|
||||
|
||||
// Prevent the JVM from shutting down while the server is running
|
||||
final Thread awaitThread = new Thread(() -> {
|
||||
try {
|
||||
localServer.awaitTermination();
|
||||
}
|
||||
catch (final InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
});
|
||||
awaitThread.setName("grpc-server-container-" + (serverCounter.incrementAndGet()));
|
||||
awaitThread.setDaemon(false);
|
||||
awaitThread.start();
|
||||
this.server = localServer.start();
|
||||
final String address = this.server.getListenSockets().toString();
|
||||
final int port = this.server.getPort();
|
||||
logger.info("gRPC Server started, listening on address: " + this.server.getListenSockets());
|
||||
this.eventPublisher.publishEvent(new GrpcServerStartedEvent(this, localServer, address, port));
|
||||
|
||||
// Prevent the JVM from shutting down while the server is running
|
||||
final Thread awaitThread = new Thread(() -> {
|
||||
try {
|
||||
localServer.awaitTermination();
|
||||
}
|
||||
catch (final InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
});
|
||||
awaitThread.setName("grpc-server-container-" + (serverCounter.incrementAndGet()));
|
||||
awaitThread.setDaemon(false);
|
||||
awaitThread.start();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user