预定 activity 永远不会 运行

Scheduled activity is never run

我定义了一个SchedulableState如下:

class MySchedulableState() : SchedulableState {
    override val participants = listOf<Party>()

    val nextActivityTime = Instant.ofEpochMilli(Instant.now().toEpochMilli() + 100)

    override fun nextScheduledActivity(thisStateRef: StateRef, flowLogicRefFactory: FlowLogicRefFactory): ScheduledActivity? {
        return ScheduledActivity(flowLogicRefFactory.create("com.template.ScheduledFlow", thisStateRef), nextActivityTime)
    }
}

但是,当我在流程中创建此状态时,计划的 activity 永远不会 运行。 Wh

问题是您的节点每次从保管库中提取状态时都使用状态的构造函数来重新创建状态。作为构建状态的一部分,Instant.now() 被再次调用并分配给 nextActivityTime,将计划的事件推到未来。

相反,您应该按如下方式定义 SchedulableState

class MySchedulableState(val now: Instant) : SchedulableState {
    override val participants = listOf<Party>()

    val nextActivityTime = Instant.ofEpochMilli(now.toEpochMilli() + 100)

    override fun nextScheduledActivity(thisStateRef: StateRef, flowLogicRefFactory: FlowLogicRefFactory): ScheduledActivity? {
        return ScheduledActivity(flowLogicRefFactory.create("com.template.ScheduledFlow", thisStateRef), nextActivityTime)
    }
}

注意我们如何在构造函数中传递当前时间。这个值不会在每次重构状态时改变(注意它必须是一个 val 以确保它被序列化)。