Service Fabric: Event notifications in Actor Model

Damir Dobric Posts

Next talks:

 

    

Follow me on Twitter: #ddobric



 

 

Archives

Actor Model in Service Fabric provides a feature called Event Notifications or Actor Events. This is useful when one actor wants to notify another actors or actor clients about some event like status change or similar. This is commonly very difficult to implement by using common approaches in distributed systems. You need to take care about concurrency, location transparency etc. Following post describes how to implement actor code, which fires notifications and how to implement the client, which runs in an actor or any code in cluster, which receives notifications.

To implement actor events (notifications) do following steps. 

Implement Interface for events

Go to interfaces project of the actor, which should fire events and implement interfaces. The method, which fires event has typically no return arguments. Notice also, that is is not an async method.

 
    public interface IMailSenderEvents: IActorEvents

    {
       
void StatusChange(string oldStatus, DateTime? oldStatusTime,
                          string newStatus, DateTime
newStatusTime);
    }

Fire Event

As next, let’s implement the code, which will fire the event. This code should be implemented in actor methods. To do this, use a helper method GetEvent<>. This event returns the instance of the statically typed proxy, which will fire event.

  
  
var evNotifier = GetEvent<IMailSenderEvents
>();
   evNotifier.StatusChange(
,,,,
);

You do not have to care about the location of the actor or client which has subscribed the notification. The runtime will dispatch event for you to all of subscribes.

Implementing Subscriber

Subscriber of an Actor Event can be any application, which is running inside of Service Fabric cluster, which hosts the actor, which fires event. First create the proxy of the actor. Then invoke SubscribeAsync<> method on the proxy instance.

IMailSender sender = ActorProxy.Create<IMailSender>(new ActorId(Id));

sender.SubscribeAsync<
IMailSenderEvents>(new StatusChangeEventHandler(this
.State));

And finally we need an implementation of event handler.

using MailSender.Interfaces;
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
using
MailSender;
using
System.Diagnostics;

namespace
SpamManager
{
   
/// <summary>

   
/// Event Handler for notifications sent by actor <see cref="MailSender"/>.
   
/// </summary>
   
public class StatusChangeEventHandler : IMailSenderEvents
    {
       
private SpamManagerState m_State;

       
public StatusChangeEventHandler(SpamManagerState
state)
        {
            m_State = state;
        }


       
/// <summary>

       
/// Invoked from runtime when status of an actor is changed.
       
/// </summary>
       
/// <param name="state">Full state of the mail sender.</param>
       
public void StatusChange(MailSenderState state)
        {
           
           . . .
        }
    }

}


Posted Nov 17 2015, 09:59 AM by Damir Dobric
developers.de is a .Net Community Blog powered by daenet GmbH.