forked from ReactiveX/RxJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added a CompletionStageUtils class which simplifies to recover from exceptions when using CompletableFutures. * Added missing Apache Headers. * Added two default methods to ThreadPoolBulkhead * Improved JavaDoc of ThreadPoolBulkhead.
- Loading branch information
Showing
8 changed files
with
241 additions
and
17 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
18 changes: 18 additions & 0 deletions
18
resilience4j-core/src/main/java/io/github/resilience4j/core/CallableUtils.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
19 changes: 18 additions & 1 deletion
19
resilience4j-core/src/main/java/io/github/resilience4j/core/ClassUtils.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
52 changes: 52 additions & 0 deletions
52
resilience4j-core/src/main/java/io/github/resilience4j/core/CompletionStageUtils.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,52 @@ | ||
/* | ||
* | ||
* Copyright 2020: Robert Winkler | ||
* | ||
* 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 io.github.resilience4j.core; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.CompletionStage; | ||
import java.util.function.Function; | ||
|
||
public class CompletionStageUtils { | ||
|
||
private CompletionStageUtils() { | ||
} | ||
|
||
/** | ||
* Returns a CompletionStage that is recovered from any exception. | ||
* | ||
* @param <T> return type of after | ||
* @param exceptionHandler the function applied after callable has failed | ||
* @return a CompletionStage that is recovered from any exception. | ||
*/ | ||
public static <T> CompletionStage<T> recover(CompletionStage<T> completionStage, Function<Throwable, T> exceptionHandler){ | ||
CompletableFuture<T> promise = new CompletableFuture<>(); | ||
completionStage.whenComplete((result, throwable) -> { | ||
if (throwable != null) { | ||
try { | ||
promise.complete(exceptionHandler.apply(throwable)); | ||
} catch (Exception fallbackException) { | ||
promise.completeExceptionally(fallbackException); | ||
} | ||
} else { | ||
promise.complete(result); | ||
} | ||
}); | ||
return promise; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
resilience4j-core/src/main/java/io/github/resilience4j/core/StringUtils.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
18 changes: 18 additions & 0 deletions
18
resilience4j-core/src/main/java/io/github/resilience4j/core/SupplierUtils.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
18 changes: 18 additions & 0 deletions
18
.../main/java/io/github/resilience4j/core/exception/AcquirePermissionCancelledException.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
51 changes: 51 additions & 0 deletions
51
resilience4j-core/src/test/java/io/github/resilience4j/core/CompletionStageUtilsTest.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,51 @@ | ||
package io.github.resilience4j.core; | ||
|
||
import org.junit.Test; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.function.Function; | ||
|
||
import static io.github.resilience4j.core.CompletionStageUtils.recover; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
public class CompletionStageUtilsTest { | ||
|
||
@Test | ||
public void shouldReturnResult() throws Exception { | ||
CompletableFuture<String> future = CompletableFuture.completedFuture("result"); | ||
|
||
String result = recover(future, (e) -> "fallback").toCompletableFuture() | ||
.get(1, TimeUnit.SECONDS); | ||
|
||
assertThat(result).isEqualTo("result"); | ||
} | ||
|
||
@Test | ||
public void shouldRecoverException() throws Exception { | ||
CompletableFuture<String> future = new CompletableFuture<>(); | ||
future.completeExceptionally(new RuntimeException()); | ||
|
||
String result = recover(future, (e) -> "fallback").toCompletableFuture() | ||
.get(1, TimeUnit.SECONDS); | ||
|
||
assertThat(result).isEqualTo("fallback"); | ||
} | ||
|
||
@Test | ||
public void shouldReturnExceptionFromRecoveryMethod() { | ||
CompletableFuture<String> future = new CompletableFuture<>(); | ||
future.completeExceptionally(new RuntimeException("bla")); | ||
|
||
RuntimeException exception = new RuntimeException("blub"); | ||
|
||
Function<Throwable, String> fallback = (e) -> { | ||
throw exception; | ||
}; | ||
|
||
assertThatThrownBy(() -> recover(future, fallback).toCompletableFuture() | ||
.get(1, TimeUnit.SECONDS)).hasCause(exception); | ||
} | ||
|
||
} |