首页 > 解决方案 > ReceiveAll() 在 Corda 中的应用是什么?如何实施?

问题描述

有没有实现 sendAll() 和 receiveAll() 的示例?

接收所有(接收类型:类,会话:列表):列表<UntrustworthyData>

文档中它说:从传入列表中指定的所有FlowSession对象接收。接收的类型必须相同。

它是否在响应程序流中使用?如果是这样,如何在receiveAll()中输入所需的会话作为列表?

标签: kotlinblockchaincorda

解决方案


receiveAll() & sendAll() 实际上是 4.5 版本的性能升级。

这里举两个例子:

  • 如果您想从 A 向 B/C 发送一条启动新会话的消息,您将对 B 和 C 使用 initialFlow,然后从 A 方执行sendAll()。在 B 和 C 的响应者端,您将做一个简单的接收。
  • 如果您想在 A 方中接收来自 B 和 C 的并行消息(例如在建立会话之后),您可以从 B 和 C 执行普通发送,并从 A执行receiveAll() 。

发起人流程:

@InitiatingFlow
@StartableByRPC
class sendAllReceiveAllExampleFlow(private val itemToBeSent : String) : FlowLogic<String>(){
    @Suspendable
    override fun call() : String {
        val counterParty1 = serviceHub.identityService.partiesFromName("PartyB",false).single()
        val counterParty2 = serviceHub.identityService.partiesFromName("PartyC",false).single()

        val counterPartySession1 = initiateFlow(counterParty1)
        val counterPartySession2 = initiateFlow(counterParty2)

        sendAll(itemToBeSent, setOf(counterPartySession1,counterPartySession2))

        val receivedBack= receiveAll(String::class.java,listOf(counterPartySession1,
counterPartySession2)).map { it.unwrap { it } }

  
       return "receivedBack :" + receivedBack.toString()
    }
}

响应流程:

class SendAllReceiveAllResponder(private val counterSessionList : FlowSession): FlowLogic<Unit>(){
    @Suspendable
    override fun call(){
        println("Inside Responder ")
        val receivedString = counterSessionList.receive<String>().unwrap { it }
        println("PayLoad Received at Responder = "+ receivedString)
        counterSessionList.send(receivedString + ourIdentity.name.organisation)
    }
}

案例场景来源:CordaLedger-Slack


推荐阅读