Search in sources :

Example 1 with ExecResult

use of ratpack.exec.ExecResult in project ratpack by ratpack.

the class BlockingHttpClient method request.

public ReceivedResponse request(HttpClient httpClient, URI uri, ExecController execController, Duration timeout, Action<? super RequestSpec> action) throws Throwable {
    CountDownLatch latch = new CountDownLatch(1);
    AtomicReference<ExecResult<ReceivedResponse>> result = new AtomicReference<>();
    execController.fork().start(e -> httpClient.request(uri, action.prepend(s -> s.readTimeout(Duration.ofHours(1)))).map(response -> {
        TypedData responseBody = response.getBody();
        ByteBuf responseBuffer = responseBody.getBuffer();
        ByteBuf heapResponseBodyBuffer = unreleasableBuffer(responseBuffer.isDirect() ? TestByteBufAllocators.LEAKING_UNPOOLED_HEAP.heapBuffer(responseBuffer.readableBytes()).writeBytes(responseBuffer) : responseBuffer.retain());
        return new DefaultReceivedResponse(response.getStatus(), response.getHeaders(), new ByteBufBackedTypedData(heapResponseBodyBuffer, responseBody.getContentType()));
    }).connect(new Downstream<ReceivedResponse>() {

        @Override
        public void success(ReceivedResponse value) {
            result.set(ExecResult.of(Result.success(value)));
            latch.countDown();
        }

        @Override
        public void error(Throwable throwable) {
            result.set(ExecResult.of(Result.error(throwable)));
            latch.countDown();
        }

        @Override
        public void complete() {
            result.set(ExecResult.complete());
            latch.countDown();
        }
    }));
    try {
        if (!latch.await(timeout.toNanos(), TimeUnit.NANOSECONDS)) {
            TemporalUnit unit = timeout.getUnits().get(0);
            throw new IllegalStateException("Request to " + uri + " took more than " + timeout.get(unit) + " " + unit.toString() + " to complete");
        }
    } catch (InterruptedException e) {
        throw Exceptions.uncheck(e);
    }
    return result.get().getValueOrThrow();
}
Also used : TypedData(ratpack.http.TypedData) ByteBufBackedTypedData(ratpack.http.internal.ByteBufBackedTypedData) TemporalUnit(java.time.temporal.TemporalUnit) DefaultReceivedResponse(ratpack.http.client.internal.DefaultReceivedResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuf(io.netty.buffer.ByteBuf) ReceivedResponse(ratpack.http.client.ReceivedResponse) DefaultReceivedResponse(ratpack.http.client.internal.DefaultReceivedResponse) ByteBufBackedTypedData(ratpack.http.internal.ByteBufBackedTypedData) Downstream(ratpack.exec.Downstream) ExecResult(ratpack.exec.ExecResult)

Example 2 with ExecResult

use of ratpack.exec.ExecResult in project ratpack by ratpack.

the class DefaultParallelBatch method yieldAll.

@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);
            }));
        }
    });
}
Also used : Types(ratpack.util.Types) Iterator(java.util.Iterator) Execution(ratpack.exec.Execution) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Operation(ratpack.exec.Operation) Promise(ratpack.exec.Promise) AtomicReference(java.util.concurrent.atomic.AtomicReference) BiAction(ratpack.func.BiAction) ParallelBatch(ratpack.exec.util.ParallelBatch) BufferingPublisher(ratpack.stream.internal.BufferingPublisher) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Lists(com.google.common.collect.Lists) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Action(ratpack.func.Action) Subscription(org.reactivestreams.Subscription) TransformablePublisher(ratpack.stream.TransformablePublisher) Collections(java.util.Collections) ExecResult(ratpack.exec.ExecResult) Promise(ratpack.exec.Promise) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecResult(ratpack.exec.ExecResult)

Aggregations

AtomicReference (java.util.concurrent.atomic.AtomicReference)2 ExecResult (ratpack.exec.ExecResult)2 Lists (com.google.common.collect.Lists)1 ByteBuf (io.netty.buffer.ByteBuf)1 TemporalUnit (java.time.temporal.TemporalUnit)1 Collections (java.util.Collections)1 Iterator (java.util.Iterator)1 List (java.util.List)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Subscription (org.reactivestreams.Subscription)1 Downstream (ratpack.exec.Downstream)1 Execution (ratpack.exec.Execution)1 Operation (ratpack.exec.Operation)1 Promise (ratpack.exec.Promise)1 ParallelBatch (ratpack.exec.util.ParallelBatch)1 Action (ratpack.func.Action)1 BiAction (ratpack.func.BiAction)1