问题
How do we implement schedulable state in corda? In my case i need to issue a monthly statement, so can schedulablestate be used for this?
回答1:
There are a number of things you need to do.
Firstly, your state object needs to implement the SchedulableState
interface. It adds an additional method:
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?
}
This interface requires a method named nextScheduledActivity
to be implemented which returns an optional ScheduledActivity
instance. ScheduledActivity
captures what FlowLogic
instance each node will run, to perform the activity, and when it will run is described by a java.time.Instant
. Once your state implements this interface and is tracked by the vault, it can expect to be queried for the next activity when committed to the vault. Example:
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)
}
}
Secondly, the FlowLogic
class which is schedulted to start (in this case FlowToStart
) must be also annotated with @SchedulableFlow.
E.g.
@InitiatingFlow
@SchedulableFlow
class FlowToStart : FlowLogic<Unit>() {
@Suspendable
override fun call() {
// Do stuff.
}
}
Now, when ExampleState
is stored in the vault, the FlowToStart
will be schedulted to start at the offset time specified in the ExampleState
.
That's it!
来源:https://stackoverflow.com/questions/45712795/implementing-schedulable-states-in-corda