Pub/sub pattern in Azure Service Fabric

后端 未结 1 1029
被撕碎了的回忆
被撕碎了的回忆 2021-01-30 23:49

I\'m working on an Azure Service Fabric application here where I have certain actors that need to receive pings/hooks from other services on demand. The application is a sort of

相关标签:
1条回答
  • 2021-01-31 00:32

    The event subscription actor can implement an event subscription interface that contains an "event-available" method. It can pass that interface to an "subscribe-to-event" method on the event router actor interface.

    The event router actor interface can keep a reference to the subscription interface as part of its state. When the event of interest to the subscriber occurs, it can call the "event-available" method on the interface that it received and saved earlier. All of this can be done without explicitly creating an actor proxy to communicate with the event subscription actor (the actors serialization infrastructure does this under the hood).

    Here's a very basic example that omits the event type, assumes just one subscriber etc., but should give you an idea of the technique.

    Interfaces:

    interface IEventRouter : IActor
    {
        void Subscribe(IEventSubscriber subscriber);
    }
    
    interface IEventSubscriber : IActor
    {
        void EventAvailable();
    }
    

    Event subscriber code:

    class EventSubscriber : Actor, IEventSubscriber
    {
        void SubscribeToEvent()
        {
            IEventRouter router = ActorProxy.Create<IEventRouter>("fabric:/MyApp/MyEventRouterActor");
            router.Subscribe(this);
        }
    
        public void EventAvailable()
        {
            // Process the event
        }
    }
    

    Event router code:

    // Define actor state
    [DataContract]
    class RouterState
    {
        [DataMember]
        public IEventSubscriber Subscriber;
    }
    
    // Define actor
    class EventRouter : Actor<RouterState>, IEventRouter
    {
        public void Subscribe(IEventSubscriber subscriber)
        {
            this.State.Subscriber = subscriber;
        }
    
        void OnEventAvailable()
        {
            this.State.Subscriber.EventAvailable();
        }
    }       
    
    0 讨论(0)
提交回复
热议问题