Search in sources :

Example 1 with DefaultThreadFactory

use of io.servicetalk.concurrent.api.DefaultThreadFactory in project servicetalk by apple.

the class ExecutionStrategyServer method main.

public static void main(String... args) throws Exception {
    int port = 8080;
    try (CompositeCloseable closeEverything = AsyncCloseables.newCompositeCloseable()) {
        Executor executor = Executors.newCachedThreadExecutor(new DefaultThreadFactory("custom"));
        // executor will be closed last, servers are prepended before executor.
        closeEverything.append(executor);
        // Default server configuration.
        // -> route offloaded to global executor
        System.out.printf("\n%d : default server\n", port);
        ServerContext defaultServer = GrpcServers.forPort(port++).listenAndAwait((GreeterService) (ctx, request) -> getReplySingle(request, "default server"));
        closeEverything.prepend(defaultServer);
        // No offloads strategy specified on the server, async route does not override its strategy.
        // -> no offloading, route executed on IoExecutor
        System.out.printf("\n%d : no offloading server, async route\n", port);
        ServerContext asyncServer = GrpcServers.forPort(port++).initializeHttp(init -> init.executionStrategy(offloadNever())).listenAndAwait((GreeterService) (ctx, request) -> getReplySingle(request, "no offloading server, async route"));
        closeEverything.prepend(asyncServer);
        // No offloads strategy specified on the server, blocking route does not override its strategy.
        // -> no offloading, route executed on IoExecutor
        System.out.printf("\n%d : no offloading server, blocking route\n", port);
        ServerContext blockingServer = GrpcServers.forPort(port++).initializeHttp(init -> init.executionStrategy(offloadNever())).listenAndAwait((Greeter.BlockingGreeterService) (ctx, request) -> getReply(request, "no offloading server, blocking route"));
        closeEverything.prepend(blockingServer);
        // No offloads strategy specified on the server, route overrides it to use the default strategy.
        // -> route offloaded to global executor
        System.out.printf("\n%d : no offloading server, default offloading for the route\n", port);
        ServerContext noOffloadsServerRouteOffloads = GrpcServers.forPort(port++).initializeHttp(init -> init.executionStrategy(offloadNever())).listenAndAwait(new Greeter.ServiceFactory.Builder().sayHello(defaultStrategy(), (ctx, request) -> getReplySingle(request, "no offloading server, default offloading for the route")).build());
        closeEverything.prepend(noOffloadsServerRouteOffloads);
        // No offloads strategy specified on the server, route overrides it to use a custom strategy.
        // -> route offloaded to global executor
        System.out.printf("\n%d: no offloading server, custom offloading for the route\n", port);
        ServerContext noOffloadsServerRouteOffloadCustom = GrpcServers.forPort(port++).initializeHttp(init -> init.executionStrategy(offloadNever())).listenAndAwait(new Greeter.ServiceFactory.Builder().sayHello(CUSTOM_STRATEGY, (ctx, request) -> getReplySingle(request, "no offloading server, custom offloading for the route")).build());
        closeEverything.prepend(noOffloadsServerRouteOffloadCustom);
        // Server with a default strategy but a custom executor, route does not override its strategy.
        // -> route offloaded to custom executor
        System.out.printf("\n%d : server with a default offloading and a custom executor\n", port);
        ServerContext customExecutorServer = GrpcServers.forPort(port++).initializeHttp(init -> init.executor(executor)).listenAndAwait((GreeterService) (ctx, request) -> getReplySingle(request, "server with a default offloading and a custom executor"));
        closeEverything.prepend(customExecutorServer);
        // Server has default configuration, route attempts to use no offloads strategy, which is ignored.
        // (Too late, already offloaded at the server level)
        // -> route offloaded to global executor
        System.out.printf("\n%d : default server, no offloading route\n", port);
        ServerContext noOffloadsRoute = GrpcServers.forPort(port++).listenAndAwait(new Greeter.ServiceFactory.Builder().sayHello(offloadNever(), (ctx, request) -> getReplySingle(request, "default server, no offloading route")).build());
        closeEverything.prepend(noOffloadsRoute);
        // Server has default configuration, route attempts to use no offloads strategy via annotation, which is
        // ignored. (Too late, already offloaded at the server level)
        // -> route offloaded to global executor
        System.out.printf("\n%d : default server, no offloading (annotation) route\n", port);
        ServerContext noOffloadsAnnotation = GrpcServers.forPort(port++).listenAndAwait(new NoOffloadsGreeterService());
        closeEverything.prepend(noOffloadsAnnotation);
        // No offloads strategy specified on the server, route overrides it to also use no offloads strategy,
        // which is redundant.
        // -> no offloading, route executed on IoExecutor
        System.out.printf("\n%d : no offloading server, no offloading route\n", port);
        ServerContext noOffloadsServerRoute = GrpcServers.forPort(port++).initializeHttp(init -> init.executionStrategy(offloadNever())).listenAndAwait(new Greeter.ServiceFactory.Builder().sayHello(offloadNever(), (ctx, request) -> getReplySingle(request, "no offloading server, no offloading route")).build());
        closeEverything.prepend(noOffloadsServerRoute);
        noOffloadsServerRoute.awaitShutdown();
    }
}
Also used : DefaultThreadFactory(io.servicetalk.concurrent.api.DefaultThreadFactory) HelloReply(io.grpc.examples.strategies.HelloReply) ServerContext(io.servicetalk.transport.api.ServerContext) GrpcExecutionStrategies.offloadNever(io.servicetalk.grpc.api.GrpcExecutionStrategies.offloadNever) Single(io.servicetalk.concurrent.api.Single) NoOffloadsRouteExecutionStrategy(io.servicetalk.router.api.NoOffloadsRouteExecutionStrategy) CompositeCloseable(io.servicetalk.concurrent.api.CompositeCloseable) Greeter(io.grpc.examples.strategies.Greeter) GrpcServiceContext(io.servicetalk.grpc.api.GrpcServiceContext) GreeterService(io.grpc.examples.strategies.Greeter.GreeterService) GrpcExecutionStrategies.defaultStrategy(io.servicetalk.grpc.api.GrpcExecutionStrategies.defaultStrategy) GrpcExecutionStrategies(io.servicetalk.grpc.api.GrpcExecutionStrategies) Single.succeeded(io.servicetalk.concurrent.api.Single.succeeded) GrpcExecutionStrategy(io.servicetalk.grpc.api.GrpcExecutionStrategy) Executor(io.servicetalk.concurrent.api.Executor) Executors(io.servicetalk.concurrent.api.Executors) AsyncCloseables(io.servicetalk.concurrent.api.AsyncCloseables) DefaultThreadFactory(io.servicetalk.concurrent.api.DefaultThreadFactory) GrpcServers(io.servicetalk.grpc.netty.GrpcServers) HelloRequest(io.grpc.examples.strategies.HelloRequest) Executor(io.servicetalk.concurrent.api.Executor) ServerContext(io.servicetalk.transport.api.ServerContext) Greeter(io.grpc.examples.strategies.Greeter) CompositeCloseable(io.servicetalk.concurrent.api.CompositeCloseable)

