New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Not an issue, but a basic question #302
Comments
You told the fallback to UpdateThis is wrong, see below. |
I am not using |
You're right, and in fact I think you want something like |
that's not what I want though (or I miss-understand your point). I want to be able to log which part of the code provided the actual result, the Since the The real use case that I have is much more involved, but it has the same idea - we have many fallbacks and we would like to be able to log - which of those actually provided the correct result. |
What I thought (though I really don't like it), is may be be able to do something like this:
Notice the:
and also the removal of
|
If you look through other Failsafe issues you'll see a lot of confusion and debate around the meaning of success and failure with respect to fallbacks. The methods In the case of a fallback, where the whole idea is to get a successful result even if the execution enclosed by the fallback fails, you would expect The reason I suggested The problem with using listeners (or instrumenting methods like Or you could build the source into the result value: public class SourcedResult<R, S> {
private final R result;
private final S source;
public static <R, S> SourcedResult<R, S> of(R result, S source) {
return new SourcedResult<R, S>(result, source);
}
private SourcedResult(R result, S source) {
this.result = result;
this.source = source;
}
public R getResult() { return result; }
public S getSource() { return source; }
@Override public String toString() {
return String.format("%s(%s)", result, source);
}
} I adapted your example in the issue description to use these sourced results: enum Source { MAIN, FALLBACK }
static void revisedExample() {
RetryPolicy<SourcedResult<String, Source>> retryPolicy = new RetryPolicy<SourcedResult<String, Source>>()
.withMaxAttempts(1)
.onSuccess(x -> System.out.println("main call provided the result"))
.onFailure(x -> System.out.println("main call failed"));
Fallback<SourcedResult<String, Source>> fallback = Fallback.of(() -> {
System.out.println("executing first fallback");
return SourcedResult.of("just fine first callback", Source.FALLBACK);
})
.handleIf(Objects::nonNull)
.onSuccess(result -> System.out.println("fallback provided a result " + result));
Failsafe.with(fallback, retryPolicy).get(() -> { return SourcedResult.of("OK", Source.MAIN); });
} and the output clarifies that even though the fallback's success listener is triggered, the result being reported is from the main call:
|
funny that we ended up writing close to the same exact pattern, we call I am, btw, in no shape or form complaining about the choices made; just wanted to make it clear for myself. Thank you for you elaborate answer! |
Suppose I have an action, that if fails, triggers a fallback. As simple as it can get:
This works just fine, but somehow I miss-understand the logs. Running this, I get :
Why is
onSuccess
from theFallback
triggered? May be I miss the obvious here... what I would like to achieve is to log which of the methods got my correct result. Be that the "main call" or the "fallback".Thank you.
The text was updated successfully, but these errors were encountered: