This repository has been archived by the owner on Oct 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 196
/
SpotterWrapper.scala
70 lines (53 loc) · 1.74 KB
/
SpotterWrapper.scala
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
package org.dbpedia.spotlight.db.concurrent
import java.io.IOException
import org.dbpedia.spotlight.model.{SurfaceFormOccurrence, Text}
import akka.actor.{OneForOneStrategy, Props, ActorSystem, Actor}
import akka.routing.SmallestMailboxRouter
import akka.actor.SupervisorStrategy.Restart
import org.dbpedia.spotlight.spot.Spotter
import akka.dispatch.Await
import akka.util.duration._
import akka.pattern.ask
import akka.util
/**
* A Wrapper for Spotter workers.
*
* @author Joachim Daiber
*/
class SpotterWrapper(val spotters: Seq[Spotter]) extends Spotter {
var requestTimeout = 60
val system = ActorSystem()
val workers = spotters.map { spotter: Spotter =>
system.actorOf(Props(new SpotterActor(spotter)))
}
def size: Int = spotters.size
val router = system.actorOf(Props[SpotterActor].withRouter(
SmallestMailboxRouter(routees = workers).withSupervisorStrategy(
OneForOneStrategy(maxNrOfRetries = 10) {
case _: IOException => Restart
})
)
)
implicit val timeout = util.Timeout(requestTimeout seconds)
def extract(text: Text): java.util.List[SurfaceFormOccurrence] = {
val futureResult = router ? SpotterRequest(text)
Await.result(futureResult, timeout.duration).asInstanceOf[java.util.List[SurfaceFormOccurrence]]
}
def close() {
system.shutdown()
}
def getName: String = "SpotterWrapper[%s]".format(spotters.head.getClass.getSimpleName)
def setName(name: String) {}
}
class SpotterActor(val spotter: Spotter) extends Actor {
def receive = {
case SpotterRequest(text) => {
try {
sender ! spotter.extract(text)
} catch {
case e: NullPointerException => throw new IOException("Could not tokenize.")
}
}
}
}
case class SpotterRequest(text: Text)