Skip to content

Commit

Permalink
Support for CompletableFuture and Future async types
Browse files Browse the repository at this point in the history
If a RetryCallback returns an async type it can be wrapped by the
RetryTemplate. The template needs to know about all the callback
return types that need wrapping, via its RetryResultProcessors
property (not set by default). See AsyncRetryTemplateTests for
examples of how to do this.
  • Loading branch information
dsyer authored and ashamukov committed Apr 28, 2019
1 parent e2b0555 commit 5852e1b
Show file tree
Hide file tree
Showing 9 changed files with 749 additions and 93 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,8 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/org/springframework/classify/SubclassClassifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,19 @@ public C classify(T classifiable) {
value = this.classified.get(cls);
}

// check for interfaces subclasses
if (value == null) {
for (Class<?> cls = exceptionClass; !cls.equals(Object.class)
&& value == null; cls = cls.getSuperclass()) {
for (Class<?> ifc : cls.getInterfaces()) {
value = this.classified.get(ifc);
if (value != null) {
break;
}
}
}
}

// ConcurrentHashMap doesn't allow nulls
if (value != null) {
this.classified.put(exceptionClass, value);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright 2019 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.retry.support;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutionException;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

import org.springframework.retry.RetryCallback;
import org.springframework.retry.RetryException;

/**
* A {@link RetryResultProcessor} for a {@link CompletableFuture}. If a
* {@link RetryCallback} returns a <code>CompletableFuture</code> this processor can be
* used internally by the {@link RetryTemplate} to wrap it and process the result.
*
* @author Dave Syer
*/
public class CompletableFutureRetryResultProcessor
implements RetryResultProcessor<CompletableFuture<?>> {

@Override
public Result<CompletableFuture<?>> process(CompletableFuture<?> completable,
Supplier<Result<CompletableFuture<?>>> supplier,
Consumer<Throwable> handler) {
@SuppressWarnings("unchecked")
CompletableFuture<Object> typed = (CompletableFuture<Object>) completable;
CompletableFuture<?> handle = typed
.thenApply(value -> CompletableFuture.completedFuture(value))
.exceptionally(throwable -> apply(supplier, handler, throwable))
.thenCompose(Function.identity());
return new Result<>(handle);
}

private CompletableFuture<Object> apply(
Supplier<Result<CompletableFuture<?>>> supplier, Consumer<Throwable> handler,
Throwable throwable) {
Throwable error = throwable;
try {
if (throwable instanceof ExecutionException
|| throwable instanceof CompletionException) {
error = throwable.getCause();
}
handler.accept(error);
Result<CompletableFuture<?>> result = supplier.get();
if (result.isComplete()) {
@SuppressWarnings("unchecked")
CompletableFuture<Object> output = (CompletableFuture<Object>) result
.getResult();
return output;
}
throw result.exception;
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
error = e;
}
catch (CompletionException e) {
error = e.getCause();
}
catch (ExecutionException e) {
error = e.getCause();
}
catch (RetryException e) {
error = e.getCause();
}
catch (Throwable e) {
error = e;
}
throw RetryTemplate.runtimeException(error);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
* Copyright 2019 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.retry.support;

import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.Consumer;
import java.util.function.Supplier;

import org.springframework.retry.RetryCallback;
import org.springframework.retry.RetryException;

/**
* A {@link RetryResultProcessor} for a plain {@link Future}. If a {@link RetryCallback}
* returns a <code>Future</code> this processor can be used internally by the
* {@link RetryTemplate} to wrap it and process the result.
*
* @author Dave Syer
*/
public class FutureRetryResultProcessor implements RetryResultProcessor<Future<?>> {

@Override
public Result<Future<?>> process(Future<?> future,
Supplier<Result<Future<?>>> supplier, Consumer<Throwable> handler) {
return new Result<Future<?>>(new FutureWrapper(future, supplier, handler));
}

private class FutureWrapper implements Future<Object> {

private Future<?> delegate;

private Supplier<Result<Future<?>>> supplier;

private Consumer<Throwable> handler;

FutureWrapper(Future<?> delegate, Supplier<Result<Future<?>>> supplier,
Consumer<Throwable> handler) {
this.delegate = delegate;
this.supplier = supplier;
this.handler = handler;
}

@Override
public boolean cancel(boolean mayInterruptIfRunning) {
return this.delegate.cancel(mayInterruptIfRunning);
}

@Override
public boolean isCancelled() {
return this.delegate.isCancelled();
}

@Override
public boolean isDone() {
return this.delegate.isDone();
}

@Override
public Object get() throws InterruptedException, ExecutionException {
try {
return this.delegate.get();
}
catch (ExecutionException e) {
return handle(e);
}
}

@Override
public Object get(long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException {
try {
return this.delegate.get(timeout, unit);
}
catch (ExecutionException e) {
return handle(e, timeout, unit);
}
}

private Object handle(ExecutionException throwable) {
return handle(throwable, -1, null);
}

private Object handle(ExecutionException throwable, long timeout, TimeUnit unit) {
Throwable error = throwable.getCause();
try {
this.handler.accept(error);
Result<Future<?>> result = this.supplier.get();
if (result.isComplete()) {
if (timeout < 0) {
return result.getResult().get();
}
else {
return result.getResult().get(timeout, unit);
}
}
throw result.exception;
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
error = e;
}
catch (CompletionException e) {
error = e.getCause();
}
catch (ExecutionException e) {
error = e.getCause();
}
catch (RetryException e) {
error = e.getCause();
}
catch (Throwable e) {
error = e;
}
throw RetryTemplate.runtimeException(error);
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2019 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.retry.support;

import java.util.function.Consumer;
import java.util.function.Supplier;

/**
* @author Dave Syer
* @param <T> the type of result from the retryable operation
*/
public interface RetryResultProcessor<T> {

Result<T> process(T input, Supplier<Result<T>> supplier, Consumer<Throwable> handler);

public static class Result<T> {

public Throwable exception;

private T result;

private boolean complete;

public Result(Throwable exception) {
this.exception = exception;
this.complete = false;
}

public Result(T result) {
this.result = result;
this.complete = true;
}

boolean isComplete() {
return this.complete;
}

public Throwable getException() {
return exception;
}

public T getResult() {
return result;
}

}

}
Loading

0 comments on commit 5852e1b

Please sign in to comment.