ratpack.exec.internal.DefaultExecController Maven / Gradle / Ivy
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ratpack.exec.internal;
import io.netty.channel.EventLoopGroup;
import io.netty.util.concurrent.DefaultThreadFactory;
import ratpack.exec.ExecControl;
import ratpack.exec.ExecController;
import ratpack.util.internal.ChannelImplDetector;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class DefaultExecController implements ExecController {
private final ExecutorService blockingExecutor;
private final EventLoopGroup eventLoopGroup;
private final DefaultExecControl control;
private final int numThreads;
public DefaultExecController() {
this(Runtime.getRuntime().availableProcessors() * 2);
}
public DefaultExecController(int numThreads) {
this.numThreads = numThreads;
this.eventLoopGroup = ChannelImplDetector.eventLoopGroup(numThreads, new ExecControllerBindingThreadFactory("ratpack-compute", Thread.MAX_PRIORITY));
this.blockingExecutor = Executors.newCachedThreadPool(new ExecControllerBindingThreadFactory("ratpack-blocking", Thread.NORM_PRIORITY));
this.control = new DefaultExecControl(this);
}
public void close() {
eventLoopGroup.shutdownGracefully(0, 0, TimeUnit.SECONDS);
blockingExecutor.shutdown();
}
@Override
public ScheduledExecutorService getExecutor() {
return eventLoopGroup;
}
@Override
public ExecutorService getBlockingExecutor() {
return blockingExecutor;
}
@Override
public EventLoopGroup getEventLoopGroup() {
return eventLoopGroup;
}
@Override
public ExecControl getControl() {
return control;
}
private class ExecControllerBindingThreadFactory extends DefaultThreadFactory {
public ExecControllerBindingThreadFactory(String name, int priority) {
super(name, priority);
}
@Override
public Thread newThread(final Runnable r) {
return super.newThread(() -> {
ExecControllerThreadBinding.set(DefaultExecController.this);
r.run();
});
}
}
@Override
public boolean isManagedThread() {
return ExecControllerThreadBinding.get().map(c -> c == this).orElse(false);
}
@Override
public int getNumThreads() {
return numThreads;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy