Allow GrpcServerFactory to return null

This commit is contained in:
Dave Syer
2024-11-19 08:41:13 +00:00
parent 8eb623e82d
commit 0dc3249251
2 changed files with 23 additions and 18 deletions

View File

@@ -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();

View File

@@ -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();
}
}
}