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