akka.contrib.pattern
Class DistributedPubSubMediator

java.lang.Object
  extended by akka.contrib.pattern.DistributedPubSubMediator
All Implemented Interfaces:
Actor, ActorLogging

public class DistributedPubSubMediator
extends java.lang.Object
implements Actor, ActorLogging

This actor manages a registry of actor references and replicates the entries to peer actors among all cluster nodes or a group of nodes tagged with a specific role.

The DistributedPubSubMediator is supposed to be started on all nodes, or all nodes with specified role, in the cluster. The mediator can be started with the DistributedPubSubExtension or as an ordinary actor.

Changes are only performed in the own part of the registry and those changes are versioned. Deltas are disseminated in a scalable way to other nodes with a gossip protocol. The registry is eventually consistent, i.e. changes are not immediately visible at other nodes, but typically they will be fully replicated to all other nodes after a few seconds.

You can send messages via the mediator on any node to registered actors on any other node. There is three modes of message delivery.

1. DistributedPubSubMediator.Send - The message will be delivered to one recipient with a matching path, if any such exists in the registry. If several entries match the path the message will be sent via the supplied routingLogic (default random) to one destination. The sender of the message can specify that local affinity is preferred, i.e. the message is sent to an actor in the same local actor system as the used mediator actor, if any such exists, otherwise route to any other matching entry. A typical usage of this mode is private chat to one other user in an instant messaging application. It can also be used for distributing tasks to registered workers, like a cluster aware router where the routees dynamically can register themselves.

2. DistributedPubSubMediator.SendToAll - The message will be delivered to all recipients with a matching path. Actors with the same path, without address information, can be registered on different nodes. On each node there can only be one such actor, since the path is unique within one local actor system. Typical usage of this mode is to broadcast messages to all replicas with the same path, e.g. 3 actors on different nodes that all perform the same actions, for redundancy.

3. DistributedPubSubMediator.Publish - Actors may be registered to a named topic instead of path. This enables many subscribers on each node. The message will be delivered to all subscribers of the topic. For efficiency the message is sent over the wire only once per node (that has a matching topic), and then delivered to all subscribers of the local topic representation. This is the true pub/sub mode. A typical usage of this mode is a chat room in an instant messaging application.

4. DistributedPubSubMediator.Publish with sendOneMessageToEachGroup - Actors may be subscribed to a named topic with an optional property group. If subscribing with a group name, each message published to a topic with the sendOneMessageToEachGroup flag is delivered via the supplied routingLogic (default random) to one actor within each subscribing group. If all the subscribed actors have the same group name, then this works just like DistributedPubSubMediator.Send and all messages are delivered to one subscribe. If all the subscribed actors have different group names, then this works like normal DistributedPubSubMediator.Publish and all messages are broadcast to all subscribers.

You register actors to the local mediator with DistributedPubSubMediator.Put or DistributedPubSubMediator.Subscribe. Put is used together with Send and SendToAll message delivery modes. The ActorRef in Put must belong to the same local actor system as the mediator. Subscribe is used together with Publish. Actors are automatically removed from the registry when they are terminated, or you can explicitly remove entries with DistributedPubSubMediator.Remove or DistributedPubSubMediator.Unsubscribe.

Successful Subscribe and Unsubscribe is acknowledged with DistributedPubSubMediator.SubscribeAck and DistributedPubSubMediator.UnsubscribeAck replies.


Nested Class Summary
static class DistributedPubSubMediator.Count$
           
static class DistributedPubSubMediator.Internal$
          INTERNAL API
static class DistributedPubSubMediator.Publish
           
static class DistributedPubSubMediator.Publish$
           
static class DistributedPubSubMediator.Put
           
static class DistributedPubSubMediator.Put$
           
static class DistributedPubSubMediator.Remove
           
static class DistributedPubSubMediator.Remove$
           
static class DistributedPubSubMediator.Send
           
static class DistributedPubSubMediator.Send$
           
static class DistributedPubSubMediator.SendToAll
           
static class DistributedPubSubMediator.SendToAll$
           
static class DistributedPubSubMediator.Subscribe
           
static class DistributedPubSubMediator.Subscribe$
           
static class DistributedPubSubMediator.SubscribeAck
           
static class DistributedPubSubMediator.SubscribeAck$
           
static class DistributedPubSubMediator.Unsubscribe
           
static class DistributedPubSubMediator.Unsubscribe$
           
static class DistributedPubSubMediator.UnsubscribeAck
           
static class DistributedPubSubMediator.UnsubscribeAck$
           
 
Nested classes/interfaces inherited from interface akka.actor.Actor
Actor.emptyBehavior$
 
Constructor Summary
DistributedPubSubMediator(scala.Option<java.lang.String> role, RoutingLogic routingLogic, scala.concurrent.duration.FiniteDuration gossipInterval, scala.concurrent.duration.FiniteDuration removedTimeToLive, int maxDeltaElements)
           
 
Method Summary
 Cluster cluster()
           
 scala.collection.immutable.Iterable<akka.contrib.pattern.DistributedPubSubMediator.Internal.Bucket> collectDelta(scala.collection.immutable.Map<Address,java.lang.Object> otherVersions)
           
static Props defaultProps(java.lang.String role)
          Java API: Factory method for DistributedPubSubMediator Props with default values.
 void gossip()
          Gossip to peer nodes.
 Cancellable gossipTask()
           
 void gossipTo(Address address)
           
 boolean matchingRole(Member m)
           
 java.lang.String mkKey(ActorPath path)
           
 java.lang.String mkKey(ActorRef ref)
           
 scala.collection.immutable.Map<Address,java.lang.Object> myVersions()
           
 scala.Function0<java.lang.Object> nextVersion()
           
 scala.collection.immutable.Set<Address> nodes()
           
 boolean otherHasNewerVersions(scala.collection.immutable.Map<Address,java.lang.Object> otherVersions)
           
 void postStop()
          User overridable callback.
 void preStart()
          User overridable callback.
static Props props(scala.Option<java.lang.String> role, RoutingLogic routingLogic, scala.concurrent.duration.FiniteDuration gossipInterval, scala.concurrent.duration.FiniteDuration removedTimeToLive, int maxDeltaElements)
          Scala API: Factory method for DistributedPubSubMediator Props.
static Props props(java.lang.String role, RoutingLogic routingLogic, scala.concurrent.duration.FiniteDuration gossipInterval, scala.concurrent.duration.FiniteDuration removedTimeToLive, int maxDeltaElements)
          Java API: Factory method for DistributedPubSubMediator Props.
 void prune()
           
 scala.concurrent.duration.FiniteDuration pruneInterval()
           
 Cancellable pruneTask()
           
 void publish(java.lang.String path, java.lang.Object msg, boolean allButSelf)
           
 void publishToEachGroup(java.lang.String path, java.lang.Object msg)
           
 void put(java.lang.String key, scala.Option<ActorRef> valueOption)
           
 scala.PartialFunction<java.lang.Object,scala.runtime.BoxedUnit> receive()
          This defines the initial actor behavior, it must return a partial function with the actor logic.
 void registerTopic(ActorRef ref)
           
 scala.collection.immutable.Map<Address,akka.contrib.pattern.DistributedPubSubMediator.Internal.Bucket> registry()
           
 long removedTimeToLiveMillis()
           
 scala.Option<Address> selectRandomNode(scala.collection.immutable.IndexedSeq<Address> addresses)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface akka.actor.Actor
aroundPostRestart, aroundPostStop, aroundPreRestart, aroundPreStart, aroundReceive, context, postRestart, preRestart, self, sender, supervisorStrategy, unhandled
 
Methods inherited from interface akka.actor.ActorLogging
_log, log
 

Constructor Detail

DistributedPubSubMediator

