执行组件事件Actor events

执行组件事件提供了一种尽最大努力将通知从执行组件发送到客户端的方法。Actor events provide a way to send best-effort notifications from the actor to the clients. 执行组件事件设计用于从执行组件到客户端的通信,而不应用于从执行组件到执行组件的通信。Actor events are designed for actor-to-client communication and shouldn't be used for actor-to-actor communication.

以下代码段演示如何在应用程序中使用执行组件事件。The following code snippets show how to use actor events in your application.

定义说明由执行组件发布的事件的接口。Define an interface that describes the events published by the actor. 此接口必须派生自 IActorEvents 接口。This interface must be derived from the IActorEvents interface. 方法的参数必须为数据协定可序列化The arguments of the methods must be data contract serializable. 当事件通知是单向且为最佳效果时,方法必须返回 void。The methods must return void, as event notifications are one way and best effort.

public interface IGameEvents : IActorEvents
{
    void GameScoreUpdated(Guid gameId, string currentScore);
}
public interface GameEvents implements ActorEvents
{
    void gameScoreUpdated(UUID gameId, String currentScore);
}

声明由执行组件在执行组件界面中发布的事件。Declare the events published by the actor in the actor interface.

public interface IGameActor : IActor, IActorEventPublisher<IGameEvents>
{
    Task UpdateGameStatus(GameStatus status);

    Task<string> GetGameScore();
}
public interface GameActor extends Actor, ActorEventPublisherE<GameEvents>
{
    CompletableFuture<?> updateGameStatus(GameStatus status);

    CompletableFuture<String> getGameScore();
}

在客户端上,实现事件处理程序。On the client side, implement the event handler.

class GameEventsHandler : IGameEvents
{
    public void GameScoreUpdated(Guid gameId, string currentScore)
    {
        Console.WriteLine(@"Updates: Game: {0}, Score: {1}", gameId, currentScore);
    }
}
class GameEventsHandler implements GameEvents {
    public void gameScoreUpdated(UUID gameId, String currentScore)
    {
        System.out.println("Updates: Game: "+gameId+" ,Score: "+currentScore);
    }
}

在客户端上,为发布事件并订阅其事件的执行组件创建代理。On the client, create a proxy to the actor that publishes the event and subscribe to its events.

var proxy = ActorProxy.Create<IGameActor>(
                    new ActorId(Guid.Parse(arg)), ApplicationName);

await proxy.SubscribeAsync<IGameEvents>(new GameEventsHandler());
GameActor actorProxy = ActorProxyBase.create<GameActor>(GameActor.class, new ActorId(UUID.fromString(args)));

return ActorProxyEventUtility.subscribeAsync(actorProxy, new GameEventsHandler());

如果发生故障转移,执行组件可能会故障转移到不同的进程或节点。In the event of failovers, the actor may fail over to a different process or node. 执行组件代理管理活动的订阅,并自动重新订阅它们。The actor proxy manages the active subscriptions and automatically re-subscribes them. 可以通过 ActorProxyEventExtensions.SubscribeAsync<TEvent> API 控制重新订阅的间隔。You can control the re-subscription interval through the ActorProxyEventExtensions.SubscribeAsync<TEvent> API. 若要取消订阅,请使用 ActorProxyEventExtensions.UnsubscribeAsync<TEvent> API。To unsubscribe, use the ActorProxyEventExtensions.UnsubscribeAsync<TEvent> API.

在执行组件上,在事件发生时发布事件。On the actor, publish the events as they happen. 如果有订阅此事件的用户,那么执行组件运行时会向他们发送通知。If there are subscribers to the event, the Actors runtime sends them the notification.

var ev = GetEvent<IGameEvents>();
ev.GameScoreUpdated(Id.GetGuidId(), score);
GameEvents event = getEvent<GameEvents>(GameEvents.class);
event.gameScoreUpdated(Id.getUUIDId(), score);

后续步骤Next steps