Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ayu-24
Created October 15, 2019 07:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ayu-24/a4ac6997ccb4e8cc56636d68b419f414 to your computer and use it in GitHub Desktop.
Save ayu-24/a4ac6997ccb4e8cc56636d68b419f414 to your computer and use it in GitHub Desktop.
import akka.Done
import akka.actor.Actor.Receive
import akka.actor.{Actor, ActorContext, ActorSystem, Props}
import akka.pattern.ask
import akka.routing.RoundRobinPool
import akka.util.Timeout
import scala.concurrent.Future
import scala.concurrent.duration._
import scala.io.Source
import scala.language.postfixOps
class Counter extends Actor {
override def receive: Receive = TimingReceive {
case file: String =>
val content = Source.fromFile(file)
.getLines().toList
.map(row => row.split(",").toList)
.size
println("Number of words in file " + file + "-" + content)
sender() ! Done
case _ => None
}
}
class TimingReceive(r: Receive, totalTime: Long)(implicit ctx: ActorContext) extends Receive {
def isDefinedAt(o: Any): Boolean = {
r.isDefinedAt(o)
}
def apply(o: Any): Unit = {
val startTime = System.nanoTime()
r(o)
val newTotal = totalTime + (System.nanoTime() - startTime)
println("Total time so far: " + totalTime + " milliseconds")
ctx.become(new TimingReceive(r, newTotal))
}
}
object WordCountUsingAkka {
implicit val timeout: Timeout = Timeout(50 seconds)
def main(args: Array[String]) {
val system = ActorSystem("Counting")
val props = Props[Counter].withRouter(RoundRobinPool(2))
val actor = system.actorOf(props, "Ping")
val result: List[Future[Done]] = List.fill[String](10)("/home/workspace/Desktop/enwik8").map { file =>
(actor ? file).mapTo[Done]
}
}
}
object TimingReceive {
def apply(r: Receive)(implicit ctx: ActorContext): Receive = new TimingReceive(r, 0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment