首页 > 解决方案 > java.lang.IllegalArgumentException:无法在 Corda 中接收原始类型 [int]

问题描述

我试图使用 receiveAll() 函数在 PartyA(Initiator party) 中接收有效负载。但是遇到了一个错误:

java.lang.IllegalArgumentException:执行流程时无法接收原始类型 [int] 。

启动流程:

@InitiatingFlow
@StartableByRPC
class sendAllMapReceiveAll_IntExampleFlow(private val itemToBeSent : String,private val item2 : Int) : 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)
        val sessionPayloadMap = mapOf(Pair(counterPartySession1,itemToBeSent), Pair(counterPartySession2,item2))
        sendAllMap(sessionPayloadMap)

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

        return receivedBack.toString()
    }
}

响应流程:

@InitiatedBy(sendAllMapReceiveAll_AnyExampleFlow::class)
class SendAllMapReceiveAllAnyResponder(private val counterSession : FlowSession): FlowLogic<Unit>(){
    @Suspendable
    override fun call(){
        val payloadItem  = counterSession.receive<Any>().unwrap { it }
        println("PayLoad Received at Responder = "+ payloadItem)
        counterSession.send(12346)
    }
}

后来为了解决这个错误。我发现这个链接很有帮助。通过在 receiveAll() 中将Int更改为Integer解决了该问题。但是int在单个接收函数中工作,如下所示:

val payloadItem  = counterSession.receive<Int>().unwrap { it }
  1. 为什么原始 int 在 receiveAll() 中不起作用,但同时在单个接收(会话)中起作用?
  2. kotlin 中的其他原始类型在 receiveAll() 中的行为是否也相似?

编辑:再添加一个示例,该示例给了我相同的错误,以解决答案中的混乱。

val receivedBack2 =receiveAllMap(mapOf(Pair(counterPartySession1,String::class.java),
                Pair(counterPartySession2,Int::class.java))).map { it.value.unwrap { it } }

标签: javakotlinblockchaincorda

解决方案


我认为您提供的代码片段不准确。
错误消息清楚地提到您尝试接收原语 int(小写i);在您的代码中,您编写 Int(大写I)。

我认为最初您int的代码中有错误,然后您将其更改为Int发布问题时的错误。

send(), receive(),sendAndReceive()仅适用于类(请参阅此处的文档);所以它将与Intor一起使用Integer;它不适用于int.


推荐阅读