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
Support basic Java Executor interface #221
Comments
|
@whiskeysierra is right on - Failsafe wouldn't be able to return execution results from an |
Yes, I understand the semantic predicament, but you could have a wrapper similarly like you have for the ExecutorService and use the CompletableFuture.supplyAsync(Supplier, Executor) to return the value? |
Seems reasonable to me. At the very least, couldn't the use of promise.delegate = es instanceof ExecutorService ?
((ExecutorService) es).submit(completingCallable) :
supplyAsync(callableToSupplier(completingCallable), es); when static <T> Supplier<T> callableToSupplier(Callable<? extends T> callable) {
return () -> {
try {
return callable.call();
} catch (RuntimeException ex) {
throw ex;
} catch (Exception ex) {
throw new RuntimeException(ex);
}
};
} |
(@Tembrel The |
@whiskeysierra - oops, thanks, updated comment. This was just a sketch meant to provoke discussion of whether it is worth offering an overload of The question remains: Is this worth doing? |
@paulius-p I'm curious, when creating a @Tembrel thanks for sparking some ideas with this. If we were to support configuring an public CheckedRunnable wrapWithExecutor(Runnable r, Executor executor) {
return () -> {
executor.execute(() -> {
try {
r.run();
} catch (RuntimeException ex) {
throw ex;
} catch (Exception ex) {
throw new RuntimeException(ex);
}
});
};
}
Failsafe.with(policy).run(wrapWithExecutor(this::connect, executor)); @paulius-p Is that basically how you think this should work - simple wrapping? Maybe something like: Failsafe.with(policy).with(executor).run(this::connect); ...but I'm not sure if this use case is too niche and the workaround too straightforward. Input welcome... |
@jhalterman Yes, that's similar to what I had in mind. For the
So, from verbosity perspective it's not that bad, but supporting |
Hey, looking at it again, it's not that hard to get the Maybe you could have a look at this again? Thanks! |
@jhalterman Yes, this should work as well 👍 Thanks for looking into this! |
Closed by #281. |
This is fixed in 2.4.2. |
@paulius-p There may be some additional improvements I'd like to make with this feature. Initially I thought that the Executor would only be used for async executions ( |
@jhalterman My use case only dealt with the async executions, but I think it would be good if it supported sync executions as well 👍 |
Closed by c1f514f |
3.0 has been released, which includes this improvement. |
Sadly this commit broke this feature. Now the execution is not managed by the provided |
@paulius-p Yea, Failsafe 2.4.x allowed an The intent with the impl for this feature was that an Of course, I want to get things working well for you. Can you describe more detail about what Failsafe settings you're using, and where the contextual information is being set? I'm guessing the security context is just being set on a different thread than the execution runs on, so it's not available there. That said, the ThreadLocal<String> threadLocal = new ThreadLocal<>();
Executor executor = runnable -> {
threadLocal.set("contextual info");
runnable.run();
};
Failsafe.none().with(executor).runAsync(() -> {
System.out.println(threadLocal.get());
}).get(); This prints:
I'm not sure if this is how you were using things, but maybe you could describe in more detail. |
@jhalterman Thanks for the reply, though you should be enjoying holidays at this time, this can definitely wait 🎄 It seems it's only affected when using the ThreadLocal<String> threadLocal = new ThreadLocal<>();
Executor executor = runnable -> {
threadLocal.set("contextual info");
runnable.run();
};
System.out.println(Failsafe.none().with(executor).getStageAsync(() -> {
System.out.println(threadLocal.get());
return CompletableFuture.completedFuture("result");
}).join()); |
@paulius-p It turns out I never added support for This test case shows what to expect now. Since |
@jhalterman yes, this partially works now, since it still returns |
@paulius-p So there are two options for approaching this problem:
I'm guessing you'd prefer the second option? When you say things partially works now, is that because using an |
Currently you can provide either
ExecutorService
,ScheduledExecutorService
or implement custom FailsafeScheduler
.It would be good if it would also accept
java.util.concurrent.Executor
interface since it is a common interface returned by other libs. My current use case is in gRPC, when splitting context for multi-threading, and gRPC methods #fixedContextExecutor return baseExecutor
.The text was updated successfully, but these errors were encountered: