6. The native Java concurrency model Based on: Threads Semaphores Synchronization Locks They are sometimes plain evil … … and sometimes a necessary pain … … but always the wrong default
17. Better performances and throughput and smallermemoryfootprint (650 bytes) than native scala actors
18.
19. Sending message to UntypedActors There are 3 different ways to send a message to an actor: Fire-and-forget ( ! In Scala ) actor.sendOneWay(msg); Uses a Future under the hood, blocking the sender Actor until eithera reply is received or the Future times out ( !! In Scala ) Object res1 = actor.sendRequestReply(msg); Object res2 = actor.sendRequestReply(msg, 1000); // timeout Explicitly returns a Future ( !!! In Scala ) Future future= actor.sendRequestReplyFuture(msg); IMPORTANT: Messages can be any kind of object but have to be immutable. Akkacan’t enforce immutability (yet) so this has to be by convention
20. Typed Actors public class CounterImplextends TypedActorimplements Counter { private int counter = 0; public void count() { counter++; System.out.println(counter); } public intgetCountNr() { returncounter; } } Counter counter= (Counter)TypedActor .newInstance(Counter.class, CounterImpl.class, 1000); counter.count(); // Fire-and-forget intcount = counter.getCountNr(); // uses Future under the hood
21. “Let it crash” fault-tolerance through supervisor hierarchies
23. What’swrong in trying to preventerrors? Java and other languages and frameworks (not designed with concurrency in mind) signal faults/errors with exceptions Throwing an exception in concurrent code will just simply blow up the thread that currently executes the code that threw it: There is no way to find out that things went wrong, apart from inspecting the stack trace There is nothing you can do to recover from the problem and bring back your system to a normal functioning
26. Supervised actors also allow you to create sets (hierarchies) of actors where you can be sure that either all are dead or none are dead
27. That encourages non-defensive programming: don’t try to prevent things from go wrong, because they will. Failures are a natural state in the life-cycle of your app: crash early and let someone else (that sees the whole picture), deal with it
28. A supervisor is responsible for starting, stopping and monitoring its child processes. It can act with 2 fault handling strategies:One-For-One or All-For-One
29. Declarative supervised actors configuration Supervisor supervisor = new Supervisor( new SupervisorConfig( new AllForOneStrategy( // or OneForOneStrategy new Class[]{Exception.class}, // Exceptions to handle 3, // maximum number of restart retries 5000), // within time in millis new Supervise[] { new Supervise( UntypedActor.actorOf(MyActor1.class), permanent()), // the actor will always be restarted new Supervise( UntypedActor.actorOf(MyActor2.class), temporary()) // the actor won’t be shut down normally })); // You can also link and unlink actors from a supervisor supervisor.link(actor1); supervisor.unlink(actor1);
30.
31. Uses a very efficient and scalable I/O implementation built upon JBossNetty and Google Protocol Buffers