-
Notifications
You must be signed in to change notification settings - Fork 519
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for CompletableFuture and Future async types
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
Showing
9 changed files
with
749 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/main/java/org/springframework/retry/support/CompletableFutureRetryResultProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
136 changes: 136 additions & 0 deletions
136
src/main/java/org/springframework/retry/support/FutureRetryResultProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} | ||
|
||
} |
62 changes: 62 additions & 0 deletions
62
src/main/java/org/springframework/retry/support/RetryResultProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.