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

org.asynchttpclient.extra.AsyncHandlerWrapper Maven / Gradle / Ivy

The newest version!
package org.asynchttpclient.extra;

import org.asynchttpclient.AsyncHandler;
import org.asynchttpclient.HttpResponseBodyPart;
import org.asynchttpclient.HttpResponseHeaders;
import org.asynchttpclient.HttpResponseStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicBoolean;

public class AsyncHandlerWrapper implements AsyncHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(AsyncHandlerWrapper.class);
    private final AsyncHandler asyncHandler;
    private final Semaphore available;
    private final AtomicBoolean complete = new AtomicBoolean(false);

    public AsyncHandlerWrapper(AsyncHandler asyncHandler, Semaphore available) {
        this.asyncHandler = asyncHandler;
        this.available = available;
    }

    private void complete() {
        if (complete.compareAndSet(false, true))
            available.release();
        if (LOGGER.isDebugEnabled())
            LOGGER.debug("Current Throttling Status after onThrowable {}", available.availablePermits());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onThrowable(Throwable t) {
        try {
            asyncHandler.onThrowable(t);
        } finally {
            complete();
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public State onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception {
        return asyncHandler.onBodyPartReceived(bodyPart);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public State onStatusReceived(HttpResponseStatus responseStatus) throws Exception {
        return asyncHandler.onStatusReceived(responseStatus);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public State onHeadersReceived(HttpResponseHeaders headers) throws Exception {
        return asyncHandler.onHeadersReceived(headers);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public T onCompleted() throws Exception {
        try {
            return asyncHandler.onCompleted();
        } finally {
            complete();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy