@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 public Promise<List<? extends ExecResult<T>>> yieldAll() { List<Promise<T>> promises = Lists.newArrayList(this.promises); if (promises.isEmpty()) { return Promise.value(Collections.emptyList()); } List<ExecResult<T>> results = Types.cast(promises); AtomicInteger counter = new AtomicInteger(promises.size()); return Promise.async(d -> { for (int i = 0; i < promises.size(); ++i) { final int finalI = i; //noinspection CodeBlock2Expr Execution.fork() .onStart(execInit) .onComplete(e -> { if (counter.decrementAndGet() == 0) { d.success(results); } }) .start(e -> promises.get(finalI).result(t -> { results.set(finalI, t); }) ); } }); }
public void start(StartEvent startEvent) { starting.incrementAndGet(); if (startupFailed) { serviceDidStart(this, startEvent); return; } execController.fork() .onComplete(e -> { if (startError == null) { running = true; } serviceDidStart(this, startEvent); }) .onError(e -> { startError = e; startupFailed = true; }) .start(e -> service.onStart(startEvent)); }
public static void start(EventLoop eventLoop, final RequestConstants requestConstants, Registry registry, Handler[] handlers, Action<? super Execution> onComplete) { ChainIndex index = new ChainIndex(handlers, registry, true); requestConstants.indexes.push(index); DefaultContext context = new DefaultContext(requestConstants); requestConstants.context = context; context.pathBindings = new PathBindingStorage(new RootPathBinding(requestConstants.request.getPath())); requestConstants.applicationConstants.execController.fork() .onError(throwable -> requestConstants.context.error(throwable instanceof HandlerException ? throwable.getCause() : throwable)) .onComplete(onComplete) .register(s -> s .add(Context.TYPE, context) .add(Request.TYPE, requestConstants.request) .add(Response.TYPE, requestConstants.response) .add(PathBindingStorage.TYPE, context.pathBindings) .addLazy(RequestId.TYPE, () -> registry.get(RequestId.Generator.TYPE).generate(requestConstants.request)) ) .eventLoop(eventLoop) .onStart(e -> DefaultRequest.setDelegateRegistry(requestConstants.request, e)) .start(e -> { requestConstants.execution = e; context.joinedRegistry = new ContextRegistry(context).join(requestConstants.execution); context.next(); }); }
/** * Starts the execution, and executes the given operation. * * @param operation the operation to execute * @since 1.4 */ @NonBlocking default void start(Operation operation) { start(e -> operation.then()); }
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) ) ); }
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); } } } }
try { execConfig.with(Execution.fork()) .start(e -> publisher.subscribe(new Subscriber<T>() { @Override
@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(); }
.onStart(execInit) .onComplete(e -> { if (wip.decrementAndGet() == 0 && done.get()) { Throwable t = error.get(); .start(e -> {
.start(e -> httpClient.request(uri, action.prepend(s -> s.readTimeout(Duration.ofHours(1)))) .map(response -> {
execSpec.with(Execution.fork()).start(e -> connect(promised)); return promised.promise();