【问题标题】:Spring @Scheduled with cron, how do I handle if the service is down on the scheduled daySpring @Scheduled with cron,如果服务在预定日期关闭,我该如何处理
【发布时间】:2021-02-09 09:39:06
【问题描述】:

假设我的系统要求您在开始使用之前添加一些余额。因此,假设它会在每个月的第 30 天扣除/支出您的余额。所以我使用 AxonFramework 结合 spring Scheduled annotation 所以代码可能看起来像这样。

@CommandHandler
@Scheduled(cron = "** cron for every 30th **")
fun handle(command: ExpendNetflixUserCommand){
  if (user.balance < netflixPackage.price){
    AggregateLifecycle.apply(command.toNetflixUserDisabled())
    throw IllegalArgumentException("Insufficient balance, please add.")
  }
  AggregateLifecycle.apply(command.toNetflixUserExpended())
}

所以我的问题是,如果它在 30 日并且我的系统即将计算这些付款,但突然服务中断/中断并且它在 31 日重新开始工作,它会重新计算之前的付款吗?所以如果不能,你能建议我应该如何处理这种情况。

【问题讨论】:

    标签: spring spring-boot kotlin axon


    【解决方案1】:

    对于这个用例,我可能会有一个用@Schedule 注释的外部组件,它将使用CommandGateway 的实例来调度命令! 使用这种方法,这个组件在您的代码之外并带来额外的好处。 其中之一是可以在此 CommandGateway 上配置 RetryScheduler,它将根据需要重试失败的命令!

    查看参考指南以获取有关如何执行此操作以及 Axon 为您提供开箱即用的实现的更多信息! https://docs.axoniq.io/reference-guide/axon-framework/axon-framework-commands/implementations#configuring-the-command-gateway

    【讨论】:

      【解决方案2】:

      您可能应该考虑使用更成熟的解决方案,例如 Spring Batch,因为您可以立即使用诸如工作状态管理之类的东西。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-09-21
        • 1970-01-01
        • 2021-02-27
        • 2020-12-25
        • 2011-02-14
        • 2010-11-27
        • 1970-01-01
        相关资源
        最近更新 更多