将方法注释为 @Suspendable
的最佳实践是什么?在 Flow 中,可能有多个查询 vault/compute 业务逻辑的私有(private)方法。这些是否应该用 @Suspendable
注释,以便它可以在节点中途崩溃时恢复?
或者 @Suspendable
仅适用于涉及 send
/sendAndReceived
的方法,它等待交易对手的响应?
最佳答案
来自 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
如果我们希望任何方法在纤程中运行,那么它必须是可挂起的。
所以基本上,任何调用可以抛出 SuspendExecution 或使用 @Suspendable
注释的方法的方法。
在我的例子中,我遇到了从函数调用 SubFlow
的错误,因为我没有用 @Suspendable
注释它,我遇到了 Quasar 异常。由于 SubFlow
是用 @Suspendable
注释的,因此注释我的函数有助于消除这些错误。
错误:检测到未检测的整个方法(“**”)或单个调用(“!!”):
关于corda - 何时将 @Suspendable 添加到流程中的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51112777/