On Github tginsberg / what-is-akka
Created by Todd Ginsberg for CJUG
So come say hi after this!
...and it does this with the Actor Concurrency model.
OK, so how does that happen? First let's look at what we're trying to avoid.Not to say it's all bad, plenty of great systems written this way.
Just not the only way to solve the problem.
Only way to talk to an actor is to send or receive a message.
Small, atomic units of functionality and state.
Each actor has a parent - heirarchy.
This is what Erlang uses. Ericsson's AXD301 switch had 9 9's (< 1s in 20 years)
Actor:
class CounterActor extends Actor with ActorLogging { var counter = 0 def receive = { case Increment => counter += 1 sender ! CounterState(counter) case Decrement => counter -= 1 sender ! CounterState(counter) } }
Sending a Message:
val actor = system.actorOf(CounterActor.props, "myCounter") actor ! Increment
Point out var.
! == send
Props - not shown here but used to create actors with properties and settings
Well what about errors?
Let it crash!
These decions can be complicated (fail at most 3 times in 10 seconds, and then restart up to 4 times before escalating).
Akka provides code to help with these
override val supervisorStrategy = OneForOneStrategy(maxNrOfRetries = 10, withinTimeRange = 1 minute) { case _: ArithmeticException => Resume case _: NullPointerException => Restart case _: IllegalArgumentException => Stop case _: Exception => Escalate }(Borrowed from the Akka documentation!)
Separation of concerns - deciding vs. doing. Akka does the restart/stop/etc
Contrast this with traditional exception handling.
Restart/Stop apply to all children.
Loose coupling between the strategy and actually carrying out the work of recovery.
Used when you want to wait for the answer
val futureAnswer = actor ? Increment val result = Await.result(futureAnswer, 10 seconds) println(result)
Many different strategies supported.
Or write your own!
Visit http://akka.io for very comprehensive documentation and examples.
Thanks!