Make ListenableFuture compliant with Java 8 lambda

Make it possible to use a ListenableFuture with Java 8
lambda expressions, using a syntax like
listenableFuture.addCallback(() -> ..., () -> ...);

Issue: SPR-11820
This commit is contained in:
Sebastien Deleuze
2014-05-27 17:03:48 +02:00
parent 89b202029a
commit 86e8bdab6b
14 changed files with 392 additions and 41 deletions

View File

@@ -29,12 +29,15 @@ import org.apache.http.nio.client.HttpAsyncClient;
import org.apache.http.nio.entity.NByteArrayEntity;
import org.apache.http.protocol.HttpContext;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.util.concurrent.FailureCallback;
import org.springframework.util.concurrent.FutureAdapter;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;
import org.springframework.util.concurrent.ListenableFutureCallbackRegistry;
import org.springframework.util.concurrent.SuccessCallback;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
/**
* {@link ClientHttpRequest} implementation that uses Apache HttpComponents HttpClient to
@@ -101,6 +104,14 @@ final class HttpComponentsAsyncClientHttpRequest extends AbstractBufferingAsyncC
this.callbacks.addCallback(callback);
}
public void addSuccessCallback(SuccessCallback<? super ClientHttpResponse> callback) {
this.callbacks.addSuccessCallback(callback);
}
public void addFailureCallback(FailureCallback callback) {
this.callbacks.addFailureCallback(callback);
}
@Override
public void completed(HttpResponse result) {
this.callbacks.success(new HttpComponentsAsyncClientHttpResponse(result));
@@ -136,6 +147,12 @@ final class HttpComponentsAsyncClientHttpRequest extends AbstractBufferingAsyncC
public void addCallback(ListenableFutureCallback<? super ClientHttpResponse> callback) {
this.callback.addCallback(callback);
}
@Override
public void addCallback(SuccessCallback<? super ClientHttpResponse> successCallback, FailureCallback failureCallback) {
this.callback.addSuccessCallback(successCallback);
this.callback.addFailureCallback(failureCallback);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -44,9 +44,11 @@ import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.client.support.AsyncHttpAccessor;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.util.Assert;
import org.springframework.util.concurrent.FailureCallback;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureAdapter;
import org.springframework.util.concurrent.ListenableFutureCallback;
import org.springframework.util.concurrent.SuccessCallback;
import org.springframework.web.util.UriTemplate;
/**
@@ -254,15 +256,21 @@ public class AsyncRestTemplate extends AsyncHttpAccessor implements AsyncRestOpe
@Override
public void addCallback(final ListenableFutureCallback<? super URI> callback) {
addCallback(callback, callback);
}
@Override
public void addCallback(final SuccessCallback<? super URI> successCallback,
final FailureCallback failureCallback) {
headersFuture.addCallback(new ListenableFutureCallback<HttpHeaders>() {
@Override
public void onSuccess(HttpHeaders result) {
callback.onSuccess(result.getLocation());
successCallback.onSuccess(result.getLocation());
}
@Override
public void onFailure(Throwable t) {
callback.onFailure(t);
failureCallback.onFailure(t);
}
});
}
@@ -391,17 +399,21 @@ public class AsyncRestTemplate extends AsyncHttpAccessor implements AsyncRestOpe
return new ListenableFuture<Set<HttpMethod>>() {
@Override
public void addCallback(
final ListenableFutureCallback<? super Set<HttpMethod>> callback) {
public void addCallback(final ListenableFutureCallback<? super Set<HttpMethod>> callback) {
addCallback(callback, callback);
}
@Override
public void addCallback(final SuccessCallback<? super Set<HttpMethod>> successCallback, final FailureCallback failureCallback) {
headersFuture.addCallback(new ListenableFutureCallback<HttpHeaders>() {
@Override
public void onSuccess(HttpHeaders result) {
callback.onSuccess(result.getAllow());
successCallback.onSuccess(result.getAllow());
}
@Override
public void onFailure(Throwable t) {
callback.onFailure(t);
failureCallback.onFailure(t);
}
});
}