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

com.king.platform.net.http.netty.ResponseFuture Maven / Gradle / Ivy

There is a newer version: 3.0.26
Show newest version
// Copyright (C) king.com Ltd 2015
// https://github.com/king/king-http-client
// Author: Magnus Gustafsson
// License: Apache 2.0, https://raw.github.com/king/king-http-client/LICENSE-APACHE

package com.king.platform.net.http.netty;

import com.king.platform.net.http.HttpResponse;
import com.king.platform.net.http.netty.eventbus.*;

import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;

public class ResponseFuture extends CompletableFuture> {
	private final RequestEventBus requestEventBus;
	private final HttpRequestContext requestContext;

	public ResponseFuture(RequestEventBus requestEventBus, HttpRequestContext requestContext, Executor callbackExecutor) {
		this.requestEventBus = requestEventBus;
		this.requestContext = requestContext;

		requestEventBus.subscribe(Event.ERROR, new RunOnceCallback2() {
			@Override
			public void onFirstEvent(HttpRequestContext requestContext, Throwable throwable) {
				callbackExecutor.execute(new Runnable() {
					@Override
					public void run() {
						ResponseFuture.this.completeExceptionally(throwable);
					}
				});

			}
		});

		requestEventBus.subscribe(Event.COMPLETED, new RunOnceCallback1() {
			@Override
			public void onFirstEvent(HttpRequestContext payload) {
				callbackExecutor.execute(new Runnable() {
					@Override
					public void run() {
						ResponseFuture.this.complete(payload.getHttpResponse());
					}
				});
			}
		});


	}

	@Override
	public boolean cancel(boolean mayInterruptIfRunning) {
		if (isCancelled() || isDone() || isCompletedExceptionally()) {
			return false;
		}

		requestEventBus.triggerEvent(Event.ERROR, requestContext, new CancellationException());

		super.cancel(mayInterruptIfRunning);
		return true;
	}



	public static  CompletableFuture> error(Throwable error) {
		ResponseFuture future = new ResponseFuture<>(new NoopRequestEventBus(), null, null);
		future.completeExceptionally(error);
		return future;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy