/// <summary> /// Registers this Actor as a subscriber for messages of type <paramref name="messageType"/> using a <see cref="IRelayBrokerActor"/> approach. /// The relay actor will register itself as subscriber to the broker actor, creating a fan out pattern for scalability. /// </summary> /// <param name="actor">The actor registering itself as a subscriber for messages of type <paramref name="messageType"/></param> /// <param name="messageType">The type of message to register for (each message type has its own <see cref="IBrokerActor"/> instance)</param> /// <param name="relayBrokerActorId">The ID of the relay broker to register with. Remember this ID in the caller, if you ever need to unregister.</param> /// <param name="sourceBrokerActorId">(optional) The ID of the source <see cref="IBrokerActor"/> to use as the source for the <paramref name="relayBrokerActorId"/> /// Remember this ID in the caller, if you ever need to unregister. /// If not specified, the default <see cref="IBrokerActor"/> for the message type <paramref name="messageType"/> will be used.</param> /// <returns></returns> public static async Task RegisterMessageTypeWithRelayBrokerAsync(this ActorBase actor, Type messageType, ActorId relayBrokerActorId, ActorId sourceBrokerActorId) { if (actor == null) { throw new ArgumentNullException(nameof(actor)); } if (messageType == null) { throw new ArgumentNullException(nameof(messageType)); } if (relayBrokerActorId == null) { throw new ArgumentNullException(nameof(relayBrokerActorId)); } if (sourceBrokerActorId == null) { sourceBrokerActorId = new ActorId(messageType.FullName); } IRelayBrokerActor relayBrokerActor = ActorProxy.Create <IRelayBrokerActor>(relayBrokerActorId, actor.ApplicationName, nameof(IRelayBrokerActor)); IBrokerActor brokerActor = ActorProxy.Create <IBrokerActor>(sourceBrokerActorId, actor.ApplicationName, nameof(IBrokerActor)); //register relay as subscriber for broker await brokerActor.RegisterSubscriberAsync(ActorReference.Get(relayBrokerActor)); //register caller as subscriber for relay broker await relayBrokerActor.RegisterSubscriberAsync(ActorReference.Get(actor)); }
/// <summary> /// Registers this Actor as a subscriber for messages of type <paramref name="messageType"/>. /// </summary> /// <returns></returns> public static async Task RegisterMessageTypeAsync(this ActorBase actor, Type messageType) { if (messageType == null) { throw new ArgumentNullException(nameof(messageType)); } if (actor == null) { throw new ArgumentNullException(nameof(actor)); } ActorId actorId = new ActorId(messageType.FullName); IBrokerActor brokerActor = ActorProxy.Create <IBrokerActor>(actorId, actor.ApplicationName, nameof(IBrokerActor)); await brokerActor.RegisterSubscriberAsync(ActorReference.Get(actor)); }
/// <summary> /// Registers a service as a subscriber for messages of type <paramref name="messageType"/> using a relay broker. /// </summary> /// <returns></returns> private static async Task RegisterMessageTypeWithRelayBrokerAsync(ServiceContext context, ServicePartitionInformation info, Type messageType, ActorId relayBrokerActorId, ActorId sourceBrokerActorId, string listenerName = null) { var serviceReference = CreateServiceReference(context, info, listenerName); if (sourceBrokerActorId == null) { sourceBrokerActorId = new ActorId(messageType.FullName); } IRelayBrokerActor relayBrokerActor = ActorProxy.Create <IRelayBrokerActor>(relayBrokerActorId, serviceReference.ApplicationName, nameof(IRelayBrokerActor)); IBrokerActor brokerActor = ActorProxy.Create <IBrokerActor>(sourceBrokerActorId, serviceReference.ApplicationName, nameof(IBrokerActor)); //register relay as subscriber for broker await brokerActor.RegisterSubscriberAsync(ActorReference.Get(relayBrokerActor)); //register caller as subscriber for relay broker await relayBrokerActor.RegisterServiceSubscriberAsync(serviceReference); }