Point-to-Point Mode
In this mode, a message is delivered to one destination actor with matching path in the cluster using DistributedPubSubMediator
. If there are several entries with matching path, message will be delivered using the specified routing logic.
Registration
You will have to first register with the local mediator actor by sending DistributedPubSubMediator.Put
message.
class Destination extends Actor with ActorLogging {
import DistributedPubSubMediator.Put
val mediator = DistributedPubSub(context.system).mediator
// register to the path
mediator ! Put(self)
def receive = {
case s: String ⇒
log.info("Got {}", s)
}
}
The actorRef
passed to Put
(self
) in this case, must belong to the same actor system as the mediator actor.
Send
Messages can be sent to destination actors using DistributedPubSubMediator.Send
message to the local mediator. You need to specifiy the path of the destination actor (without the address information).
class Sender extends Actor {
import DistributedPubSubMediator.Send
// activate the extension
val mediator = DistributedPubSub(context.system).mediator
def receive = {
case in: String ⇒
val out = in.toUpperCase
mediator ! Send(path = "/user/destination", msg = out, localAffinity = true)
}
}
localAffinity = true
means if there is a matching actor in the local actor system, then it is preferred over remote actors.
Remove
Actors are automatically removed from the registry when they are terminated, or you can explicitly remove entries with DistributedPubSubMediator.Remove
.
SendToAll
It is possible to broadcast a message to all matching actors using DistributedPubSubMediator.SendToAll
message.