Dapr - actors之构建块(10)
若要在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 个参数:
- The name of the reminder.
- The user state provided during registration.
- The invocation due time provided during registration.
- The invocation period provided during registration.
- 提醒的名称。
- 注册期间提供的用户状态。
- 注册期间提供的调用到期时间。
- 注册期间提供的调用周期。
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.RegisterReminderAsync
RegisterTimerAsync
IRemindable.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.cs
USE_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:RegisterExitAsync
RegisterExitAsync
当车辆到达速度相机区域末尾时,退出摄像头调用 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.VehicleState
RegisterEntryAsync
上面的代码当前假定 实例 VehicleState
已由 方法 RegisterEntryAsync
保存。 可以通过首先检查 以确保状态存在来改进代码。 得益于turn-based 的访问模型,代码中不需要显式锁。
- 点赞
- 收藏
- 关注作者
评论(0)