blob: 7065397a4592a753c2a0e17e1481c1904168447e (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
/**
* Copyright (C) 2009-2011 Scalable Solutions AB <http://scalablesolutions.se>
*/
package akka.util
import java.util.concurrent.ConcurrentSkipListSet
import akka.actor.{ ActorInitializationException, ActorRef }
/**
* A manager for listener actors. Intended for mixin by observables.
*
* @author Martin Krasser
*/
trait ListenerManagement {
private val listeners = new ConcurrentSkipListSet[ActorRef]
/**
* Specifies whether listeners should be started when added and stopped when removed or not
*/
protected def manageLifeCycleOfListeners: Boolean = true
/**
* Adds the <code>listener</code> this this registry's listener list.
* The <code>listener</code> is started by this method if manageLifeCycleOfListeners yields true.
*/
def addListener(listener: ActorRef) {
if (manageLifeCycleOfListeners) listener.start()
listeners add listener
}
/**
* Removes the <code>listener</code> this this registry's listener list.
* The <code>listener</code> is stopped by this method if manageLifeCycleOfListeners yields true.
*/
def removeListener(listener: ActorRef) {
listeners remove listener
if (manageLifeCycleOfListeners) listener.stop()
}
/*
* Returns whether there are any listeners currently
*/
def hasListeners: Boolean = !listeners.isEmpty
/**
* Checks if a specific listener is registered. ActorInitializationException leads to removal of listener if that
* one isShutdown.
*/
def hasListener(listener: ActorRef): Boolean = listeners.contains(listener)
protected[akka] def notifyListeners(message: => Any) {
if (hasListeners) {
val msg = message
val iterator = listeners.iterator
while (iterator.hasNext) {
val listener = iterator.next
// Uncomment if those exceptions are so frequent as to bottleneck
// if (listener.isShutdown) iterator.remove() else
try {
listener ! msg
} catch {
case e: ActorInitializationException =>
if (listener.isShutdown) iterator.remove()
}
}
}
}
/**
* Execute <code>f</code> with each listener as argument. ActorInitializationException is not handled.
*/
protected[akka] def foreachListener(f: (ActorRef) => Unit) {
val iterator = listeners.iterator
while (iterator.hasNext) {
val listener = iterator.next
if (listener.isRunning) f(listener)
}
}
}
|