Creating a New Akka Application
Akka is a toolkit and runtime for building concurrent, distributed, and reactive applications and systems on the JVM. The Reactive Manifesto (reactivemanifesto.org) defines reactive in terms of its four guiding principles
1. Message-driven
2. Elastic
3. Resilient
4. Responsive
Message-driven means the application reacts to events by using a message-driven programming model. This allows the application to more effectively share resources by doing work only in response to outside messages.
Elastic means the application is able to react to increasing load by making the architecture highly concurrent and distributed.
When an application is Resilient, it can easily deal with failure and recovery. Instead of the application simply dying, it manages failure through fault isolation so other parts of the application can keep running.
The final property, being Responsive, means the application responds quickly not only in ideal conditions, but also when there are application failures.
Akka was designed to enable developers to easily build reactive 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 theActor 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 messages and never talks with it directly. This abstraction allows actor-based applications to seamlessly scale from a single machine to large clusters.
To create a new Akka application, use your favorite IDE and build tool to create a new empty project. Akka is provided as regular jar files available through Maven central, so you can use it by just adding the following dependency to your project (after taking a look at akka.io to see which is the latest stable version). NOTE: 2.5.0 will be available very soon.
Maven:
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-actor_2.11</artifactId>
<version>2.5.0-RC1</version>
</dependency>
Gradle:
compile 'com.typesafe.akka:akka-actor_2.11:2.5.0-RC1'
SBT:
"com.typesafe.akka" %% "akka-actor" % "2.5.0-RC1"
With the project in place, create a simple Java class called Application with a main method, and inside of that, create an ActorSystem:
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
public class Application {
public static void main(String[] args) throws Exception {
final ActorSystem system = ActorSystem.create();
System.out.println("Press any key to terminate");
System.in.read();
System.out.println("Shutting down actor system...");
system.terminate();
}
}
ActorSystem starts and maintains thread pools, and will keep running until you explicitly tell it to terminate, so in this sample we block the main thread until a key is pressed, which triggers termination. If we did not do that, the main method would return and the ActorSystem would keep running.
This Refcard introduces Akka by modeling a simple robot named AkkaBot with Akka’s Java APIs. There is a full set of corresponding Scala APIs, but those will not be covered. The AkkaBot will be modeled as an actor, which is either moving or still, and has a direction it is moving in.
{{ parent.title || parent.header.title}}
{{ parent.tldr }}
{{ parent.linkDescription }}
{{ parent.urlSource.name }}