ReceiveAll()在Corda中的应用是什么?如何实施?

What is the application of ReceiveAll() in Corda? How to implement it?

有没有实现 sentAll() 和 receiveAll() 的例子?

receiveAll(receiveType: Class, sessions: List): List<UntrustworthyData>

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

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

receiveAll() & sendAll() are actually performance upgrades in 4.5 version.

这里举两个例子:

  • 如果您想从 A 向 B/C 发送消息以启动新会话,您将为 B 和 C 使用 initiateFlow,然后执行 sendAll() 来自 A 方。在 B 和 C 的响应方,您将进行普通接收。
  • 如果你想从 A 方的 B 和 C 并行接收消息(比如在建立会话之后),你可以从 B 和 C 发送一个简单的消息,然后 receiveAll() 来自 A.

发起者流程:

@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

您还可以查看此单元测试 - FlowParallelMessagingTests.kt - 在 github 上的 Corda 存储库中。

此外,要补充一些关于此 API 的内容,有两个 API 允许您同时向多个会话发送消息:

  • SendAll() 一起,您向所有会话发送 相同的负载 ,如前一条评论中所示;
  • 使用 SendAllMap() 您可以同时向不同的会话发送不同的负载。我在上面提供的 link 中有一个示例。