diff --git a/spring-grpc-core/src/main/java/org/springframework/grpc/server/GrpcServerFactory.java b/spring-grpc-core/src/main/java/org/springframework/grpc/server/GrpcServerFactory.java index ea73486..24e971a 100644 --- a/spring-grpc-core/src/main/java/org/springframework/grpc/server/GrpcServerFactory.java +++ b/spring-grpc-core/src/main/java/org/springframework/grpc/server/GrpcServerFactory.java @@ -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(); diff --git a/spring-grpc-core/src/main/java/org/springframework/grpc/server/lifecycle/GrpcServerLifecycle.java b/spring-grpc-core/src/main/java/org/springframework/grpc/server/lifecycle/GrpcServerLifecycle.java index 1278c2a..47a3be5 100644 --- a/spring-grpc-core/src/main/java/org/springframework/grpc/server/lifecycle/GrpcServerLifecycle.java +++ b/spring-grpc-core/src/main/java/org/springframework/grpc/server/lifecycle/GrpcServerLifecycle.java @@ -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(); + + } } }