Академический Документы
Профессиональный Документы
Культура Документы
CONTENTS
What is the Actor Model?
Defining an Actor Message-Driven, Elastic, Resilient, and Responsive Java
Creating Actors UPDATED BY JOHAN ANDRN
INTRODUCTION messages and never talks with it directly. This abstraction allows
actor-based applications to seamlessly scale from a single
Akka is a toolkit and runtime for building concurrent,
machine to large clusters.
distributed, and reactive applications and systems on the JVM.
The Reactive Manifesto (reactivemanifesto.org) defines
CREATING A NEW AKKA APPLICATION
reactive in terms of its four guiding principles:
1. Message-driven To create a new Akka application, use your favorite IDE and
build tool to create a new empty project. Akka is provided as
2. Elastic
regular jar files available through Maven central, so you can use
3. Resilient it by just adding the following dependency to your project (after
4. Responsive taking a look at akka.io to see which is the latest stable version).
NOTE: 2.5.0 will be available very soon.
Message-driven means the application reacts to events by
Maven:
using a message-driven programming model. This allows the
application to more effectively share resources by doing work <dependency>
only in response to outside messages. <groupId>com.typesafe.akka</groupId>
<artifactId>akka-actor_2.11</artifactId>
Elastic means the application is able to react to increasing
<version>2.5.0-RC1</version>
load by making the architecture highly concurrent and
</dependency>
distributed.
v
Self healing, distributed
Microservices on the JVM
with Akka and Java
Akka was designed to enable developers to easily build reactive
GET STARTED WITH AKKA
systems using a high level of abstraction, without having to deal
with low-level concepts like threads, mutexes, and deadlocks. It
does so by leveraging the Actor Model of concurrency and fault-
tolerance. This is a powerful model that allows the behavior and
state of the application to be encapsulated and modeled as an
actor in a very natural and simple way. The key principle behind
an actor is that the application only interacts with it through
public class Application { 3. The actor is able to maintain mutable state without
worrying about concurrent access to this state.
public static void main(String[] args) throws
Exception { Shared mutable state is a cause of many concurrency problems.
final ActorSystem system = ActorSystem. Akka actors solve this problem by processing a single message
create();
at a time and transparently handling memory visibility when the
System.out.println(Press any key to
actor executes on different threads over time. Many threads can
terminate); hold an ActorRef and send messages to it concurrently, yet the
System.in.read(); actor will process them in a linear fashion. Accessing or mutating
System.out.println(Shutting down actor the internal state of an actor is fully thread safe since the thread is
system...);
protected by the Actor Model.
system.terminate();
}
The strong isolation principles of actors, together with the message-
}
driven model and location transparency, make it easy to solve hard
ActorSystem starts and maintains thread pools, and will keep concurrency and scalability problems in an intuitive way. It abstracts
running until you explicitly tell it to terminate, so in this sample away the underlying threading model and actor scheduling so you
we block the main thread until a key is pressed, which triggers can focus on building your application and not infrastructure.
termination. If we did not do that, the main method would return and
An actor is created by defining a class that extends AbstractActor
the ActorSystem would keep running.
and implements the createReceive method to define how the
This Refcard introduces Akka by modeling a simple robot named actor should react to the different messages it receives.
AkkaBot with Akkas Java APIs. There is a full set of corresponding
To start building our robot, the AkkaBot, we first need to create
Scala APIs, but those will not be covered. The AkkaBot will be
an actor that will represent it. It will contain information about
modeled as an actor, which is either moving or still, and has a
whether the robot is moving the direction of movement. It has an
direction it is moving in.
onReceive method that defines its behavior for how it should
react upon receiving a move message.
WHAT IS THE ACTOR MODEL?
The Actor Model was originally invented by Carl Hewitt in 1973 and DEFINING AN ACTOR
has seen a recent resurgence in popularity. In the Actor Model,
Create the file AkkaBot.java in the source directory and enter the
an application is broken up into small units of execution called
Java code below:
actors. These actors encapsulate the behavior and state of a part
of the application. The first part of the definition of actors makes
import akka.actor.AbstractActor;
them sound very much like objects in a standard object-oriented
design, which is true. However, what distinguishes an actor from a public class AkkaBot extends AbstractActor {
standard object is a third property: communication.
public Receive createReceive() {
Actors never interact directly with each other and instead return receiveBuilder().build();
communicate via messages. The behavior of an actor is primarily }
defined by how it handles messages as they are received. Since the }
state of an actor is isolated from the rest of the system, an actor
never has to worry about synchronization issues, such as thread
locking, when modifying its state. CREATING ACTORS
Actors in Akka are extremely lightweight since they are standard Now that we have defined the actor, lets create an instance of
objects that only consume a few hundred bytes each. The only it. Since we need Akka to manage the actors lifecycle, we dont
constraint in number of actors is the amount of memory they create it directly just using the new operator, but instead ask the
consume. This means that a single application can easily create ActorSystem to create and manage the actor for us. What we
thousands or even millions of concurrent actors. get back is not an instance of the actor itself, but an ActorRef
pointing to our actor instance.
An important part of the Actor Model is that you never create
actors directly, Instead, they are created by the ActorSystem. The This level of indirection adds a lot of power and flexibility. It
returned value is not the actual actor; instead it is a reference to enables location transparency, which means that the ActorRef
that actor called an ActorRef. There are three advantages to using can, while retaining the same semantics, represent an instance of
this level of indirection and never accessing the actor directly: the running actor in-process or on a remote machine (i.e. location
doesnt matter). This also means that the runtime can optimize Lets define the messages by putting them inside the AkkaBot
the system by changing an actors location or the applications class. It is very important that the messages we create are
topology while it is running. This level of indirection also enables immutable, meaning that they cannot be changed after
the let it crash model of failure management, in which the system construction, this guarantees that it is safe to share the messages
can heal itself by crashing and restarting faulty actors. between different threads of the JVM without using any
concurrency primitives such as locks.
To create (top level) actors in Akka you use the ActorSystem.
actorOf factory method. This method takes a configuration // Inside AkkaBot.java code add the following inner
object called Props and an optional name. classes
public enum Direction { FORWARD, BACKWARDS, RIGHT, LEFT }
The ActorSystem in Akka is also much more than just a factory for public static class Move {
public final Direction direction;
creating actors. It manages the entire lifecycle of the actor, maintains
public Move(Direction direction) {
the execution contexts (thread pools) in which actors run, a
this.direction = direction;
scheduling service, an event stream of what is happening, and more. }
}
Previously, we added the basic code for creating an ActorSystem public static class Stop {}
and the AkkaBot in the Application class. Now well update it to public static class GetRobotState {}
actually create an instance of your actor in the main method: public static class RobotState {
public final Direction direction;
public final boolean moving;
public static void main(String[] args) throws public RobotState(Direction direction, boolean moving) {
Exception { this.direction = direction;
final ActorSystem system = ActorSystem.create(); this.moving = moving;
}
final ActorRef akkaBot = system.actorOf( }
Props.create(AkkaBot.class),
akkaBot);
The actors mailbox is essentially a message queue and has When you run the application, you should see some basic logging
ordering semantics. This guarantees that the ordering of multiple of the application such as:
messages sent from the same sender is preserved, while the same
messages can be interleaved with the messages sent by another I am now moving FORWARD
I am now moving BACKWARDS
sender. When the actor is not processing messages it does not
I stopped moving
consume any resources, apart from memory.
// Add the following inside AkkaBot If you are not inside an actor or do not want to pass the sender,
private Optional<Direction> direction = Optional.empty(); use ActorRef.noSender() instead. An example of this case is
private boolean moving = false;
in the main class where messages were sent to the robot without
public Receive createReceive() { a sender.
return receiveBuilder()
.match(Move.class, this::onMove) THE SENDER REFERENCE
.match(Stop.class, this::onStop)
.build(); Since each message is paired with its unique sender reference, the
} current sender reference will change with each new message you
process. You can access it using the getSender() method. For
private void onMove(Move move) {
moving = true; example, to send a message back to the sender of the message that
direction = Optional.of(move.direction); is being handled, use:
System.out.println(I am now moving + direction.
get());
} // From within an actor
getSender().tell(new Greeting(greeting), getSelf());
private void onStop(Stop stop) {
moving = false;
System.out.println(I stopped moving); If you need to use a specific sender reference after some
} asynchronous processing, like after messaging with other actors,
you will need to store a reference to the sender in a variable. The
We can now test out the actors by sending them some simple reason for this is that the sender might change if other messaging
commands from the Bot Main app. or processing happens in the meantime.
Add the following to the main method in the Application class: In some cases there might not be a sender, like when a message is
sent from outside an actor or the sender was Actor.noSender.
akkaBot.tell(
In these cases, sending a message to sender would cause it to be
new AkkaBot.Move(AkkaBot.Direction.FORWARD), sent to dead letters. The dead-letter actor is where all unhandled
ActorRef.noSender()); messages end up. For debugging and auditing purposes, you can
akkaBot.tell(
watch for messages to the dead-letter actor.
new AkkaBot.Move(AkkaBot.Direction.BACKWARDS),
ActorRef.noSender());
akkaBot.tell( ACTOR HIERARCHIES
new AkkaBot.Stop(),
ActorRef.noSender()); The real power of the Actor Model is in actor hierarchies. An actor
hierarchy is when a parent actor creates and supervises child actors.
This structure helps avoid cascading failures that can take down an are coming from. Add a prefix of getSelf().path() to the
entire system by isolating and supervising child nodes. printlns in AkkaBot:
Creating child actors is very similar to creating top level actors - the System.out.println(getSelf().path() + : I am now moving
only difference is the context the child actors are created in. The + direction.get());
actor context can be thought of as where in the actor hierarchy an
actor lives. As an example, lets create a Bot Master that creates Then modify the Application main method to start the Bot Master
several children. Add the class BotMaster that creates several instead of the AkkaBot:
children in the constructor:
final ActorRef botMaster = system.actorOf(
Props.create(BotMaster.class),
import akka.actor.AbstractActor; botMaster);
import akka.actor.Props;
botMaster.tell(new BotMaster.StartChildBots(), ActorRef.
public class BotMaster extends AbstractActor { noSender());
public BotMaster() {
for (int index = 0; index < 10; index++) { The method call to getSelf().path() returns the path of the
getContext().actorOf(Props.create(AkkaBot.class)); current actor. This path will be something like:
}
} /user/botMaster/$a
@Override
public Receive createReceive() { User is a top level system actor that is always the root parent
return emptyBehavior();
of all user-created actors, botMaster we created by calling
}
} ActorSystem.actorOf, and $a is the actual bot actor. Since we
didnt give it an explicit name, Akka gave it a name that is unique
We access the local actor context and create new actors in that among its siblings. From the path we can see this actor is a child
context. That makes all of the AkkaBot actors we created children of botMaster.
of the Bot Master actor. Now that the Bot Master has children, it
can interact with the children directly. To do this, lets add a simple
method to make the child bots move.
There are two ways actors can deal with failures. The first is
public Receive createReceive() {
through customizing the supervision strategy. Each actor has a
return receiveBuilder()
default supervisor strategy that defines how it handles failures in .match(StartChildBots.class, this::onStartChildBots)
its children. The default supervisor strategy for an exception is to .match(Terminated.class, this::onChildTerminated)
restart the actor. This supervisor strategy can be overridden to .build();
}
define a different behavior. Besides restart, other possible actions
include escalating the exception up the hierarchy, simply resuming private void onChildTerminated(Terminated terminated) {
the actor, or stopping the actor. System.out.println(Child has stopped, starting a new
one);
Resuming and restarting might seem to be the same, however final ActorRef child =
getContext().actorOf(Props.create(AkkaBot.class));
there is one key difference. When an actor is restarted, a new
getContext().watch(child);
instance of the actor is created, which resets its state. If an actor }
is resumed, the actor state is maintained. With both strategies the
message that caused the fault is lost, but the actor does not lose
Now we need to make the child randomly fail. This can be done by
any of its other messages waiting in the mailbox.
adding the following to the Move message handler in the AkkaBot:
The second way a parent can manage the failures of its
children is to watch the children. Then the parent receives an System.out.println(getSelf().path() + : I am now moving
+ direction.get());
akka.actor.Terminated message when the child is terminated.
As an example, lets assume that if a child robot stops, we want to final Random random = new java.util.Random();
do some special processing. To do this we simply add a watch after final int nextInt = random.nextInt(10);
creating the robot. if ((nextInt % 2) == 0) {
getContext().stop(getSelf());
}
This is done by changing the constructor of the Bot Master by
watching each created child: You can now see how the actors deal with failure.
public BotMaster() {
FURTHER LEARNING
for (int index = 0; index < 10; index++) {
final ActorRef child = Now you have learned the basics of Akka actors, but theres a
getContext().actorOf(Props.create(AkkaBot.class));
lot more to it than just that. Actors can trivially be distributed
getContext().watch(child);
}
across multiple nodes within a cluster, handle load balancing and
} sharding as well as persistence, or streaming using Akka Streams.
If you want to learn more about what is in the toolkit, check out
And then handle the case of receiving a Terminated message: the Akka reference documentation at akka.io/docs and the Akka
samples at github.com/akka/akka-samples.
DZone communities deliver over 6 million pages each month to more than 3.3 million software
DZONE, INC.
developers, architects and decision makers. DZone offers something for everyone, including news,
150 PRESTON EXECUTIVE DR. REFCARDZ FEEDBACK WELCOME
tutorials, cheat sheets, research guides, feature articles, source code and more.
CARY, NC 27513 refcardz@dzone.com
"DZone is a developer's dream," says PC Magazine.
888.678.0399 SPONSORSHIP OPPORTUNITIES
Copyright 2017 DZone, Inc. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or
transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. 919.678.0300 sales@dzone.com