public DistributedPubSubMediator(scala.Option<java.lang.String> role,
                                 RoutingLogic routingLogic,
                                 scala.concurrent.duration.FiniteDuration gossipInterval,
                                 scala.concurrent.duration.FiniteDuration removedTimeToLive,
                                 int maxDeltaElements)
Method Detail

props

public static Props props(scala.Option<java.lang.String> role,
                          RoutingLogic routingLogic,
                          scala.concurrent.duration.FiniteDuration gossipInterval,
                          scala.concurrent.duration.FiniteDuration removedTimeToLive,
                          int maxDeltaElements)
Scala API: Factory method for DistributedPubSubMediator Props.

Parameters:
role - (undocumented)
routingLogic - (undocumented)
gossipInterval - (undocumented)
removedTimeToLive - (undocumented)
maxDeltaElements - (undocumented)
Returns:
(undocumented)

props

public static Props props(java.lang.String role,
                          RoutingLogic routingLogic,
                          scala.concurrent.duration.FiniteDuration gossipInterval,
                          scala.concurrent.duration.FiniteDuration removedTimeToLive,
                          int maxDeltaElements)
Java API: Factory method for DistributedPubSubMediator Props.

Parameters:
role - (undocumented)
routingLogic - (undocumented)
gossipInterval - (undocumented)
removedTimeToLive - (undocumented)
maxDeltaElements - (undocumented)
Returns:
(undocumented)

defaultProps

public static Props defaultProps(java.lang.String role)
Java API: Factory method for DistributedPubSubMediator Props with default values.

Parameters:
role - (undocumented)
Returns:
(undocumented)

cluster

public Cluster cluster()

removedTimeToLiveMillis

public long removedTimeToLiveMillis()

gossipTask

public Cancellable gossipTask()

pruneInterval

public scala.concurrent.duration.FiniteDuration pruneInterval()

pruneTask

public Cancellable pruneTask()

registry

public scala.collection.immutable.Map<Address,akka.contrib.pattern.DistributedPubSubMediator.Internal.Bucket> registry()

nodes

public scala.collection.immutable.Set<Address> nodes()

nextVersion

public scala.Function0<java.lang.Object> nextVersion()

preStart

public void preStart()
Description copied from interface: Actor
User overridable callback.

Is called when an Actor is started. Actors are automatically started asynchronously when created. Empty default implementation.

Specified by:
preStart in interface Actor

postStop

public void postStop()
Description copied from interface: Actor
User overridable callback.

Is called asynchronously after 'actor.stop()' is invoked. Empty default implementation.

Specified by:
postStop in interface Actor

matchingRole

public boolean matchingRole(Member m)

receive

public scala.PartialFunction<java.lang.Object,scala.runtime.BoxedUnit> receive()
Description copied from interface: Actor
This defines the initial actor behavior, it must return a partial function with the actor logic.

Specified by:
receive in interface Actor
Returns:
(undocumented)

publish

public void publish(java.lang.String path,
                    java.lang.Object msg,
                    boolean allButSelf)

publishToEachGroup

public void publishToEachGroup(java.lang.String path,
                               java.lang.Object msg)

put

public void put(java.lang.String key,
                scala.Option<ActorRef> valueOption)

registerTopic

public void registerTopic(ActorRef ref)

mkKey

public java.lang.String mkKey(ActorRef ref)

mkKey

public java.lang.String mkKey(ActorPath path)

myVersions

public scala.collection.immutable.Map<Address,java.lang.Object> myVersions()

collectDelta

public scala.collection.immutable.Iterable<akka.contrib.pattern.DistributedPubSubMediator.Internal.Bucket> collectDelta(scala.collection.immutable.Map<Address,java.lang.Object> otherVersions)

otherHasNewerVersions

public boolean otherHasNewerVersions(scala.collection.immutable.Map<Address,java.lang.Object> otherVersions)

gossip

public void gossip()
Gossip to peer nodes.


gossipTo

public void gossipTo(Address address)

selectRandomNode

public scala.Option<Address> selectRandomNode(scala.collection.immutable.IndexedSeq<Address> addresses)

prune

public void prune()