introducing akka
DESCRIPTION
Introduction to Akka 2. Explains what Akka's actors are all about and how to utilize them to write scalable and fault-tolerant systems. Talk given at JavaZone 2012.TRANSCRIPT
![Page 1: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/1.jpg)
Introducing
Jonas Bonér CTO Typesafe
![Page 2: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/2.jpg)
Introducing
![Page 3: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/3.jpg)
Introducing
Akka (Áhkká)
The name comes from the goddess in the Sami (native swedes) mythology that represented all
the wisdom and beauty in the world.
It is also the name of a beautiful mountain in Laponia in the north part of Sweden
![Page 4: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/4.jpg)
Introducing
![Page 5: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/5.jpg)
Vision
Simpler
Concurrency
Scalability
Fault-tolerance
![Page 6: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/6.jpg)
Vision
...with a single unified
Programming Model
Managed Runtime
Open Source Distribution
![Page 7: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/7.jpg)
Manage System Overload
![Page 8: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/8.jpg)
Scale UP & Scale OUT
![Page 9: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/9.jpg)
Program at a Higher Level
![Page 10: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/10.jpg)
Program at a Higher Level
![Page 11: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/11.jpg)
Program at a Higher Level• Never think in terms of shared state, state
visibility, threads, locks, concurrent collections, thread notifications etc.
![Page 12: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/12.jpg)
Program at a Higher Level• Never think in terms of shared state, state
visibility, threads, locks, concurrent collections, thread notifications etc.
• Low level concurrency plumbing BECOMES SIMPLE WORKFLOW - you only think about how messages flow in the system
![Page 13: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/13.jpg)
Program at a Higher Level• Never think in terms of shared state, state
visibility, threads, locks, concurrent collections, thread notifications etc.
• Low level concurrency plumbing BECOMES SIMPLE WORKFLOW - you only think about how messages flow in the system
• You get high CPU utilization, low latency, high throughput and scalability - FOR FREE as part of the model
![Page 14: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/14.jpg)
Program at a Higher Level• Never think in terms of shared state, state
visibility, threads, locks, concurrent collections, thread notifications etc.
• Low level concurrency plumbing BECOMES SIMPLE WORKFLOW - you only think about how messages flow in the system
• You get high CPU utilization, low latency, high throughput and scalability - FOR FREE as part of the model
• Proven and superior model for detecting and recovering from errors
![Page 15: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/15.jpg)
Distributable by Design
![Page 16: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/16.jpg)
Distributable by Design
![Page 17: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/17.jpg)
Distributable by Design
• Actors are location transparent & distributable by design
![Page 18: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/18.jpg)
Distributable by Design
• Actors are location transparent & distributable by design
• Scale UP and OUT for free as part of the model
![Page 19: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/19.jpg)
Distributable by Design
• Actors are location transparent & distributable by design
• Scale UP and OUT for free as part of the model
• You get the PERFECT FABRIC for the CLOUD
![Page 20: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/20.jpg)
Distributable by Design
• Actors are location transparent & distributable by design
• Scale UP and OUT for free as part of the model
• You get the PERFECT FABRIC for the CLOUD
- elastic & dynamic
![Page 21: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/21.jpg)
Distributable by Design
• Actors are location transparent & distributable by design
• Scale UP and OUT for free as part of the model
• You get the PERFECT FABRIC for the CLOUD
- elastic & dynamic
- fault-tolerant & self-healing
![Page 22: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/22.jpg)
Distributable by Design
• Actors are location transparent & distributable by design
• Scale UP and OUT for free as part of the model
• You get the PERFECT FABRIC for the CLOUD
- elastic & dynamic
- fault-tolerant & self-healing
- adaptive load-balancing, cluster rebalancing & actor migration
![Page 23: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/23.jpg)
Distributable by Design
• Actors are location transparent & distributable by design
• Scale UP and OUT for free as part of the model
• You get the PERFECT FABRIC for the CLOUD
- elastic & dynamic
- fault-tolerant & self-healing
- adaptive load-balancing, cluster rebalancing & actor migration
- build extremely loosely coupled and dynamic systems that can change and adapt at runtime
![Page 24: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/24.jpg)
Selection of Akka Production Users
![Page 25: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/25.jpg)
![Page 26: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/26.jpg)
How can we achieve this?
![Page 27: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/27.jpg)
How can we achieve this?
![Page 28: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/28.jpg)
How can we achieve this?
Let’s use Actors
![Page 29: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/29.jpg)
What is an Actor?
![Page 30: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/30.jpg)
What is an Actor?
![Page 31: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/31.jpg)
What is an Actor?
• Akka's unit of code organization is called an Actor
![Page 32: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/32.jpg)
What is an Actor?
• Akka's unit of code organization is called an Actor
• Actors helps you create concurrent, scalable and fault-tolerant applications
![Page 33: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/33.jpg)
What is an Actor?
• Akka's unit of code organization is called an Actor
• Actors helps you create concurrent, scalable and fault-tolerant applications
• Like Java EE servlets and session beans, Actors is a model for organizing your code that keeps many “policy decisions” separate from the business logic
![Page 34: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/34.jpg)
What is an Actor?
• Akka's unit of code organization is called an Actor
• Actors helps you create concurrent, scalable and fault-tolerant applications
• Like Java EE servlets and session beans, Actors is a model for organizing your code that keeps many “policy decisions” separate from the business logic
• Actors may be new to many in the Java community, but they are a tried-and-true concept (Hewitt 1973) used for many years in telecom systems with 9 nines uptime
![Page 35: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/35.jpg)
![Page 36: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/36.jpg)
Actors can be seen asVirtual Machines (nodes)
in Cloud Computing
![Page 37: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/37.jpg)
![Page 38: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/38.jpg)
Encapsulated and decoupled black boxes...
![Page 39: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/39.jpg)
Encapsulated and decoupled black boxes...
![Page 40: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/40.jpg)
![Page 41: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/41.jpg)
...that manages their own memory and behavior
![Page 42: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/42.jpg)
...that manages their own memory and behavior
![Page 43: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/43.jpg)
![Page 44: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/44.jpg)
Communicates with asynchronous
non-blocking messages
![Page 45: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/45.jpg)
Communicates with asynchronous
non-blocking messages
![Page 46: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/46.jpg)
![Page 47: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/47.jpg)
Elastic - grow and shrink on demand
![Page 48: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/48.jpg)
Elastic - grow and shrink on demand
![Page 49: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/49.jpg)
Elastic - grow and shrink on demand
![Page 50: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/50.jpg)
Elastic - grow and shrink on demand
![Page 51: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/51.jpg)
![Page 52: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/52.jpg)
Hot deploy - change behavior at runtime
![Page 53: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/53.jpg)
Hot deploy - change behavior at runtime
![Page 54: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/54.jpg)
Now - if we replace
with ActorEVERYTHING will STILL HOLD for both
LOCAL and DISTRIBUTED Actors
![Page 55: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/55.jpg)
What can I use Actors for?
![Page 56: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/56.jpg)
What can I use Actors for?In different scenarios, an Actor may be an alternative to:
![Page 57: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/57.jpg)
What can I use Actors for?In different scenarios, an Actor may be an alternative to:
- a thread
![Page 58: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/58.jpg)
What can I use Actors for?In different scenarios, an Actor may be an alternative to:
- a thread
- an object instance or component
![Page 59: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/59.jpg)
What can I use Actors for?In different scenarios, an Actor may be an alternative to:
- a thread
- an object instance or component
- a callback or listener
![Page 60: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/60.jpg)
What can I use Actors for?In different scenarios, an Actor may be an alternative to:
- a thread
- an object instance or component
- a callback or listener
- a singleton or service
![Page 61: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/61.jpg)
What can I use Actors for?In different scenarios, an Actor may be an alternative to:
- a thread
- an object instance or component
- a callback or listener
- a singleton or service
- a router, load-balancer or pool
![Page 62: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/62.jpg)
What can I use Actors for?In different scenarios, an Actor may be an alternative to:
- a thread
- an object instance or component
- a callback or listener
- a singleton or service
- a router, load-balancer or pool
- a Java EE Session Bean or Message-Driven Bean
![Page 63: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/63.jpg)
What can I use Actors for?In different scenarios, an Actor may be an alternative to:
- a thread
- an object instance or component
- a callback or listener
- a singleton or service
- a router, load-balancer or pool
- a Java EE Session Bean or Message-Driven Bean
- an out-of-process service
![Page 64: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/64.jpg)
What can I use Actors for?In different scenarios, an Actor may be an alternative to:
- a thread
- an object instance or component
- a callback or listener
- a singleton or service
- a router, load-balancer or pool
- a Java EE Session Bean or Message-Driven Bean
- an out-of-process service
- a Finite State Machine (FSM)
![Page 65: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/65.jpg)
So, what is the
Actor Model?
![Page 66: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/66.jpg)
Carl Hewitt’s definition
http://bit.ly/hewitt-on-actors
![Page 67: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/67.jpg)
Carl Hewitt’s definition
http://bit.ly/hewitt-on-actors
- The fundamental unit of computation that embodies:
![Page 68: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/68.jpg)
Carl Hewitt’s definition
http://bit.ly/hewitt-on-actors
- The fundamental unit of computation that embodies:
- Processing
![Page 69: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/69.jpg)
Carl Hewitt’s definition
http://bit.ly/hewitt-on-actors
- The fundamental unit of computation that embodies:
- Processing
- Storage
![Page 70: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/70.jpg)
Carl Hewitt’s definition
http://bit.ly/hewitt-on-actors
- The fundamental unit of computation that embodies:
- Processing
- Storage
- Communication
![Page 71: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/71.jpg)
Carl Hewitt’s definition
http://bit.ly/hewitt-on-actors
- The fundamental unit of computation that embodies:
- Processing
- Storage
- Communication
- 3 axioms - When an Actor receives a message it can:
![Page 72: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/72.jpg)
Carl Hewitt’s definition
http://bit.ly/hewitt-on-actors
- The fundamental unit of computation that embodies:
- Processing
- Storage
- Communication
- 3 axioms - When an Actor receives a message it can:
- Create new Actors
![Page 73: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/73.jpg)
Carl Hewitt’s definition
http://bit.ly/hewitt-on-actors
- The fundamental unit of computation that embodies:
- Processing
- Storage
- Communication
- 3 axioms - When an Actor receives a message it can:
- Create new Actors
- Send messages to Actors it knows
![Page 74: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/74.jpg)
Carl Hewitt’s definition
http://bit.ly/hewitt-on-actors
- The fundamental unit of computation that embodies:
- Processing
- Storage
- Communication
- 3 axioms - When an Actor receives a message it can:
- Create new Actors
- Send messages to Actors it knows
- Designate how it should handle the next message it receives
![Page 75: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/75.jpg)
4 core Actor operations
0. DEFINE
1. CREATE
2. SEND
3. BECOME
4. SUPERVISE
![Page 76: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/76.jpg)
public class Greeting implements Serializable {public final String who;public Greeting(String who) { this.who = who; }
}
public class GreetingActor extends UntypedActor { LoggingAdapter log = Logging.getLogger(getContext().system(), this);
public void onReceive(Object message) throws Exception {if (message instanceof Greeting)
log.info("Hello {}", ((Greeting) message).who);}
}}
0. DEFINE
![Page 77: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/77.jpg)
public class Greeting implements Serializable {public final String who;public Greeting(String who) { this.who = who; }
}
public class GreetingActor extends UntypedActor { LoggingAdapter log = Logging.getLogger(getContext().system(), this);
public void onReceive(Object message) throws Exception {if (message instanceof Greeting)
log.info("Hello {}", ((Greeting) message).who);}
}}
0. DEFINEDefine the message(s) the Actor
should be able to respond to
![Page 78: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/78.jpg)
public class Greeting implements Serializable {public final String who;public Greeting(String who) { this.who = who; }
}
public class GreetingActor extends UntypedActor { LoggingAdapter log = Logging.getLogger(getContext().system(), this);
public void onReceive(Object message) throws Exception {if (message instanceof Greeting)
log.info("Hello {}", ((Greeting) message).who);}
}}
0. DEFINEDefine the message(s) the Actor
should be able to respond to
Define the Actor class
![Page 79: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/79.jpg)
public class Greeting implements Serializable {public final String who;public Greeting(String who) { this.who = who; }
}
public class GreetingActor extends UntypedActor { LoggingAdapter log = Logging.getLogger(getContext().system(), this);
public void onReceive(Object message) throws Exception {if (message instanceof Greeting)
log.info("Hello {}", ((Greeting) message).who);}
}}
0. DEFINEDefine the message(s) the Actor
should be able to respond to
Define the Actor class
Define the Actor’s behavior
![Page 80: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/80.jpg)
case class Greeting(who: String)
class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) => log.info("Hello {}", who) }}
Scala version
![Page 81: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/81.jpg)
1. CREATE• CREATE - creates a new instance of an Actor
• Extremely lightweight (2.7 Million per Gb RAM)
• Very strong encapsulation - encapsulates:
- state
- behavior
- message queue
• State & behavior is indistinguishable from each other
• Only way to observe state is by sending an actor a message and see how it reacts
![Page 82: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/82.jpg)
public class Greeting implements Serializable {public final String who;public Greeting(String who) { this.who = who; }
}
public class GreetingActor extends UntypedActor { LoggingAdapter log = Logging.getLogger(getContext().system(), this);
public void onReceive(Object message) throws Exception {if (message instanceof Greeting)
log.info("Hello {}", ((Greeting) message).who);}
}}
ActorSystem system = ActorSystem.create("MySystem");ActorRef greeter = system.actorOf(new Props(GreetingActor.class), "greeter");
CREATE Actor
![Page 83: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/83.jpg)
public class Greeting implements Serializable {public final String who;public Greeting(String who) { this.who = who; }
}
public class GreetingActor extends UntypedActor { LoggingAdapter log = Logging.getLogger(getContext().system(), this);
public void onReceive(Object message) throws Exception {if (message instanceof Greeting)
log.info("Hello {}", ((Greeting) message).who);}
}}
ActorSystem system = ActorSystem.create("MySystem");ActorRef greeter = system.actorOf(new Props(GreetingActor.class), "greeter");
CREATE Actor
Create an Actor system
![Page 84: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/84.jpg)
public class Greeting implements Serializable {public final String who;public Greeting(String who) { this.who = who; }
}
public class GreetingActor extends UntypedActor { LoggingAdapter log = Logging.getLogger(getContext().system(), this);
public void onReceive(Object message) throws Exception {if (message instanceof Greeting)
log.info("Hello {}", ((Greeting) message).who);}
}}
ActorSystem system = ActorSystem.create("MySystem");ActorRef greeter = system.actorOf(new Props(GreetingActor.class), "greeter");
CREATE Actor
Create an Actor system Actor configuration
![Page 85: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/85.jpg)
public class Greeting implements Serializable {public final String who;public Greeting(String who) { this.who = who; }
}
public class GreetingActor extends UntypedActor { LoggingAdapter log = Logging.getLogger(getContext().system(), this);
public void onReceive(Object message) throws Exception {if (message instanceof Greeting)
log.info("Hello {}", ((Greeting) message).who);}
}}
ActorSystem system = ActorSystem.create("MySystem");ActorRef greeter = system.actorOf(new Props(GreetingActor.class), "greeter");
CREATE Actor
Create an Actor system Actor configuration
Give it a name
![Page 86: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/86.jpg)
public class Greeting implements Serializable {public final String who;public Greeting(String who) { this.who = who; }
}
public class GreetingActor extends UntypedActor { LoggingAdapter log = Logging.getLogger(getContext().system(), this);
public void onReceive(Object message) throws Exception {if (message instanceof Greeting)
log.info("Hello {}", ((Greeting) message).who);}
}}
ActorSystem system = ActorSystem.create("MySystem");ActorRef greeter = system.actorOf(new Props(GreetingActor.class), "greeter");
CREATE Actor
Create an Actor system
Create the Actor
Actor configuration
Give it a name
![Page 87: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/87.jpg)
public class Greeting implements Serializable {public final String who;public Greeting(String who) { this.who = who; }
}
public class GreetingActor extends UntypedActor { LoggingAdapter log = Logging.getLogger(getContext().system(), this);
public void onReceive(Object message) throws Exception {if (message instanceof Greeting)
log.info("Hello {}", ((Greeting) message).who);}
}}
ActorSystem system = ActorSystem.create("MySystem");ActorRef greeter = system.actorOf(new Props(GreetingActor.class), "greeter");
CREATE Actor
Create an Actor system
Create the Actor
Actor configuration
Give it a nameYou get an ActorRef back
![Page 88: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/88.jpg)
Guardian System Actor
Actors can form hierarchies
![Page 89: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/89.jpg)
Guardian System Actor
system.actorOf(Props[Foo], “Foo”)
Actors can form hierarchies
![Page 90: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/90.jpg)
Foo
Guardian System Actor
system.actorOf(Props[Foo], “Foo”)
Actors can form hierarchies
![Page 91: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/91.jpg)
Foo
Guardian System Actor
context.actorOf(Props[A], “A”)
Actors can form hierarchies
![Page 92: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/92.jpg)
A
Foo
Guardian System Actor
context.actorOf(Props[A], “A”)
Actors can form hierarchies
![Page 93: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/93.jpg)
A
B
BarFoo
C
BE
A
D
C
Guardian System Actor
Actors can form hierarchies
![Page 94: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/94.jpg)
A
B
BarFoo
C
BE
A
D
C
Guardian System Actor
Name resolution - like a file-system
![Page 95: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/95.jpg)
A
B
BarFoo
C
BE
A
D
C
/Foo
Guardian System Actor
Name resolution - like a file-system
![Page 96: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/96.jpg)
A
B
BarFoo
C
BE
A
D
C
/Foo
/Foo/A
Guardian System Actor
Name resolution - like a file-system
![Page 97: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/97.jpg)
A
B
BarFoo
C
BE
A
D
C
/Foo
/Foo/A
/Foo/A/B
Guardian System Actor
Name resolution - like a file-system
![Page 98: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/98.jpg)
A
B
BarFoo
C
BE
A
D
C
/Foo
/Foo/A
/Foo/A/B
/Foo/A/D
Guardian System Actor
Name resolution - like a file-system
![Page 99: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/99.jpg)
2. SEND
![Page 100: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/100.jpg)
2. SEND• SEND - sends a message to an Actor
![Page 101: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/101.jpg)
2. SEND• SEND - sends a message to an Actor
• Asynchronous and Non-blocking - Fire-forget
![Page 102: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/102.jpg)
2. SEND• SEND - sends a message to an Actor
• Asynchronous and Non-blocking - Fire-forget
• Everything happens Reactively
![Page 103: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/103.jpg)
2. SEND• SEND - sends a message to an Actor
• Asynchronous and Non-blocking - Fire-forget
• Everything happens Reactively
- An Actor is passive until a message is sent to it, which triggers something within the Actor
![Page 104: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/104.jpg)
2. SEND• SEND - sends a message to an Actor
• Asynchronous and Non-blocking - Fire-forget
• Everything happens Reactively
- An Actor is passive until a message is sent to it, which triggers something within the Actor
- Messages is the Kinetic Energy in an Actor system
![Page 105: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/105.jpg)
2. SEND• SEND - sends a message to an Actor
• Asynchronous and Non-blocking - Fire-forget
• Everything happens Reactively
- An Actor is passive until a message is sent to it, which triggers something within the Actor
- Messages is the Kinetic Energy in an Actor system
- Actors can have lots of buffered Potential Energy but can't do anything with it until it is triggered by a message
![Page 106: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/106.jpg)
2. SEND• SEND - sends a message to an Actor
• Asynchronous and Non-blocking - Fire-forget
• Everything happens Reactively
- An Actor is passive until a message is sent to it, which triggers something within the Actor
- Messages is the Kinetic Energy in an Actor system
- Actors can have lots of buffered Potential Energy but can't do anything with it until it is triggered by a message
• EVERYTHING is asynchronous and lockless
![Page 107: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/107.jpg)
Throughput on a single box
+50 million messages per second
![Page 108: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/108.jpg)
public class Greeting implements Serializable {public final String who;public Greeting(String who) { this.who = who; }
}
public class GreetingActor extends UntypedActor { LoggingAdapter log = Logging.getLogger(getContext().system(), this);
public void onReceive(Object message) throws Exception {if (message instanceof Greeting)
log.info("Hello {}", ((Greeting) message).who);}
}}
ActorSystem system = ActorSystem.create("MySystem");ActorRef greeter = system.actorOf(new Props(GreetingActor.class), "greeter");greeter.tell(new Greeting("Charlie Parker"));
SEND message
![Page 109: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/109.jpg)
public class Greeting implements Serializable {public final String who;public Greeting(String who) { this.who = who; }
}
public class GreetingActor extends UntypedActor { LoggingAdapter log = Logging.getLogger(getContext().system(), this);
public void onReceive(Object message) throws Exception {if (message instanceof Greeting)
log.info("Hello {}", ((Greeting) message).who);}
}}
ActorSystem system = ActorSystem.create("MySystem");ActorRef greeter = system.actorOf(new Props(GreetingActor.class), "greeter");greeter.tell(new Greeting("Charlie Parker"));
SEND message
Send the message
![Page 110: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/110.jpg)
public class Greeting implements Serializable {public final String who;public Greeting(String who) { this.who = who; }
}
public class GreetingActor extends UntypedActor { LoggingAdapter log = Logging.getLogger(getContext().system(), this);
public void onReceive(Object message) throws Exception {if (message instanceof Greeting)
log.info("Hello {}", ((Greeting) message).who);}
}}
ActorSystem system = ActorSystem.create("MySystem");ActorRef greeter = system.actorOf(new Props(GreetingActor.class), "greeter");greeter.tell(new Greeting("Charlie Parker"));
Full example
![Page 111: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/111.jpg)
case class Greeting(who: String)
class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) => log.info("Hello {}", who) }}
val system = ActorSystem("MySystem")val greeter = system.actorOf(Props[GreetingActor], name = "greeter")greeter tell Greeting("Charlie Parker")
Scala version
![Page 112: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/112.jpg)
class SomeActor extends UntypedActor { void onReceive(Object msg) { if (msg instanceof User) { User user = (User) msg; // reply to sender getSender().tell(“Hi ” + user.name); } }}
Reply
![Page 113: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/113.jpg)
class SomeActor extends Actor { def receive = { case User(name) => // reply to sender sender tell (“Hi ” + name) }}
Scala version
![Page 114: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/114.jpg)
akka { actor { provider = akka.remote.RemoteActorRefProvider deployment { /Greeter { remote = } } }}
Just feed the ActorSystem with this configuration
Remote deployment
![Page 115: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/115.jpg)
akka { actor { provider = akka.remote.RemoteActorRefProvider deployment { /Greeter { remote = } } }}
Just feed the ActorSystem with this configuration
Configure a Remote Provider
Remote deployment
![Page 116: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/116.jpg)
akka { actor { provider = akka.remote.RemoteActorRefProvider deployment { /Greeter { remote = } } }}
Just feed the ActorSystem with this configuration
Configure a Remote Provider
For the Greeter actor
Remote deployment
![Page 117: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/117.jpg)
akka { actor { provider = akka.remote.RemoteActorRefProvider deployment { /Greeter { remote = } } }}
Just feed the ActorSystem with this configuration
Configure a Remote Provider
Define Remote Path
For the Greeter actor
Remote deployment
![Page 118: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/118.jpg)
akka { actor { provider = akka.remote.RemoteActorRefProvider deployment { /Greeter { remote = } } }}
Just feed the ActorSystem with this configuration
Configure a Remote Provider
Define Remote Path Protocol
For the Greeter actor
akka://
Remote deployment
![Page 119: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/119.jpg)
akka { actor { provider = akka.remote.RemoteActorRefProvider deployment { /Greeter { remote = } } }}
Just feed the ActorSystem with this configuration
Configure a Remote Provider
Define Remote Path Protocol Actor System
For the Greeter actor
akka://MySystem
Remote deployment
![Page 120: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/120.jpg)
akka { actor { provider = akka.remote.RemoteActorRefProvider deployment { /Greeter { remote = } } }}
Just feed the ActorSystem with this configuration
Configure a Remote Provider
Define Remote Path Protocol Actor System Hostname
For the Greeter actor
akka://MySystem@machine1
Remote deployment
![Page 121: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/121.jpg)
akka { actor { provider = akka.remote.RemoteActorRefProvider deployment { /Greeter { remote = } } }}
Just feed the ActorSystem with this configuration
Configure a Remote Provider
Define Remote Path Protocol Actor System Hostname Port
For the Greeter actor
akka://MySystem@machine1:2552
Remote deployment
![Page 122: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/122.jpg)
akka { actor { provider = akka.remote.RemoteActorRefProvider deployment { /Greeter { remote = } } }}
Just feed the ActorSystem with this configuration
Zero code changes
Configure a Remote Provider
Define Remote Path Protocol Actor System Hostname Port
For the Greeter actor
akka://MySystem@machine1:2552
Remote deployment
![Page 123: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/123.jpg)
3. BECOME
![Page 124: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/124.jpg)
3. BECOME
• BECOME - dynamically redefines Actor’s behavior
![Page 125: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/125.jpg)
3. BECOME
• BECOME - dynamically redefines Actor’s behavior
• Triggered reactively by receive of message
![Page 126: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/126.jpg)
3. BECOME
• BECOME - dynamically redefines Actor’s behavior
• Triggered reactively by receive of message
• In a type system analogy it is as if the object changed type - changed interface, protocol & implementation
![Page 127: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/127.jpg)
3. BECOME
• BECOME - dynamically redefines Actor’s behavior
• Triggered reactively by receive of message
• In a type system analogy it is as if the object changed type - changed interface, protocol & implementation
• Will now react differently to the messages it receives
![Page 128: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/128.jpg)
3. BECOME
• BECOME - dynamically redefines Actor’s behavior
• Triggered reactively by receive of message
• In a type system analogy it is as if the object changed type - changed interface, protocol & implementation
• Will now react differently to the messages it receives
• Behaviors are stacked & can be pushed and popped
![Page 129: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/129.jpg)
Why would I want to do that?
![Page 130: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/130.jpg)
Why would I want to do that?• Let a highly contended Actor adaptively transform
himself into an Actor Pool or a Router
![Page 131: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/131.jpg)
Why would I want to do that?• Let a highly contended Actor adaptively transform
himself into an Actor Pool or a Router
• Implement an FSM (Finite State Machine)
![Page 132: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/132.jpg)
Why would I want to do that?• Let a highly contended Actor adaptively transform
himself into an Actor Pool or a Router
• Implement an FSM (Finite State Machine)
• Implement graceful degradation
![Page 133: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/133.jpg)
Why would I want to do that?• Let a highly contended Actor adaptively transform
himself into an Actor Pool or a Router
• Implement an FSM (Finite State Machine)
• Implement graceful degradation
• Spawn up (empty) generic Worker processes that can become whatever the Master currently needs
![Page 134: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/134.jpg)
Why would I want to do that?• Let a highly contended Actor adaptively transform
himself into an Actor Pool or a Router
• Implement an FSM (Finite State Machine)
• Implement graceful degradation
• Spawn up (empty) generic Worker processes that can become whatever the Master currently needs
• etc. use your imagination
![Page 135: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/135.jpg)
Why would I want to do that?• Let a highly contended Actor adaptively transform
himself into an Actor Pool or a Router
• Implement an FSM (Finite State Machine)
• Implement graceful degradation
• Spawn up (empty) generic Worker processes that can become whatever the Master currently needs
• etc. use your imagination
• Very useful once you get the used to it
![Page 136: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/136.jpg)
context.become(new Procedure[Object]() { void apply(Object msg) { // new body if (msg instanceof NewMessage) { NewMessage newMsg = (NewMessage)msg; ... } }});
become
![Page 137: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/137.jpg)
context.become(new Procedure[Object]() { void apply(Object msg) { // new body if (msg instanceof NewMessage) { NewMessage newMsg = (NewMessage)msg; ... } }});
Actor context available from within an Actor
become
![Page 138: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/138.jpg)
context become { // new body case NewMessage => ... }
Scala version
![Page 139: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/139.jpg)
Load Balancing
![Page 140: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/140.jpg)
Routers
val router = system.actorOf( Props[SomeActor].withRouter( RoundRobinRouter(nrOfInstances = 5)))
Scala API
![Page 141: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/141.jpg)
Router + Resizer
val resizer = DefaultResizer(lowerBound = 2, upperBound = 15)
val router = system.actorOf( Props[ExampleActor1].withRouter( RoundRobinRouter(resizer = Some(resizer))))
Scala API
![Page 142: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/142.jpg)
Failure management in Java/C/C# etc.
![Page 143: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/143.jpg)
• You are given a SINGLE thread of control
Failure management in Java/C/C# etc.
![Page 144: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/144.jpg)
• You are given a SINGLE thread of control
• If this thread blows up you are screwed
Failure management in Java/C/C# etc.
![Page 145: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/145.jpg)
• You are given a SINGLE thread of control
• If this thread blows up you are screwed
• So you need to do all explicit error handling WITHIN this single thread
Failure management in Java/C/C# etc.
![Page 146: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/146.jpg)
• You are given a SINGLE thread of control
• If this thread blows up you are screwed
• So you need to do all explicit error handling WITHIN this single thread
• To make things worse - errors do not propagate between threads so there is NO WAY OF EVEN FINDING OUT that something have failed
Failure management in Java/C/C# etc.
![Page 147: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/147.jpg)
• You are given a SINGLE thread of control
• If this thread blows up you are screwed
• So you need to do all explicit error handling WITHIN this single thread
• To make things worse - errors do not propagate between threads so there is NO WAY OF EVEN FINDING OUT that something have failed
• This leads to DEFENSIVE programming with:
Failure management in Java/C/C# etc.
![Page 148: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/148.jpg)
• You are given a SINGLE thread of control
• If this thread blows up you are screwed
• So you need to do all explicit error handling WITHIN this single thread
• To make things worse - errors do not propagate between threads so there is NO WAY OF EVEN FINDING OUT that something have failed
• This leads to DEFENSIVE programming with:
• Error handling TANGLED with business logic
Failure management in Java/C/C# etc.
![Page 149: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/149.jpg)
• You are given a SINGLE thread of control
• If this thread blows up you are screwed
• So you need to do all explicit error handling WITHIN this single thread
• To make things worse - errors do not propagate between threads so there is NO WAY OF EVEN FINDING OUT that something have failed
• This leads to DEFENSIVE programming with:
• Error handling TANGLED with business logic
• SCATTERED all over the code base
Failure management in Java/C/C# etc.
![Page 150: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/150.jpg)
• You are given a SINGLE thread of control
• If this thread blows up you are screwed
• So you need to do all explicit error handling WITHIN this single thread
• To make things worse - errors do not propagate between threads so there is NO WAY OF EVEN FINDING OUT that something have failed
• This leads to DEFENSIVE programming with:
• Error handling TANGLED with business logic
• SCATTERED all over the code base
We can do better than this!!!
Failure management in Java/C/C# etc.
![Page 151: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/151.jpg)
4. SUPERVISE
![Page 152: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/152.jpg)
4. SUPERVISE
• SUPERVISE - manage another Actor’s failures
![Page 153: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/153.jpg)
4. SUPERVISE
• SUPERVISE - manage another Actor’s failures
• Error handling in actors is handle by letting Actors monitor (supervise) each other for failure
![Page 154: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/154.jpg)
4. SUPERVISE
• SUPERVISE - manage another Actor’s failures
• Error handling in actors is handle by letting Actors monitor (supervise) each other for failure
• This means that if an Actor crashes, a notification will be sent to his supervisor, who can react upon the failure
![Page 155: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/155.jpg)
4. SUPERVISE
• SUPERVISE - manage another Actor’s failures
• Error handling in actors is handle by letting Actors monitor (supervise) each other for failure
• This means that if an Actor crashes, a notification will be sent to his supervisor, who can react upon the failure
• This provides clean separation of processing and error handling
![Page 156: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/156.jpg)
...let’s take a standard OO application
![Page 157: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/157.jpg)
![Page 158: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/158.jpg)
Which components have critically important state
and explicit error handling?
![Page 159: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/159.jpg)
![Page 160: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/160.jpg)
Fault-tolerant onion-layered Error Kernel
![Page 161: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/161.jpg)
ErrorKernel
![Page 162: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/162.jpg)
ErrorKernel
![Page 163: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/163.jpg)
ErrorKernel
![Page 164: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/164.jpg)
ErrorKernel
![Page 165: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/165.jpg)
ErrorKernel
![Page 166: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/166.jpg)
ErrorKernel
![Page 167: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/167.jpg)
ErrorKernel
Node 1 Node 2
![Page 168: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/168.jpg)
SUPERVISE ActorEvery single actor has a
default supervisor strategy.Which is usually sufficient.But it can be overridden.
![Page 169: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/169.jpg)
class Supervisor extends UntypedActor { private SupervisorStrategy strategy = new OneForOneStrategy( 10, Duration.parse("1 minute"), new Function<Throwable, Directive>() { @Override public Directive apply(Throwable t) { if (t instanceof ArithmeticException) return resume(); else if (t instanceof NullPointerException) return restart(); else return escalate(); } });
@Override public SupervisorStrategy supervisorStrategy() { return strategy; } ActorRef worker = context.actorOf(new Props(Worker.class)); public void onReceive(Object message) throws Exception { if (message instanceof Integer) worker.forward(message); }}
SUPERVISE ActorEvery single actor has a
default supervisor strategy.Which is usually sufficient.But it can be overridden.
![Page 170: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/170.jpg)
class Supervisor extends UntypedActor { private SupervisorStrategy strategy = new OneForOneStrategy( 10, Duration.parse("1 minute"), new Function<Throwable, Directive>() { @Override public Directive apply(Throwable t) { if (t instanceof ArithmeticException) return resume(); else if (t instanceof NullPointerException) return restart(); else return escalate(); } });
@Override public SupervisorStrategy supervisorStrategy() { return strategy; } ActorRef worker = context.actorOf(new Props(Worker.class)); public void onReceive(Object message) throws Exception { if (message instanceof Integer) worker.forward(message); }}
SUPERVISE Actor
![Page 171: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/171.jpg)
class Supervisor extends Actor { override val supervisorStrategy = (maxNrOfRetries = 10, withinTimeRange = 1 minute) { case _: ArithmeticException => Resume case _: NullPointerException => Restart case _: Exception => Escalate }
val worker = context.actorOf(Props[Worker])
def receive = { case n: Int => worker forward n }}
Scala version
![Page 172: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/172.jpg)
class Supervisor extends Actor { override val supervisorStrategy = (maxNrOfRetries = 10, withinTimeRange = 1 minute) { case _: ArithmeticException => Resume case _: NullPointerException => Restart case _: Exception => Escalate }
val worker = context.actorOf(Props[Worker])
def receive = { case n: Int => worker forward n }}
Scala version
OneForOneStrategy
![Page 173: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/173.jpg)
class Supervisor extends Actor { override val supervisorStrategy = (maxNrOfRetries = 10, withinTimeRange = 1 minute) { case _: ArithmeticException => Resume case _: NullPointerException => Restart case _: Exception => Escalate }
val worker = context.actorOf(Props[Worker])
def receive = { case n: Int => worker forward n }}
Scala version
AllForOneStrategy
![Page 174: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/174.jpg)
class Worker extends Actor { ... override def preRestart( reason: Throwable, message: Option[Any]) { ... // clean up before restart } override def postRestart(reason: Throwable) { ... // init after restart }}
Manage failure
Scala API
![Page 175: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/175.jpg)
This was
Akka 2.x
![Page 176: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/176.jpg)
This was
Akka 2.xWell...it’s a start...
![Page 177: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/177.jpg)
...we have much much more
![Page 178: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/178.jpg)
AMQP
Dataflow
...we have much much moreCluster FSM
Transactors
Spring
Pub/Sub
ZeroMQ
Microkernel
IOTestKit
Agents
SLF4J
Durable MailboxesEventBus
Camel
TypedActor
Extensions
HTTP/REST
![Page 179: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/179.jpg)
get it and learn morehttp://akka.io
http://typesafe.com
http://letitcrash.com
![Page 180: Introducing Akka](https://reader030.vdocuments.us/reader030/viewer/2022020920/554a05f3b4c905557a8b5479/html5/thumbnails/180.jpg)
EOF