【问题标题】:akka system.scheduler().scheduleAtFixedRate can i change value of Duration.ofMinutes at run time?akka system.scheduler().scheduleAtFixedRate 我可以在运行时更改 Duration.ofMinutes 的值吗?
【发布时间】:2021-04-16 21:23:10
【问题描述】:

下面的代码在主类的INIT方法中

system.scheduler()
.scheduleAtFixedRate(Duration.ZERO,
Duration.ofMinutes(settings.getInterval()),
readActor,TriggerBatch.fromTimer(),
system.dispatcher(),
ActorRef.noSender());

一旦业务逻辑被执行,即 receiveBuilder() 方法,那么 我需要在运行时更改 Duration.ofMinutes 值

我尝试了以下解决方案

  solution 1. 
  Config config = new CachingConfig(ConfigFactory.load());
  Config config1 = config.withValue("service- 
  name.scheduler.interval",ConfigValueFactory.fromAnyRef("20"));// in application.conf value for interval is =1 
  config1.getInt("service-name.scheduler.interval"); 

solution 2. 
Config overrides = ConfigFactory.parseString("service-name.scheduler.interval=20");
Config actualConfig = overrides.withFallback(ConfigFactory.load());
actualConfig.getInt("service-name.scheduler.interval");

此解决方案有效,但调度程序没有采用更新的值,它仍然采用旧的值,即 service-name.scheduler.interval = 1

【问题讨论】:

    标签: java akka akka-stream akka-http


    【解决方案1】:

    有几个选项,假设您控制调用 system.scheduler().scheduleAtFixedRate 的 init 方法。

    首先,调用scheduleAtFixedRate 会给你一个Cancellable 回复。因此,您可以cancel Cancellable 并为调度设置新的间隔。您需要将Cancellable 传递给actor,以便可以在receiveBuilder 方法中访问它(您可以在创建actor 时通过Props 传递它,或者通过发送给actor 的消息)。

    作为替代方案,您可以让 init 生成一个 actor 来管理消息的预定发送。该参与者的状态将是来自scheduleAtFixedRateCancellable,您可以向它发送消息以更改发送之间的间隔。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-07
      • 1970-01-01
      • 2011-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多