Skip to content

Commit

Permalink
rename FaultToleranceContext.get() to call()
Browse files Browse the repository at this point in the history
This is because there are other `get()` methods, with parameters, whose
purpose is completely different. Therefore, the zero-parameter version
of `get()` is renamed back to `call()`, as it was before. This means
that the `FaultToleranceContext` class can no longer implement `Supplier`,
and we don't want it to implement `Callable` either. Fortunately, that
has never been useful for anything, so this commit just drops it.
  • Loading branch information
Ladicek committed Nov 22, 2024
1 parent f2e4dac commit d0a7c64
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
14 changes: 7 additions & 7 deletions doc/modules/ROOT/pages/internals/core.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ NOTE: The `Future` type here is _not_ a `java.util.concurrent.Future`.
It comes from {smallrye-fault-tolerance} and it could be described as a very bare-bones variant of `CompletableFuture`, except it's split into `Completer` and `Future`.
It is not supposed to be used outside of this project.

The `FaultToleranceContext` is a `Supplier<Future<V>>` that represents the method invocation guarded by this fault tolerance strategy.
The fault tolerance strategy does its work around `ctx.get()`.
It can catch exceptions, invoke `ctx.get()` multiple times, invoke something else, etc.
The `FaultToleranceContext` is similar to a `Callable<Future<V>>`; it represents the method invocation guarded by this fault tolerance strategy.
The fault tolerance strategy does its work around `ctx.call()`.
It can catch exceptions, invoke `ctx.call()` multiple times, invoke something else, etc.
As an example, let's consider this strategy, applicable to methods that return a `String`:

[source,java]
Expand All @@ -28,7 +28,7 @@ public class MyStringFallback implements FaultToleranceStrategy<String> {
public Future<String> apply(FaultToleranceContext<String> ctx) {
Completer<String> completer = Completer.create();
try {
ctx.get().then((value, error) -> {
ctx.call().then((value, error) -> {
if (error == null) {
completer.complete(value);
} else {
Expand Down Expand Up @@ -81,9 +81,9 @@ public class MyStringFallback implements FaultToleranceStrategy<String> {

We see that one strategy delegates to another, passing the `FaultToleranceContext` along.
In fact, all the implementations in {smallrye-fault-tolerance} are written like this: they expect to be used in a chain, so they take another `FaultToleranceStrategy` to which they delegate.
But if all strategies have this form, when is `ctx.get()` actually invoked?
But if all strategies have this form, when is `ctx.call()` actually invoked?
Good question!
The ultimate `ctx.get()` invocation is done by a special fault tolerance strategy which is called, well, `Invocation`.
The ultimate `ctx.call()` invocation is done by a special fault tolerance strategy which is called, well, `Invocation`.

As an example which uses real {microprofile-fault-tolerance} annotations, let's consider this method:

Expand All @@ -105,7 +105,7 @@ Fallback(
Retry(
Timeout(
Invocation(
// ctx.get() will happen here
// ctx.call() will happen here
// that will, in turn, invoke doSomething()
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.util.function.Consumer;
import java.util.function.Supplier;

public final class FaultToleranceContext<V> implements Supplier<Future<V>> {
public final class FaultToleranceContext<V> {
private final Supplier<Future<V>> delegate;
private final boolean isAsync;

Expand All @@ -16,8 +16,7 @@ public FaultToleranceContext(Supplier<Future<V>> delegate, boolean isAsync) {
this.isAsync = isAsync;
}

@Override
public Future<V> get() {
public Future<V> call() {
return delegate.get();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private Invocation() {
public Future<V> apply(FaultToleranceContext<V> ctx) {
LOG.trace("Guarded method invocation started");
try {
return ctx.get();
return ctx.call();
} catch (Exception e) {
return Future.ofError(e);
} finally {
Expand Down

0 comments on commit d0a7c64

Please sign in to comment.