@Override ExecutorService getExecutor() { return execController.getEventLoopGroup(); } }
/** * Returns the execution controller bound to the current thread, or throws an exception if called on a non Ratpack managed compute thread. * <p> * If called on a non Ratpack compute thread, the returned optional will be empty. * * @return the execution controller for the current thread * @throws UnmanagedThreadException when called from a non Ratpack managed thread */ static ExecController require() throws UnmanagedThreadException { return current().orElseThrow(UnmanagedThreadException::new); }
/** * Used to create a new execution. * <p> * This method obtains the thread bound {@link ExecController} and simply calls {@link ExecController#fork()}. * * @return an execution starter * @throws UnmanagedThreadException if there is no thread bound execution controller (i.e. this was called on a thread that is not managed by the Ratpack application) */ static ExecStarter fork() throws UnmanagedThreadException { return ExecController.require().fork(); }
default ExecStarter fork() { return getController().fork(); }
@Override ExecutorService getExecutor() { return execController.getBlockingExecutor(); } }
/** * A scheduler that uses the application event loop and initialises each job as an {@link ratpack.exec.Execution} (via {@link ExecController#fork()}). * <p> * That same as {@link #scheduler(ExecController)}, but obtains the exec controller via {@link ExecController#require()}. * * @return a scheduler */ public static Scheduler scheduler() { return scheduler(ExecController.require()); }
@Inject public HystrixThreadPoolMetricsPeriodicPublisher(HystrixModule.Config config, ExecController execController) { super( execController.getExecutor(), integer -> HystrixThreadPoolMetrics.getInstances(), Duration.ofSeconds(config.getStreamInterval()) ); }
@Override public void close() { controller.close(); }
@Override public void run(Action<? super RegistrySpec> registry, Action<? super Execution> action) throws Exception { final AtomicReference<Throwable> thrown = new AtomicReference<>(); final CountDownLatch latch = new CountDownLatch(1); controller.fork() .onError(thrown::set) .register(registry) .onComplete(e -> latch.countDown() ) .start(action::execute); latch.await(); Throwable throwable = thrown.get(); if (throwable != null) { throw Exceptions.toException(throwable); } }
@Override ExecutorService getExecutor() { return execController.getBlockingExecutor(); } }
@Inject public HystrixCommandMetricsPeriodicPublisher(HystrixModule.Config config, ExecController execController) { super( execController.getExecutor(), integer -> HystrixCommandMetrics.getInstances(), Duration.ofSeconds(config.getStreamInterval()) ); }
throw Exceptions.uncheck(e); } finally { registry.get(ExecController.class).close();
private <T extends HttpResponse> Promise<T> intercept(Promise<T> promise, Action<? super HttpResponse> action, Action<? super Throwable> errorAction) { return promise.wiretap(r -> { if (r.isError()) { ExecController.require() .fork() .eventLoop(Execution.current().getEventLoop()) .start(e -> errorAction.execute(r.getThrowable()) ); } }) .next(r -> ExecController.require() .fork() .eventLoop(Execution.current().getEventLoop()) .start(e -> action.execute(r) ) ); }
@Override ExecutorService getExecutor() { return execController.getEventLoopGroup(); } }
public void stop(StopEvent stopEvent) { if (stopped.compareAndSet(false, true)) { if (running) { execController.fork() .onComplete(e -> serviceDidStop(this, stopEvent) ) .onError(e -> LOGGER.warn("Service '" + service.getName() + "' thrown an exception while stopping.", e) ) .start(e -> service.onStop(stopEvent)); } else { serviceDidStop(this, stopEvent); } } } }
}, execution.getController().getBlockingExecutor() ).thenAcceptAsync(v -> continuation.resume(() -> downstream.accept(v)), eventLoop)
private Scheduler getDelegateScheduler() { return ExecController.current() .map(c -> map.computeIfAbsent(c, this::getExecutorBackedScheduler)) .orElseGet(() -> { if (fallback.get() == null) { int nThreads = Runtime.getRuntime().availableProcessors(); ExecutorService executor = Executors.newFixedThreadPool(nThreads); Scheduler scheduler = Schedulers.from(executor); fallback.compareAndSet(null, scheduler); } return fallback.get(); }); }
@Inject public HystrixCollapserMetricsPeriodicPublisher(HystrixModule.Config config, ExecController execController) { super( execController.getExecutor(), integer -> HystrixCollapserMetrics.getInstances(), Duration.ofSeconds(config.getStreamInterval()) ); }
@Override public EventLoopGroup eventLoopGroup(ThreadFactory threadFactory) { return Execution.current().getController().getEventLoopGroup(); }
@Override public <T> ExecResult<T> yield(Action<? super RegistrySpec> registry, final Function<? super Execution, ? extends Promise<T>> func) throws Exception { AtomicReference<ExecResult<T>> reference = new AtomicReference<>(); CountDownLatch latch = new CountDownLatch(1); controller.fork() .register(registry) .onError(throwable -> reference.set(new ResultBackedExecResult<>(Result.<T>error(throwable)))) .onComplete(exec -> latch.countDown()) .start(execution -> { reference.set(ExecResult.complete()); Promise<T> promise = func.apply(execution); if (promise == null) { reference.set(null); } else { promise.then(t -> reference.set(new ResultBackedExecResult<>(Result.success(t)))); } }); latch.await(); return reference.get(); }