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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
import scala.actors.MigrationSystem._
import scala.actors.Actor._
import scala.actors.{ Actor, StashingActor, ActorRef, Props, MigrationSystem, PoisonPill }
import java.util.concurrent.{ TimeUnit, CountDownLatch }
import scala.collection.mutable.ArrayBuffer
object Test {
def testLoopWithConditionReact() = {
// Snippet showing composition of receives
// Loop with Condition Snippet - before
val myActor = actor {
var c = true
loopWhile(c) {
react {
case x: Int =>
// do task
println("do task")
if (x == 42) c = false
}
}
}
myActor.start()
myActor ! 1
myActor ! 42
// Loop with Condition Snippet - migrated
val myAkkaActor = MigrationSystem.actorOf(Props(() => new StashingActor {
def receive = {
case x: Int =>
// do task
println("do task")
if (x == 42) context.stop(self)
}
}, "default-stashing-dispatcher"))
myAkkaActor ! 1
myAkkaActor ! 42
}
def testNestedReact() = {
// Snippet showing composition of receives
// Loop with Condition Snippet - before
val myActor = actor {
var c = true
loopWhile(c) {
react {
case x: Int =>
// do task
println("do task " + x)
if (x == 42) c = false
else
react {
case y: String =>
println("do string " + y)
}
println("after react")
}
}
}
myActor.start()
myActor ! 1
myActor ! "I am a String"
myActor ! 42
Thread.sleep(100)
println(myActor.getState)
// Loop with Condition Snippet - migrated
val myAkkaActor = MigrationSystem.actorOf(Props(() => new StashingActor {
def receive = {
case x: Int =>
// do task
println("do task " + x)
if (x == 42)
context.stop(self)
else
context.become(({
case y: String =>
println("do string " + y)
}: Receive).andThen(x => {
unstashAll()
context.unbecome()
}).orElse { case x => stash() })
}
}, "default-stashing-dispatcher"))
myAkkaActor ! 1
myAkkaActor ! "I am a String"
myAkkaActor ! 42
}
def exceptionHandling() = {
// Stashing actor with act and exception handler
val myActor = MigrationSystem.actorOf(Props(() => new StashingActor {
def receive = { case _ => println("Dummy method.") }
override def act() = {
loop {
react {
case "fail" =>
throw new Exception("failed")
case "work" =>
println("working")
case "die" =>
exit()
}
}
}
override def exceptionHandler = {
case x: Exception => println("scala got exception")
}
}, "default-stashing-dispatcher"))
myActor ! "work"
myActor ! "fail"
myActor ! "die"
Thread.sleep(100)
// Stashing actor in Akka style
val myAkkaActor = MigrationSystem.actorOf(Props(() => new StashingActor {
def receive = PFCatch({
case "fail" =>
throw new Exception("failed")
case "work" =>
println("working")
case "die" =>
context.stop(self)
}, { case x: Exception => println("akka got exception") })
}, "default-stashing-dispatcher"))
myAkkaActor ! "work"
myAkkaActor ! "fail"
myAkkaActor ! "die"
}
def main(args: Array[String]) = {
testLoopWithConditionReact()
Thread.sleep(100)
exceptionHandling()
Thread.sleep(100)
testNestedReact()
}
}
// As per Jim Mcbeath blog (http://jim-mcbeath.blogspot.com/2008/07/actor-exceptions.html)
class PFCatch(f: PartialFunction[Any, Unit], handler: PartialFunction[Exception, Unit])
extends PartialFunction[Any, Unit] {
def apply(x: Any) = {
try {
f(x)
} catch {
case e: Exception if handler.isDefinedAt(e) => handler(e)
}
}
def isDefinedAt(x: Any) = f.isDefinedAt(x)
}
object PFCatch {
def apply(f: PartialFunction[Any, Unit], handler: PartialFunction[Exception, Unit]) = new PFCatch(f, handler)
}
|