Aggregations

Greeter (io.grpc.examples.strategies.Greeter)1 GreeterService (io.grpc.examples.strategies.Greeter.GreeterService)1 HelloReply (io.grpc.examples.strategies.HelloReply)1 HelloRequest (io.grpc.examples.strategies.HelloRequest)1 AsyncCloseables (io.servicetalk.concurrent.api.AsyncCloseables)1 CompositeCloseable (io.servicetalk.concurrent.api.CompositeCloseable)1 DefaultThreadFactory (io.servicetalk.concurrent.api.DefaultThreadFactory)1 Executor (io.servicetalk.concurrent.api.Executor)1 Executors (io.servicetalk.concurrent.api.Executors)1 Single (io.servicetalk.concurrent.api.Single)1 Single.succeeded (io.servicetalk.concurrent.api.Single.succeeded)1 GrpcExecutionStrategies (io.servicetalk.grpc.api.GrpcExecutionStrategies)1 GrpcExecutionStrategies.defaultStrategy (io.servicetalk.grpc.api.GrpcExecutionStrategies.defaultStrategy)1 GrpcExecutionStrategies.offloadNever (io.servicetalk.grpc.api.GrpcExecutionStrategies.offloadNever)1 GrpcExecutionStrategy (io.servicetalk.grpc.api.GrpcExecutionStrategy)1 GrpcServiceContext (io.servicetalk.grpc.api.GrpcServiceContext)1 GrpcServers (io.servicetalk.grpc.netty.GrpcServers)1 NoOffloadsRouteExecutionStrategy (io.servicetalk.router.api.NoOffloadsRouteExecutionStrategy)1 ServerContext (io.servicetalk.transport.api.ServerContext)1