Dapr - actors之构建块(9)
Call non-.NET actors
调用非.NET actors
So far, the examples used strongly-typed actor proxies based on .NET interfaces to illustrate actor invocations. This works great when both the actor host and client are .NET applications. However, if the actor host is not a .NET application, you don't have an actor interface to create a strongly-typed proxy. In these cases, you can use a weakly-typed proxy.
到目前为止,这些示例使用基于 .NET 接口的强类型Actor 代理来说明actor 调用。 当actor 主机和客户端都是 .NET 应用程序时,这非常有效。 但是,如果actor 主机不是 .NET 应用程序,则没有创建强类型代理的actor 接口。 在这些情况下,可以使用弱类型代理。
You create weakly-typed proxies in a similar way to strongly-typed proxies. Instead of relying on a .NET interface, you need to pass in the actor method name as a string.
创建弱类型代理的方式与强类型代理类似。 需要将actor 方法名称作为字符串传递,而不是依赖于 .NET 接口。
C#
[HttpPut("{scoreId}")]
public Task<int> IncrementAsync(string scoreId)
{
var scoreActor = _actorProxyFactory.CreateActorProxy(
new ActorId(scoreId),
"ScoreActor");
return scoreActor("IncrementScoreAsync");
}
Timers and reminders
计时器和提醒
Use the method of the base class to schedule actor timers. In the following example, a exposes a method. Clients can call the method to start a timer that repeatedly writes a given text to the log output.RegisterTimerAsync
Actor
TimerActor
StartTimerAsync
使用 RegisterTimerAsync
基类的 Actor
方法计划actor 计时器。 在下面的示例中, TimerActor
公开 StartTimerAsync
方法。 客户端可以调用 方法来启动一个计时器,该计时器将给定的文本重复写入日志输出。
C#
public class TimerActor : Actor, ITimerActor
{
public TimerActor(ActorHost host) : base(host)
{
}
public Task StartTimerAsync(string name, string text)
{
return RegisterTimerAsync(
name,
nameof(TimerCallback),
Encoding.UTF8.GetBytes(text),
TimeSpan.Zero,
TimeSpan.FromSeconds(3));
}
public Task TimerCallbackAsync(byte[] state)
{
var text = Encoding.UTF8.GetString(state);
Logger.LogInformation($"Timer fired: {text}");
return Task.CompletedTask;
}
}
The method calls to schedule the timer. takes five arguments:StartTimerAsync
RegisterTimerAsync
RegisterTimerAsync
StartTimerAsync
方法调用 RegisterTimerAsync
来调度计时器。 RegisterTimerAsync
采用五个参数:
- The name of the timer.
- The name of the method to call when the timer fires.
- The state to pass to the callback method.
- The amount of time to wait before the callback method is first invoked.
- The time interval between callback method invocations. You can specify to disable periodic signaling.
TimeSpan.FromMilliseconds(-1)
- 计时器的名称。
- 触发计时器时要调用的方法的名称。
- 要传递给回调方法的状态。
- 首次调用回调方法之前要等待的时间。
- 回调方法调用之间的时间间隔。 可以指定 以
TimeSpan.FromMilliseconds(-1)
禁用定期信号。
The method receives the user state in binary form. In the example, the callback decodes the state back to a before writing it to the log.TimerCallbackAsync
string
TimerCallbackAsync
方法以二进制形式接收用户状态。 在示例中,回调在将状态写入日志之前将状态 string
解码 。
Timers can be stopped by calling :UnregisterTimerAsync
可以通过调用 UnregisterTimerAsync
来停止计时器 :
C#
public class TimerActor : Actor, ITimerActor
{
// ...
public Task StopTimerAsync(string name)
{
return UnregisterTimerAsync(name);
}
}
Remember that timers do not reset the actor idle timer. When no other calls are made on the actor, it may be deactivated and the timer will be stopped automatically. To schedule work that does reset the idle timer, use reminders which we'll look at next.
请记住,计时器不会重置Actor空闲计时器。 当actor 上未进行其他调用时,可能会停用该Actor,并且计时器将自动停止。 若要计划重置空闲计时器的工作,请使用我们接下来将查看的提醒。
To use reminders in an actor, your actor class must implement the interface:IRemindable
- 点赞
- 收藏
- 关注作者
评论(0)