Akka.net 调度程序只发送第一个值而不更新它,如何解决

Akka.net scheduler sends just first value and doesn't update it, How fix it

我找不到一个有趣的时刻的答案。 在 akka.net 我有调度程序。它将在整理出数字的演员中工作。

这里是一个简单的实现

_statusScheduler = Context.System.Scheduler.ScheduleTellRepeatedlyCancelable(
            TimeSpan.FromSeconds(_shedulerInterval),
            TimeSpan.FromSeconds(_shedulerInterval),
            _reporterActor,
            new ProgressReport(requestId, _testedQuantity),
            Self);

where

_shedulerInterval - 5-second interval,

_testedQuantity - quantity of tested number all time updated.

5秒后发送0;总是,不是一个改变的数字。这里有一个问题:是否可以发送更新的数量?

我无法通过 Recieve<> 方法将消息发送到更新数量,因为我的 actor 正在处理计数消息并且它一直在计算数量并更新它(当它完成时它会收到下一条消息)。但是所有五秒钟我都应该由调度程序生成报告。可以修复吗?

我认为现在我需要发送所有逻辑,因为它工作正常,我的问题是调度程序行为。

这里的问题是您传递给调度程序的消息 new ProgressReport(requestId, _testedQuantity) 是每次要发送的消息。由于您按值传递这些整数值,因此该对象将在您创建消息时具有这些字段的原始值,因此消息将永远不会更新。

如果您希望能够更改在循环调度程序中发送的内容,请改为执行以下操作:

var self = Self; // need a closure here, since ActorContext won't be available
_statusScheduler = Context.System.Scheduler.Advanced.ScheduleRepeatedlyCancelable(interval, interval, () => {
  _reporterActor.Tell(new ProgressReport(requestId, _testedQuantity), self);
});

调度程序的这种用法将在每次调用 lambda 函数时生成一条新消息,因此您将能够在对象中包含更新的整数值。