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

io.vrap.rmf.base.client.http.QueueMiddleware Maven / Gradle / Ivy

There is a newer version: 17.17.0
Show newest version

package io.vrap.rmf.base.client.http;

import java.time.Duration;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledExecutorService;
import java.util.function.Function;

import io.vrap.rmf.base.client.ApiHttpRequest;
import io.vrap.rmf.base.client.ApiHttpResponse;

import dev.failsafe.Bulkhead;
import dev.failsafe.Failsafe;
import dev.failsafe.FailsafeExecutor;
import dev.failsafe.spi.Scheduler;

/**
 * Implementation of a Queue to limit the number of concurrent requests handled by the client
 *
 * {@include.example io.vrap.rmf.base.client.QueueMiddlewareTest#queueConfiguration()}
 *
 * @deprecated use {@link PolicyMiddleware} instead
 */
@Deprecated
public class QueueMiddleware implements QueueRequestMiddleware, AutoCloseable {

    private final FailsafeExecutor> failsafeExecutor;

    public QueueMiddleware(final ExecutorService executorService, final int maxConnection, final Duration maxWaitTime) {
        this(Scheduler.of(executorService), maxConnection, maxWaitTime);
    }

    public QueueMiddleware(final ScheduledExecutorService executorService, final int maxConnection,
            final Duration maxWaitTime) {
        this(Scheduler.of(executorService), maxConnection, maxWaitTime);
    }

    public QueueMiddleware(final int maxConnection, final Duration maxWaitTime) {
        this(Scheduler.DEFAULT, maxConnection, maxWaitTime);
    }

    public QueueMiddleware(final Scheduler scheduler, final int maxConnections, final Duration maxWaitTime) {
        final Bulkhead> bulkhead = Bulkhead.> builder(maxConnections)
                .withMaxWaitTime(maxWaitTime)
                .build();
        this.failsafeExecutor = Failsafe.with(bulkhead).with(scheduler);
    }

    @Override
    public CompletableFuture> invoke(ApiHttpRequest request,
            Function>> next) {
        return failsafeExecutor.getStageAsync(() -> next.apply(request));
    }

    @Override
    public void close() {
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy