use of java.util.concurrent.ForkJoinPool.ForkJoinWorkerThreadFactory in project grpc-java by grpc.
the class AsyncServer method newServer.
static Server newServer(ServerConfiguration config) throws IOException {
SslContext sslContext = null;
if (config.tls) {
System.out.println("Using fake CA for TLS certificate.\n" + "Run the Java client with --tls --testca");
File cert = TestUtils.loadCert("server1.pem");
File key = TestUtils.loadCert("server1.key");
SslContextBuilder sslContextBuilder = GrpcSslContexts.forServer(cert, key);
if (config.transport == ServerConfiguration.Transport.NETTY_NIO) {
sslContextBuilder = GrpcSslContexts.configure(sslContextBuilder, SslProvider.JDK);
} else {
// Native transport with OpenSSL
sslContextBuilder = GrpcSslContexts.configure(sslContextBuilder, SslProvider.OPENSSL);
}
if (config.useDefaultCiphers) {
sslContextBuilder.ciphers(null);
}
sslContext = sslContextBuilder.build();
}
final EventLoopGroup boss;
final EventLoopGroup worker;
final Class<? extends ServerChannel> channelType;
switch(config.transport) {
case NETTY_NIO:
{
boss = new NioEventLoopGroup();
worker = new NioEventLoopGroup();
channelType = NioServerSocketChannel.class;
break;
}
case NETTY_EPOLL:
{
try {
// These classes are only available on linux.
Class<?> groupClass = Class.forName("io.netty.channel.epoll.EpollEventLoopGroup");
@SuppressWarnings("unchecked") Class<? extends ServerChannel> channelClass = (Class<? extends ServerChannel>) Class.forName("io.netty.channel.epoll.EpollServerSocketChannel");
boss = (EventLoopGroup) groupClass.getConstructor().newInstance();
worker = (EventLoopGroup) groupClass.getConstructor().newInstance();
channelType = channelClass;
break;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
case NETTY_UNIX_DOMAIN_SOCKET:
{
try {
// These classes are only available on linux.
Class<?> groupClass = Class.forName("io.netty.channel.epoll.EpollEventLoopGroup");
@SuppressWarnings("unchecked") Class<? extends ServerChannel> channelClass = (Class<? extends ServerChannel>) Class.forName("io.netty.channel.epoll.EpollServerDomainSocketChannel");
boss = (EventLoopGroup) groupClass.getConstructor().newInstance();
worker = (EventLoopGroup) groupClass.getConstructor().newInstance();
channelType = channelClass;
break;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
default:
{
// Should never get here.
throw new IllegalArgumentException("Unsupported transport: " + config.transport);
}
}
NettyServerBuilder builder = NettyServerBuilder.forAddress(config.address).bossEventLoopGroup(boss).workerEventLoopGroup(worker).channelType(channelType).addService(new BenchmarkServiceImpl()).sslContext(sslContext).flowControlWindow(config.flowControlWindow);
if (config.directExecutor) {
builder.directExecutor();
} else {
// TODO(carl-mastrangelo): This should not be necessary. I don't know where this should be
// put. Move it somewhere else, or remove it if no longer necessary.
// See: https://github.com/grpc/grpc-java/issues/2119
builder.executor(new ForkJoinPool(Runtime.getRuntime().availableProcessors(), new ForkJoinWorkerThreadFactory() {
final AtomicInteger num = new AtomicInteger();
@Override
public ForkJoinWorkerThread newThread(ForkJoinPool pool) {
ForkJoinWorkerThread thread = ForkJoinPool.defaultForkJoinWorkerThreadFactory.newThread(pool);
thread.setDaemon(true);
thread.setName("grpc-server-app-" + "-" + num.getAndIncrement());
return thread;
}
}, UncaughtExceptionHandlers.systemExit(), true));
}
return builder.build();
}
use of java.util.concurrent.ForkJoinPool.ForkJoinWorkerThreadFactory in project grpc-java by grpc.
the class Utils method getExecutor.
private static synchronized ExecutorService getExecutor() {
if (clientExecutor == null) {
clientExecutor = new ForkJoinPool(Runtime.getRuntime().availableProcessors(), new ForkJoinWorkerThreadFactory() {
final AtomicInteger num = new AtomicInteger();
@Override
public ForkJoinWorkerThread newThread(ForkJoinPool pool) {
ForkJoinWorkerThread thread = defaultForkJoinWorkerThreadFactory.newThread(pool);
thread.setDaemon(true);
thread.setName("grpc-client-app-" + "-" + num.getAndIncrement());
return thread;
}
}, UncaughtExceptionHandlers.systemExit(), true);
}
return clientExecutor;
}
use of java.util.concurrent.ForkJoinPool.ForkJoinWorkerThreadFactory in project torodb by torodb.
the class DefaultConcurrentToolsFactory method createExecutorService.
@Override
@SuppressFBWarnings(value = { "NP_NONNULL_PARAM_VIOLATION" }, justification = "ForkJoinPool constructor admits a null " + "UncaughtExceptionHandler")
public ExecutorService createExecutorService(String prefix, boolean blockerTasks, int maxThreads) {
ExecutorService executorService;
if (blockerTasks) {
ThreadFactory threadFactory = blockerThreadFactoryFunction.apply(prefix);
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(maxThreads, maxThreads, 10L, TimeUnit.MINUTES, new LinkedBlockingQueue<>(), threadFactory);
threadPoolExecutor.allowCoreThreadTimeOut(true);
executorService = threadPoolExecutor;
} else {
ForkJoinWorkerThreadFactory threadFactory = forkJoinThreadFactoryFunction.apply(prefix);
executorService = new ForkJoinPool(maxThreads, threadFactory, null, true);
}
shutdownHelper.terminateOnShutdown(prefix, executorService);
return executorService;
}
Aggregations