Actor events

Actor events provide a mechanism for the application to send notifications to the clients using events. However, this Actor events do not guarantee reliable delivery and therefore if a guaranteed message delivery is desired then other notification mechanisms should be used. Due to its unreliable nature, this mechanism should only be used for Actor-client communication and never for Actor-Actor communication.

To use Actor events, you first need to define an event by extending it from the IActorEvents interface. This interface has no members and is only used by the runtime to identify events. All methods in the interface should return void and the parameters should be data contract serializable since they need to be sent to the client over the network:

    public interface IReminderActivatedEvent : IActorEvents 
{
void ReminderActivated(string message);
}

The Actor who publishes this event would need to implement the IActorEventPublisher<T> interface. This interface has no members and is only used by the runtime to identify the event publisher:

internal class HelloWorldActor : Actor, IActorEventPublisher<IReminderActivatedEvent> 
{
...
}

On the client side, you need to declare an event handler, which is the component that will get invoked when an event is triggered. To define the handler, you simply need to implement the Actor event interface:

    public class ReminderHandler : IReminderActivatedEvent 
{
public void ReminderActivated(string message)
{
...
}
}

Finally, using the Actor proxy, the client can register the handler with the event:

var actorClientProxy = ActorProxy.Create<IHelloWorldActor>( 
new ActorId(userName),
"fabric:/HelloWorldActorsApplication");
await actorClientProxy.SubscribeAsync<IReminderActivatedEvent>(new ReminderHandler());

The Actor proxy is a smart component and it abstracts fault handling and name resolution from the client. In case of failure of Actor host node, which leads to Actor migrations to new nodes, the proxy will automatically subscribe to the event again.

To unsubscribe an event, the client can use the UnsubscribeAsync method on the Actor proxy. To trigger an event, an Actor needs to use the GetEvent<T > method to get the event reference and then trigger events by calling methods on the event interface:

    var evt = this.GetEvent<IReminderActivatedEvent>(); 
evt.ReminderActivated(reminderMessage.reminderMessage);
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.67.13