2017-08-16 33 views
1

我們如何在corda中實現可調度狀態?在我的情況下,我需要發佈每月的聲明,所以可以使用schedulablestate來做到這一點?在corda中實現可調度狀態

+0

這是一個有趣的問題,我正在研究一個類似的用例,但我還沒有在實際的實施,但是,schedulableState是你需要的 –

回答

4

有很多事情你需要做。

首先,您的狀態對象需要實現SchedulableState接口。它增加了一個額外的方法:

interface SchedulableState : ContractState { 
    /** 
    * Indicate whether there is some activity to be performed at some future point in time with respect to this 
    * [ContractState], what that activity is and at what point in time it should be initiated. 
    * This can be used to implement deadlines for payment or processing of financial instruments according to a schedule. 
    * 
    * The state has no reference to it's own StateRef, so supply that for use as input to any FlowLogic constructed. 
    * 
    * @return null if there is no activity to schedule. 
    */ 
    fun nextScheduledActivity(thisStateRef: StateRef, flowLogicRefFactory: FlowLogicRefFactory): ScheduledActivity? 
} 

該接口需要被實現,它返回一個可選ScheduledActivity實例名爲nextScheduledActivity的方法。 ScheduledActivity捕獲每個節點將運行的實例,執行該活動,以及何時將運行由java.time.Instant描述。一旦你的狀態實現了這個界面並且被Vault跟蹤,它可以期待在下一個活動被提交到Vault時被查詢。例如:

class ExampleState(val initiator: Party, 
        val requestTime: Instant, 
        val delay: Long) : SchedulableState { 
    override val contract: Contract get() = DUMMY_PROGRAM_ID 
    override val participants: List<AbstractParty> get() = listOf(initiator) 
    override fun nextScheduledActivity(thisStateRef: StateRef, flowLogicRefFactory: FlowLogicRefFactory): ScheduledActivity? { 
     val responseTime = requestTime.plusSeconds(delay) 
     val flowRef = flowLogicRefFactory.create(FlowToStart::class.java) 
     return ScheduledActivity(flowRef, responseTime) 
    } 
} 

其次,這是schedulted開始(在這種情況下FlowToStart)的FlowLogic類也必須標註了@SchedulableFlow.例如

@InitiatingFlow 
@SchedulableFlow 
class FlowToStart : FlowLogic<Unit>() { 
    @Suspendable 
    override fun call() { 
     // Do stuff. 
    } 
} 

現在,當ExampleState存儲在庫中,FlowToStart將schedulted在ExampleState指定的偏移時間啓動。

就是這樣!

+0

在我的情況下,我需要創建基於委託人父母國家的金額。所以,我的父母狀態將是可調度狀態,它每月安排一個流量來創建月度報表(子狀態)。我對嗎? – Raghuram

+0

完全正確。你實現可調度狀態,並延遲一個月的時間。然後,當定時器觸發時,發出語句的流程將被執行。歡呼聲 –

+0

可調度狀態是2個節點之間的一個tx的一部分,並且此狀態駐留在雙方的節點中。在這種情況下,調度程序流程將在兩個節點中被觸發。但我希望它只在一個節點中被觸發。可能嗎? – Raghuram