Dapr - actors之构建块(10)

举报
小云悠悠zZ 发表于 2023/01/30 16:41:28 2023/01/30
【摘要】 上面的代码当前假定 实例 VehicleState 已由 方法 RegisterEntryAsync 保存。 可以通过首先检查 以确保状态存在来改进代码。 得益于turn-based 的访问模型,代码中不需要显式锁。

若要在Actor 中使用提醒,Actor 类必须实现 IRemindable 接口:

C#

public interface IRemindable
{
    Task ReceiveReminderAsync(
        string reminderName, byte[] state,
        TimeSpan dueTime, TimeSpan period);
}

The method is called when a reminder is fired. It takes 4 arguments:ReceiveReminderAsync

触发提醒时调用ReceiveReminderAsync 方法。 它采用 4 个参数:

  1. The name of the reminder.
  2. The user state provided during registration.
  3. The invocation due time provided during registration.
  4. The invocation period provided during registration.
  1. 提醒的名称。
  2. 注册期间提供的用户状态。
  3. 注册期间提供的调用到期时间。
  4. 注册期间提供的调用周期。

To register a reminder, use the method of the actor base class. The following example sets a reminder to fire a single time with a due time of three minutes.RegisterReminderAsync

若要注册提醒,请使用 Actor基类的 方法RegisterReminderAsync 。 以下示例设置一个提醒,以在到期时间为 3 分钟时发送一次。

C#

public class ReminderActor : Actor, IReminderActor, IRemindable
{
    public ReminderActor(ActorHost host) : base(host)
    {
    }

    public Task SetReminderAsync(string text)
    {
        return RegisterReminderAsync(
            "DoNotForget",
            Encoding.UTF8.GetBytes(text),
            TimeSpan.FromSeconds(3),
            TimeSpan.FromMilliseconds(-1));
    }

    public Task ReceiveReminderAsync(
        string reminderName, byte[] state,
        TimeSpan dueTime, TimeSpan period)
    {
        if (reminderName == "DoNotForget")
        {
            var text = Encoding.UTF8.GetString(state);

            Logger.LogInformation($"Don't forget: {text}");
        }

        return Task.CompletedTask;
    }
}

The method is similar to but you don't have to specify a callback method explicitly. As the above example shows, you implement to handle fired reminders.RegisterReminderAsyncRegisterTimerAsyncIRemindable.ReceiveReminderAsync

RegisterReminderAsync方法类似于 RegisterTimerAsync ,但不必显式指定回调方法。 如上面的示例所示,实现 IRemindable.ReceiveReminderAsync 以处理触发的提醒。

Reminders both reset the idle timer and are persistent. Even if your actor is deactivated, it will be reactivated at the moment a reminder fires. To stop a reminder from firing, call .UnregisterReminderAsync

提醒同时重置空闲计时器和持久性。 即使Actor 已停用,也会在触发提醒时重新激活。 若要停止触发提醒,请调用 UnregisterReminderAsync 

Sample application: Dapr Traffic Control

示例应用程序:Dapr 交通控制

The default version of Dapr Traffic Control does not use the actor model. However, it does contain an alternative actor-based implementation of the TrafficControl service that you can enable. To make use of actors in the TrafficControl service, open up the file and uncomment the statement at the top of the file:src/TrafficControlService/Controllers/TrafficController.csUSE_ACTORMODEL

Dapr 交通控制的默认版本不使用Actor 模型。 但是,它确实包含可以启用的 TrafficControl 服务的基于Actor 的替代实现。 若要使用 TrafficControl 服务中的Actor ,请打开 文件并取消注释文件 src/TrafficControlService/Controllers/TrafficController.cs 顶部的 USE_ACTORMODEL 语句:

C#

#define USE_ACTORMODEL

When the actor model is enabled, the application uses actors to represent vehicles. The operations that can be invoked on the vehicle actors are defined in an interface:IVehicleActor

启用Actor模型后,应用程序将使用Actor 来表示车辆。 可在车辆Actor 上调用的操作在接口中 IVehicleActor 定义:

C#

public interface IVehicleActor : IActor
{
    Task RegisterEntryAsync(VehicleRegistered msg);
    Task RegisterExitAsync(VehicleRegistered msg);
}

The (simulated) entry cameras call the method when a new vehicle is first detected in the lane. The only responsibility of this method is storing the entry timestamp in the actor state:RegisterEntryAsync

首次在 (检测到) 车辆时,模拟入口相机会 调用 RegisterEntryAsync 方法。 此方法的唯一职责是在actor 状态中存储条目时间戳:

C#

var vehicleState = new VehicleState
{
    LicenseNumber = msg.LicenseNumber,
    EntryTimestamp = msg.Timestamp
};
await StateManager.SetStateAsync("VehicleState", vehicleState);

When the vehicle reaches the end of the speed camera zone, the exit camera calls the method. The method first gets the current states and updates it to include the exit timestamp:RegisterExitAsyncRegisterExitAsync

当车辆到达速度相机区域末尾时,退出摄像头调用 RegisterExitAsync 方法。 RegisterExitAsync方法首先获取当前状态并更新它以包括退出时间戳:

C#

var vehicleState = await StateManager.GetStateAsync<VehicleState>("VehicleState");
vehicleState.ExitTimestamp = msg.Timestamp;

Note

备注

The code above currently assumes that a instance has already been saved by the method. The code could be improved by first checking to make sure the state exists. Thanks to the turn-based access model, no explicit locks are required in the code.VehicleStateRegisterEntryAsync

上面的代码当前假定 实例 VehicleState 已由 方法 RegisterEntryAsync 保存。 可以通过首先检查 以确保状态存在来改进代码。 得益于turn-based 的访问模型,代码中不需要显式锁。

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。