Switched Listeners impl from Agent to CopyOnWriteArraySet
This commit is contained in:
parent
c70852177f
commit
89bf596f38
2 changed files with 23 additions and 15 deletions
|
|
@ -6,26 +6,34 @@ package se.scalablesolutions.akka.patterns
|
|||
|
||||
import se.scalablesolutions.akka.actor.{Actor, ActorRef}
|
||||
|
||||
import java.util.concurrent.CopyOnWriteArraySet
|
||||
|
||||
sealed trait ListenerMessage
|
||||
case class Listen(listener: ActorRef) extends ListenerMessage
|
||||
case class Deafen(listener: ActorRef) extends ListenerMessage
|
||||
case class WithListeners(f: Set[ActorRef] => Unit) extends ListenerMessage
|
||||
case class WithListeners(f: List[ActorRef] => Unit) extends ListenerMessage
|
||||
|
||||
/** Listeners is a generic trait to implement listening capability on an Actor
|
||||
* Use the <code>gossip(msg)</code> method to have it sent to the listenees
|
||||
* Send <code>Listen(self)</code> to start listening
|
||||
* Send <code>Deafen(self)</code> to stop listening
|
||||
* Send <code>WithListeners(fun)</code> to traverse the current listeners
|
||||
/**
|
||||
* Listeners is a generic trait to implement listening capability on an Actor.
|
||||
* <p/>
|
||||
* Use the <code>gossip(msg)</code> method to have it sent to the listeners.
|
||||
* <p/>
|
||||
* Send <code>Listen(self)</code> to start listening.
|
||||
* <p/>
|
||||
* Send <code>Deafen(self)</code> to stop listening.
|
||||
* <p/>
|
||||
* Send <code>WithListeners(fun)</code> to traverse the current listeners.
|
||||
*/
|
||||
trait Listeners { self : Actor =>
|
||||
import se.scalablesolutions.akka.actor.Agent
|
||||
private lazy val listeners = Agent(Set[ActorRef]())
|
||||
trait Listeners { self: Actor =>
|
||||
private val listeners = new CopyOnWriteArraySet[ActorRef]
|
||||
|
||||
protected def listenerManagement : Receive = {
|
||||
case Listen(l) => listeners( _ + l)
|
||||
case Deafen(l) => listeners( _ - l )
|
||||
case WithListeners(f) => listeners foreach f
|
||||
protected def listenerManagement: Receive = {
|
||||
case Listen(l) => listeners add l
|
||||
case Deafen(l) => listeners remove l
|
||||
case WithListeners(f) => f(listenersAsList)
|
||||
}
|
||||
|
||||
protected def gossip(msg : Any) = listeners foreach ( _ foreach ( _ ! msg ) )
|
||||
protected def gossip(msg: Any) = listenersAsList foreach (_ ! msg)
|
||||
|
||||
private def listenersAsList: List[ActorRef] = listeners.toArray.toList.asInstanceOf[List[ActorRef]]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -223,7 +223,7 @@ class AkkaParent(info: ProjectInfo) extends DefaultProject(info) {
|
|||
|
||||
class AkkaRedisProject(info: ProjectInfo) extends AkkaDefaultProject(info, distPath) {
|
||||
val redis = "com.redis" % "redisclient" % "2.8.0.RC2-1.4-SNAPSHOT" % "compile"
|
||||
//override def testOptions = TestFilter((name: String) => name.endsWith("Test")) :: Nil
|
||||
override def testOptions = TestFilter((name: String) => name.endsWith("Test")) :: Nil
|
||||
}
|
||||
|
||||
class AkkaMongoProject(info: ProjectInfo) extends AkkaDefaultProject(info, distPath) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue