by
3 6
0
3
2,448
0
Top 1% !
Famous
Specified
Popularity: 8721st place

Published on:

No tags for this snippet yet.
LanguageScala
SourceGitHub

Simple Word Counter implemented using Akka

Simple Word Counter implemented using Akka: 
WordCounter.scala
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/680954/Simple-Word-Counter-implemented-using-Ak?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
import akka.actor.{ Actor, ActorRef, Props, ActorSystem } case class ProcessStringMsg(string: String) case class StringProcessedMsg(words: Integer) class StringCounterActor extends Actor { def receive = { case ProcessStringMsg(string) => { val wordsInLine = string.split(" ").length sender ! StringProcessedMsg(wordsInLine) } case _ => println("Error: message not recognized") } } case class StartProcessFileMsg() class WordCounterActor(filename: String) extends Actor { private var running = false private var totalLines = 0 private var linesProcessed = 0 private var result = 0 private var fileSender: Option[ActorRef] = None def receive = { case StartProcessFileMsg() => { if (running) { // println just used for example purposes; // Akka logger should be used instead println("Warning: duplicate start message received") } else { running = true fileSender = Some(sender) // save reference to process invoker import scala.io.Source._ fromFile(filename).getLines.foreach { line => context.actorOf(Props[StringCounterActor]) ! ProcessStringMsg(line) totalLines += 1 } } } case StringProcessedMsg(words) => { result += words linesProcessed += 1 if (linesProcessed == totalLines) { fileSender.map(_ ! result) // provide result to process invoker } } case _ => println("message not recognized!") } } object Sample extends App { import akka.util.Timeout import scala.concurrent.duration._ import akka.pattern.ask import akka.dispatch.ExecutionContexts._ implicit val ec = global override def main(args: Array[String]) { val system = ActorSystem("System") val actor = system.actorOf(Props(new WordCounterActor(args(0)))) implicit val timeout = Timeout(25 seconds) val future = actor ? StartProcessFileMsg() future.map { result => println("Total number of words " + result) system.shutdown } } }
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus