io.grpc.Server#isTerminated ( )源码实例Demo

下面列出了io.grpc.Server#isTerminated ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: genie   文件: IntrospectionAutoConfiguration.java
/**
 * Provide a {@link GenieWebRpcInfo} bean if one hasn't already been defined.
 *
 * @param server The gRPC {@link Server} instance. Must not be {@link Server#isShutdown()} or
 *               {@link Server#isTerminated()}. Must be able to get the port the server is listening on.
 * @return A {@link GenieWebRpcInfo} instance
 * @throws IllegalStateException When an instance can't be created
 */
@Bean
@ConditionalOnMissingBean(
    {
        GenieWebRpcInfo.class
    }
)
public GenieWebRpcInfo genieWebRpcInfo(final Server server) throws IllegalStateException {
    if (server.isShutdown() || server.isTerminated()) {
        throw new IllegalStateException("gRPC server is already shut down. Can't start.");
    } else {
        final int port = GRpcServerUtils.startServer(server);
        if (port < 1) {
            throw new IllegalStateException("gRPC server started on illegal port: " + port);
        }
        return new GenieWebRpcInfo(port);
    }
}
 
@Produces
@ApplicationScoped
public Lifecycle storageGrpcServer(
    HostInfo storageLocalHost,
    KeyValueStoreGrpc.KeyValueStoreImplBase storageStoreGrpcImpl,
    AsyncBiFunctionServiceGrpc.AsyncBiFunctionServiceImplBase storageAsyncBiFunctionServiceGrpcImpl
) {

    UnknownStatusDescriptionInterceptor unknownStatusDescriptionInterceptor =
        new UnknownStatusDescriptionInterceptor(
            ImmutableMap.of(
                IllegalArgumentException.class, Status.INVALID_ARGUMENT,
                IllegalStateException.class, Status.FAILED_PRECONDITION,
                InvalidStateStoreException.class, Status.FAILED_PRECONDITION,
                Throwable.class, Status.INTERNAL
            )
        );

    Server server = ServerBuilder
        .forPort(storageLocalHost.port())
        .addService(
            ServerInterceptors.intercept(
                storageStoreGrpcImpl,
                unknownStatusDescriptionInterceptor
            )
        )
        .addService(
            ServerInterceptors.intercept(
                storageAsyncBiFunctionServiceGrpcImpl,
                unknownStatusDescriptionInterceptor
            )
        )
        .build();

    return new Lifecycle() {
        @Override
        public void start() {
            try {
                server.start();
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }

        @Override
        public void stop() {
            ConcurrentUtil
                .<Server>consumer(Server::awaitTermination)
                .accept(server.shutdown());
        }

        @Override
        public boolean isRunning() {
            return !(server.isShutdown() || server.isTerminated());
        }
    };
}