-
Notifications
You must be signed in to change notification settings - Fork 8
/
Worker.scala
29 lines (22 loc) · 904 Bytes
/
Worker.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
package actor_system.actor
import akka.actor.{Actor, ActorSystem}
import akka.contrib.mailbox.PeekMailboxExtension
import actor_system.message.{ResultMsg, WorkMsg}
import scala.concurrent.{ExecutionContext, Future}
/**
* Created by pabloperezgarcia on 18/12/2016.
*/
class Worker extends Actor {
def receive: PartialFunction[Any, Unit] = {
case WorkMsg(start, nrOfElements) =>
//sender Return asynchronously the message back to the master
sender ! ResultMsg(returnResultMsg(start, nrOfElements)) // perform the wor
PeekMailboxExtension.ack()//This return the ack to the sender
}
private def returnResultMsg(taskNumber: Int, nrOfElements: Int): Future[String] = {
implicit val ec: ExecutionContext = ActorSystem().dispatcher
Future {
s"Worker $taskNumber in thread ${Thread.currentThread().getName} finish job ${nrOfElements - taskNumber}"
}
}
}