All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zalando.riptide.AsyncPlugin Maven / Gradle / Ivy

There is a newer version: 4.0.0
Show newest version
package org.zalando.riptide;

import lombok.AllArgsConstructor;

import java.util.concurrent.Executor;

import static java.util.concurrent.CompletableFuture.supplyAsync;
import static java.util.function.Function.identity;
import static org.zalando.fauxpas.FauxPas.throwingSupplier;

@AllArgsConstructor
final class AsyncPlugin implements Plugin {

    private final Executor executor;

    @Override
    public RequestExecution aroundAsync(final RequestExecution execution) {
        return arguments ->
                supplyAsync(throwingSupplier(() -> execution.execute(arguments)), executor)
                .thenCompose(identity());
    }

    @Override
    public RequestExecution aroundNetwork(final RequestExecution execution) {
        return arguments ->
                execution.execute(arguments)
                        .whenCompleteAsync((r, e) -> {
                            // this will force any further callbacks to be executed using the executor
                        }, executor);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy