summaryrefslogtreecommitdiff
path: root/test/disabled/presentation/akka/src/akka/config/SupervisionConfig.scala
blob: 40f61f615fe88ab065006e8b4a991c5ad54cf635 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
 * Copyright (C) 2009-2011 Scalable Solutions AB <http://scalablesolutions.se>
 */

package akka.config

import akka.dispatch.MessageDispatcher
import akka.actor.{ MaximumNumberOfRestartsWithinTimeRangeReached, ActorRef }
import akka.japi.{ Procedure2, Procedure }

case class RemoteAddress(val hostname: String, val port: Int)

/**
 * Configuration classes - not to be used as messages.
 *
 * @author <a href="http://jonasboner.com">Jonas Bon&#233;r</a>
 */
object Supervision {
  sealed abstract class ConfigElement

  abstract class Server extends ConfigElement
  sealed abstract class LifeCycle extends ConfigElement
  sealed abstract class FaultHandlingStrategy(val trapExit: List[Class[_ <: Throwable]]) extends ConfigElement

  case class SupervisorConfig(restartStrategy: FaultHandlingStrategy, worker: List[Server], maxRestartsHandler: (ActorRef, MaximumNumberOfRestartsWithinTimeRangeReached) => Unit = { (aRef, max) => () }) extends Server {
    //Java API
    def this(restartStrategy: FaultHandlingStrategy, worker: Array[Server]) = this(restartStrategy, worker.toList)
    def this(restartStrategy: FaultHandlingStrategy, worker: Array[Server], restartHandler: Procedure2[ActorRef, MaximumNumberOfRestartsWithinTimeRangeReached]) = this(restartStrategy, worker.toList, { (aRef, max) => restartHandler.apply(aRef, max) })
  }

  class Supervise(val actorRef: ActorRef, val lifeCycle: LifeCycle, val registerAsRemoteService: Boolean = false) extends Server {
    //Java API
    def this(actorRef: ActorRef, lifeCycle: LifeCycle) =
      this(actorRef, lifeCycle, false)
  }

  object Supervise {
    def apply(actorRef: ActorRef, lifeCycle: LifeCycle, registerAsRemoteService: Boolean = false) = new Supervise(actorRef, lifeCycle, registerAsRemoteService)
    def apply(actorRef: ActorRef, lifeCycle: LifeCycle) = new Supervise(actorRef, lifeCycle, false)
    def unapply(supervise: Supervise) = Some((supervise.actorRef, supervise.lifeCycle, supervise.registerAsRemoteService))
  }

  object AllForOneStrategy {
    def apply(trapExit: List[Class[_ <: Throwable]], maxNrOfRetries: Int, withinTimeRange: Int): AllForOneStrategy =
      new AllForOneStrategy(trapExit,
        if (maxNrOfRetries < 0) None else Some(maxNrOfRetries), if (withinTimeRange < 0) None else Some(withinTimeRange))
  }

  case class AllForOneStrategy(override val trapExit: List[Class[_ <: Throwable]],
                               maxNrOfRetries: Option[Int] = None,
                               withinTimeRange: Option[Int] = None) extends FaultHandlingStrategy(trapExit) {
    def this(trapExit: List[Class[_ <: Throwable]], maxNrOfRetries: Int, withinTimeRange: Int) =
      this(trapExit,
        if (maxNrOfRetries < 0) None else Some(maxNrOfRetries), if (withinTimeRange < 0) None else Some(withinTimeRange))

    def this(trapExit: Array[Class[_ <: Throwable]], maxNrOfRetries: Int, withinTimeRange: Int) =
      this(trapExit.toList,
        if (maxNrOfRetries < 0) None else Some(maxNrOfRetries), if (withinTimeRange < 0) None else Some(withinTimeRange))

    def this(trapExit: java.util.List[Class[_ <: Throwable]], maxNrOfRetries: Int, withinTimeRange: Int) =
      this(trapExit.toArray.toList.asInstanceOf[List[Class[_ <: Throwable]]],
        if (maxNrOfRetries < 0) None else Some(maxNrOfRetries), if (withinTimeRange < 0) None else Some(withinTimeRange))
  }

  object OneForOneStrategy {
    def apply(trapExit: List[Class[_ <: Throwable]], maxNrOfRetries: Int, withinTimeRange: Int): OneForOneStrategy =
      new OneForOneStrategy(trapExit,
        if (maxNrOfRetries < 0) None else Some(maxNrOfRetries), if (withinTimeRange < 0) None else Some(withinTimeRange))
  }

  case class OneForOneStrategy(override val trapExit: List[Class[_ <: Throwable]],
                               maxNrOfRetries: Option[Int] = None,
                               withinTimeRange: Option[Int] = None) extends FaultHandlingStrategy(trapExit) {
    def this(trapExit: List[Class[_ <: Throwable]], maxNrOfRetries: Int, withinTimeRange: Int) =
      this(trapExit,
        if (maxNrOfRetries < 0) None else Some(maxNrOfRetries), if (withinTimeRange < 0) None else Some(withinTimeRange))

    def this(trapExit: Array[Class[_ <: Throwable]], maxNrOfRetries: Int, withinTimeRange: Int) =
      this(trapExit.toList,
        if (maxNrOfRetries < 0) None else Some(maxNrOfRetries), if (withinTimeRange < 0) None else Some(withinTimeRange))

    def this(trapExit: java.util.List[Class[_ <: Throwable]], maxNrOfRetries: Int, withinTimeRange: Int) =
      this(trapExit.toArray.toList.asInstanceOf[List[Class[_ <: Throwable]]],
        if (maxNrOfRetries < 0) None else Some(maxNrOfRetries), if (withinTimeRange < 0) None else Some(withinTimeRange))
  }

  case object NoFaultHandlingStrategy extends FaultHandlingStrategy(Nil)

  //Scala API
  case object Permanent extends LifeCycle
  case object Temporary extends LifeCycle
  case object UndefinedLifeCycle extends LifeCycle

  //Java API (& Scala if you fancy)
  def permanent(): LifeCycle = Permanent
  def temporary(): LifeCycle = Temporary
  def undefinedLifeCycle(): LifeCycle = UndefinedLifeCycle

  //Java API
  def noFaultHandlingStrategy = NoFaultHandlingStrategy

  case class SuperviseTypedActor(_intf: Class[_],
                                 val target: Class[_],
                                 val lifeCycle: LifeCycle,
                                 val timeout: Long,
                                 _dispatcher: MessageDispatcher, // optional
                                 _remoteAddress: RemoteAddress // optional
                                 ) extends Server {
    val intf: Option[Class[_]] = Option(_intf)
    val dispatcher: Option[MessageDispatcher] = Option(_dispatcher)
    val remoteAddress: Option[RemoteAddress] = Option(_remoteAddress)

    def this(target: Class[_], lifeCycle: LifeCycle, timeout: Long) =
      this(null: Class[_], target, lifeCycle, timeout, null: MessageDispatcher, null: RemoteAddress)

    def this(intf: Class[_], target: Class[_], lifeCycle: LifeCycle, timeout: Long) =
      this(intf, target, lifeCycle, timeout, null: MessageDispatcher, null: RemoteAddress)

    def this(intf: Class[_], target: Class[_], lifeCycle: LifeCycle, timeout: Long, dispatcher: MessageDispatcher) =
      this(intf, target, lifeCycle, timeout, dispatcher, null: RemoteAddress)

    def this(target: Class[_], lifeCycle: LifeCycle, timeout: Long, dispatcher: MessageDispatcher) =
      this(null: Class[_], target, lifeCycle, timeout, dispatcher, null: RemoteAddress)

    def this(intf: Class[_], target: Class[_], lifeCycle: LifeCycle, timeout: Long, remoteAddress: RemoteAddress) =
      this(intf, target, lifeCycle, timeout, null: MessageDispatcher, remoteAddress)

    def this(target: Class[_], lifeCycle: LifeCycle, timeout: Long, remoteAddress: RemoteAddress) =
      this(null: Class[_], target, lifeCycle, timeout, null: MessageDispatcher, remoteAddress)

    def this(target: Class[_], lifeCycle: LifeCycle, timeout: Long, dispatcher: MessageDispatcher, remoteAddress: RemoteAddress) =
      this(null: Class[_], target, lifeCycle, timeout, dispatcher, remoteAddress)
  }
}