问题描述
是否可以从由InitiatedBy注释的流到也由InitiatedBy注释的流发起流会话?
Is it possible to initiate a flow session from a flow that is annoted with InitiatedBy to a flow which is also annoted with InitiatedBy?
例如:
@InitiatingFlow
类流A
@InitiatedBy(FlowA.class)
类FlowB
@InitiatedBy(FlowB。 class)
FlowC类
是否有可能实现流会话的顺序,例如:
A-> B- > C?
is it possible to achieve sequence of flow session like:A->B->C ?
推荐答案
是的,这是可能的,如下所示:
Yes, this is possible, as follows:
@InitiatingFlow
@StartableByRPC
class Initiator(val firstCounterparty: Party, val secondCounterparty: Party) : FlowLogic<Int>() {
override val progressTracker = ProgressTracker()
@Suspendable
override fun call(): Int {
val flowSession = initiateFlow(firstCounterparty)
flowSession.send(secondCounterparty)
return flowSession.receive<Int>().unwrap { it }
}
}
@InitiatingFlow
@InitiatedBy(Initiator::class)
class Responder(val flowSession: FlowSession) : FlowLogic<Unit>() {
@Suspendable
override fun call() {
val secondCounterparty = flowSession.receive<Party>().unwrap { it }
val newFlowSession = initiateFlow(secondCounterparty)
val int = newFlowSession.receive<Int>().unwrap { it }
flowSession.send(int)
}
}
@InitiatingFlow
@InitiatedBy(Responder::class)
class ResponderResponder(val flowSession: FlowSession) : FlowLogic<Unit>() {
@Suspendable
override fun call() {
flowSession.send(3)
}
}
但是,有一个主要警告。在Corda 3.x中,您不能有两个 FlowSession
同一交易对手在同一流程中。因此,要么您需要禁止A-> B-> A,如下所示:
However, there is one major caveat. In Corda 3.x, you can't have two FlowSession
s with the same counterparty in the same flow. So either you need to disallow the case where A -> B -> A, as follows:
@InitiatingFlow
@StartableByRPC
class Initiator(val firstCounterparty: Party, val secondCounterparty: Party) : FlowLogic<Int>() {
override val progressTracker = ProgressTracker()
@Suspendable
override fun call(): Int {
if (secondCounterparty == ourIdentity) {
throw FlowException("In Corda 3.x, you can't have two flow sessions with the same party.")
}
val flowSession = initiateFlow(firstCounterparty)
flowSession.send(secondCounterparty)
return flowSession.receive<Int>().unwrap { it }
}
}
@InitiatingFlow
@InitiatedBy(Initiator::class)
class Responder(val flowSession: FlowSession) : FlowLogic<Unit>() {
@Suspendable
override fun call() {
val secondCounterparty = flowSession.receive<Party>().unwrap { it }
if (secondCounterparty == flowSession.counterparty) {
throw FlowException("In Corda 3.x, you can't have two flow sessions with the same party.")
}
val newFlowSession = initiateFlow(secondCounterparty)
val int = newFlowSession.receive<Int>().unwrap { it }
flowSession.send(int)
}
}
@InitiatingFlow
@InitiatedBy(Responder::class)
class ResponderResponder(val flowSession: FlowSession) : FlowLogic<Unit>() {
@Suspendable
override fun call() {
flowSession.send(3)
}
}
或者您需要进入中的
,然后开始启动 InitiatingFlow
子流响应程序 ResponderResponder
的流程,如下所示:
Or you need to drop into an InitiatingFlow
subflow in Responder
before starting the flow that starts ResponderResponder
, as follows:
@InitiatingFlow
@StartableByRPC
class Initiator(val firstCounterparty: Party, val secondCounterparty: Party) : FlowLogic<Int>() {
override val progressTracker = ProgressTracker()
@Suspendable
override fun call(): Int {
val flowSession = initiateFlow(firstCounterparty)
flowSession.send(secondCounterparty)
return flowSession.receive<Int>().unwrap { it }
}
}
@InitiatingFlow
@InitiatedBy(Initiator::class)
class Responder(val flowSession: FlowSession) : FlowLogic<Unit>() {
@Suspendable
override fun call() {
val secondCounterparty = flowSession.receive<Party>().unwrap { it }
val int = subFlow(ResponderInitiator(secondCounterparty))
flowSession.send(int)
}
}
@InitiatingFlow
class ResponderInitiator(val counterparty: Party) : FlowLogic<Int>() {
@Suspendable
override fun call(): Int {
val flowSession = initiateFlow(counterparty)
return flowSession.receive<Int>().unwrap { it }
}
}
@InitiatingFlow
@InitiatedBy(ResponderInitiator::class)
class ResponderResponder(val flowSession: FlowSession) : FlowLogic<Unit>() {
@Suspendable
override fun call() {
flowSession.send(3)
}
}
这篇关于从使用InitiatedBy注释的流到也是InitiatedBy的流启动流会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!