ListenableFuture provides CompletableFuture adaptation via completable()

Issue: SPR-15696
This commit is contained in:
Juergen Hoeller
2017-06-27 00:43:37 +02:00
parent 98642c7e29
commit 87430f3cd3
12 changed files with 284 additions and 51 deletions

View File

@@ -73,6 +73,12 @@ public class CompletableToListenableFutureAdapter<T> implements ListenableFuture
this.callbacks.addFailureCallback(failureCallback);
}
@Override
public CompletableFuture<T> completable() {
return this.completableFuture;
}
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
return this.completableFuture.cancel(mayInterruptIfRunning);

View File

@@ -0,0 +1,44 @@
/*
* Copyright 2002-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.util.concurrent;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
/**
* Extension of {@link CompletableFuture} which allows for cancelling
* a delegate along with the {@link CompletableFuture} itself.
*
* @author Juergen Hoeller
* @since 5.0
*/
class DelegatingCompletableFuture<T> extends CompletableFuture<T> {
private final Future<T> delegate;
public DelegatingCompletableFuture(Future<T> delegate) {
this.delegate = delegate;
}
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
boolean result = this.delegate.cancel(mayInterruptIfRunning);
super.cancel(mayInterruptIfRunning);
return result;
}
}

View File

@@ -16,6 +16,7 @@
package org.springframework.util.concurrent;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
/**
@@ -27,6 +28,7 @@ import java.util.concurrent.Future;
*
* @author Arjen Poutsma
* @author Sebastien Deleuze
* @author Juergen Hoeller
* @since 4.0
*/
public interface ListenableFuture<T> extends Future<T> {
@@ -45,4 +47,15 @@ public interface ListenableFuture<T> extends Future<T> {
*/
void addCallback(SuccessCallback<? super T> successCallback, FailureCallback failureCallback);
/**
* Expose this {@link ListenableFuture} as a JDK {@link CompletableFuture}.
* @since 5.0
*/
default CompletableFuture<T> completable() {
CompletableFuture<T> completable = new DelegatingCompletableFuture<>(this);
addCallback(completable::complete, completable::completeExceptionally);
return completable;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 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.
@@ -18,6 +18,8 @@ package org.springframework.util.concurrent;
import java.util.concurrent.ExecutionException;
import org.springframework.lang.Nullable;
/**
* Abstract class that adapts a {@link ListenableFuture} parameterized over S into a
* {@code ListenableFuture} parameterized over T. All methods are delegated to the
@@ -51,19 +53,21 @@ public abstract class ListenableFutureAdapter<T, S> extends FutureAdapter<T, S>
ListenableFuture<S> listenableAdaptee = (ListenableFuture<S>) getAdaptee();
listenableAdaptee.addCallback(new ListenableFutureCallback<S>() {
@Override
public void onSuccess(S result) {
T adapted;
try {
adapted = adaptInternal(result);
}
catch (ExecutionException ex) {
Throwable cause = ex.getCause();
onFailure(cause != null ? cause : ex);
return;
}
catch (Throwable ex) {
onFailure(ex);
return;
public void onSuccess(@Nullable S result) {
T adapted = null;
if (result != null) {
try {
adapted = adaptInternal(result);
}
catch (ExecutionException ex) {
Throwable cause = ex.getCause();
onFailure(cause != null ? cause : ex);
return;
}
catch (Throwable ex) {
onFailure(ex);
return;
}
}
successCallback.onSuccess(adapted);
}

View File

@@ -17,6 +17,7 @@
package org.springframework.util.concurrent;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
@@ -65,6 +66,14 @@ public class ListenableFutureTask<T> extends FutureTask<T> implements Listenable
this.callbacks.addFailureCallback(failureCallback);
}
@Override
public CompletableFuture<T> completable() {
CompletableFuture<T> completable = new DelegatingCompletableFuture<>(this);
this.callbacks.addSuccessCallback(completable::complete);
this.callbacks.addFailureCallback(completable::completeExceptionally);
return completable;
}
@Override
protected void done() {

View File

@@ -17,6 +17,7 @@
package org.springframework.util.concurrent;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
@@ -68,6 +69,7 @@ public class SettableListenableFuture<T> implements ListenableFuture<T> {
return this.settableTask.setExceptionResult(exception);
}
@Override
public void addCallback(ListenableFutureCallback<? super T> callback) {
this.settableTask.addCallback(callback);
@@ -78,6 +80,12 @@ public class SettableListenableFuture<T> implements ListenableFuture<T> {
this.settableTask.addCallback(successCallback, failureCallback);
}
@Override
public CompletableFuture<T> completable() {
return this.settableTask.completable();
}
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
boolean cancelled = this.settableTask.cancel(mayInterruptIfRunning);