问题
Whats the best practice here to annotate a method as @Suspendable
? In a Flow, there may be multiple private methods that query the vault/ compute business logic. Should these be annotated with @Suspendable
so it can recover if a node crashes midway?
Or is @Suspendable
only for methods where send
/ sendAndReceived
are involved where its waiting for responses from counterparties?
回答1:
From paralleluniverse:
The run methods in Fiber, SuspendableRunnable, and SuspendableCallable declare that they may throw a SuspendExecution exception.
@Suspendable is our way to specify a suspendable method is by declaring throws SuspendExecution.This is convenient because SuspendExecution is a checked exception, so if f calls g and g is suspendable, the Java compiler will force us to declare that f is suspendable (and it must be because it calls g and g might be suspended).
Sometimes, however, we cannot declare f to throw SuspendExecution. One example is that f is an implementation of an interface method, and we cannot (or don’t want to) change the interface so that it throws SuspendExecution.
So, suppose method f is declared in interface I, and we’d like to make its implementation in class C suspendable. The compiler will not let us declare that we throw SuspendExecution because that will conflict with f’s declaration in I.
What we do, then, is annotate C.f with the @Suspendable annotation (in the co.paralleluniverse.fibers package). Assuming C.f calls park or some other suspendable method g – which does declare throws SuspendExecution, we need to surround f’s body with try {} catch(SuspendExecution) just so the method will compile.
if we want to run h in a fiber, then it must be suspendable because it calls f which is suspendable. We could designate h as suspendable either by annotating it with @Suspendable or by declaring SuspendExecution
If we want any method to run in a fiber, then it must be suspendable.
so basically, any method that calls a method which can throw SuspendExecution or is annotated with @Suspendable
.
In my case I faced error calling SubFlow
from a function, since I didn't annotate it with @Suspendable
I faced Quasar Exceptions. As SubFlow
is annotated with @Suspendable
annotating my function helped get rid of those errors.
Error: Uninstrumented whole methods ('**') or single calls ('!!') detected:
来源:https://stackoverflow.com/questions/51112777/when-to-add-suspendable-to-methods-in-a-flow