###Implementation of Concurrency and Fault Tolerance Made Easy: An Akka Tutorial with Examples
Using Akka Actors this reads all text files in a directory and counts the number of words in each file.
This is an Intellij SBT project, so you can clone it and play around. I hope this helps someone! 😀
Example output:
Stream Complete: 2.txt Total Number of Words: 13084 Total Time: 148ms
Stream Complete: 4.txt Total Number of Words: 13084 Total Time: 113ms
Stream Complete: 8.txt Total Number of Words: 9100 Total Time: 107ms
Stream Complete: 5.txt Total Number of Words: 13084 Total Time: 160ms
Stream Complete: 6.txt Total Number of Words: 9100 Total Time: 99ms
Stream Complete: 7.txt Total Number of Words: 9100 Total Time: 109ms
Stream Complete: 3.txt Total Number of Words: 13084 Total Time: 238ms
Stream Complete: 10.txt Total Number of Words: 26168 Total Time: 306ms
Stream Complete: 9.txt Total Number of Words: 1256064 Total Time: 1267ms
Stream Complete: 1.txt Total Number of Words: 1256064 Total Time: 1283ms
####Update: Don't create a new ActorSystem for each file.
Thanks Reddit User mmccaskill for pointing it out.
-
The solution was to pass in the ActorSystem as a parameter.
-
Don't name the Listener and Router Actors let Akka do it, so it will be unique
-
Keep count of the number of files processed, so you can terminate the ActorSystem when everything finishes.
Read more here: [Do I need to re-use the same Akka ActorSystem or can I just create one every time I need one?] (http://stackoverflow.com/questions/10396552/do-i-need-to-re-use-the-same-akka-actorsystem-or-can-i-just-create-one-every-tim)
####Messages are sent to an Actor through one of the following methods.
!
means “fire-and-forget”, e.g. send a message asynchronously and return immediately. Also known as tell.?
sends a message asynchronously and returns a Future representing a possible reply. Also known as ask