Book Akka Essentials ⬆
Directory akka-essentials\ contains Akka code examples from Gupta's book Akka Essentials (Packt, 2012).It also includes different build scripts for experimenting with Akka on a Windows machine. |
The code examples presented below are written both in Java and in Scala and can be built/run with the following tools:
Build tool | Build file | Parent file | Environment(s) |
---|---|---|---|
ant.bat |
build.xml |
build.xml |
Any a) |
gradle.bat |
build.gradle |
common.gradle |
Any |
make.exe b) |
Makefile |
Makefile.inc |
Any |
mvn.cmd |
pom.xml |
pom.xml |
Any |
sbt.bat |
build.sbt |
Any | |
cmd.exe |
build.bat |
Windows only |
b) Default shell is
/bin/sh
as described in the online document Choosing the Shell.Code example FirstAkkaApplication
1 is an Akka application written in Java (e.g. MapReduceApplication.java
, MasterActor.java
2).
Batch file build.bat
matches what the user would run from the command prompt (use option -debug
to see the execution details):
> build -verbose run Compile 9 Java source files to directory "target\classes" [MapReduceApp-akka.actor.default-dispatcher-4] INFO akka.event.slf4j.Slf4jLogger - Slf4jLogger started {over=1, quick=1, belong=1, lazy=1, best=1, man's=1, brown=1, fox=2, fell=1, tried=1, same=1, friend=1, family=1, dog=4, jump=1}
Commands ant.bat
(build.xml
), gradle.bat
(build.gradle
), make
(Makefile
), mvn
(pom.xml
) and sbt
(build.sbt
) produce the same output; for instance :
> make -s run {over=1, quick=1, belong=1, lazy=1, best=1, man's=1, brown=1, fox=2, fell=1, tried=1, same=1, friend=1, family=1, dog=4, jump=1}
🔎
FirstAkkaApplicationScala
is the same Akka application written in Scala (e.g.MapReduceApplication.scala
,MasterActor.scala
). For instancebuild.bat
generates the following output:> build run [MapReduceApp-akka.actor.default-dispatcher-4] INFO akka.event.slf4j.Slf4jLogger - Slf4jLogger started HashMap(over -> 1, quick -> 1, belong -> 1, lazy -> 1, best -> 1, man's -> 1, brown -> 1, fox -> 2, fell -> 1, tried -> 1, same -> 1, friend -> 1, family -> 1, dog -> 4, jump -> 1)
ProcessOrder
Example ▴
Code example ProcessOrder
is an Akka application written in Java (e.g. ProcessOrderApp.java
, ProcessOrderActor.java
).
Batch file build.bat
matches what the user would run from the command prompt (use option -debug
to see the execution details):
> build -verbose run Compile 8 Java source files to directory "target\classes" [ProcessOrder-akka.actor.default-dispatcher-5] INFO akka.event.slf4j.Slf4jLogger - Slf4jLogger started OrderActor: userId=1 OrderAggregate: akka.actor.Status$Failure
🔎 Code example
ProcessOrderScala
is the same Akka application written in Scala (e.g.MapReduceApplication.scala
,MasterActor.scala
). For instancebuild.bat
generates the following output :> build run ...
PingPong
Example ▴
Code example PingPong
is an Akka application written in Java (e.g. PingPongApp.java
, PingPongActor.java
).
Batch file build.bat
matches what the user would run from the command prompt (use option -debug
to see the execution details):
> build -verbose run Compile 2 Java source files to directory "target\classes" [PingPong-akka.actor.default-dispatcher-5] INFO akka.event.slf4j.Slf4jLogger - Slf4jLogger started PING PONG PING PONG PING PONG PING PONG PING PONG PING
🔎
PingPongScala
is the same Akka application written in Scala (e.g.MapReduceApplication.scala
,PingPongActor.scala
). For instancebuild.bat
generates the following output :> build run [PingPong-akka.actor.default-dispatcher-4] INFO akka.event.slf4j.Slf4jLogger - Slf4jLogger started PING PONG PING PONG PING PONG PING PONG PING PONG PING
Footnotes ▴
[1] Source code updates ↩
- We have updated several deprecated code in the original examples of Gupta's book:
-
Original source code Updated source code a) (≤2.5) akka.actor.UntypedActor
akka.actor.UntypedAbstractActor
(≤2.3) akka.routing.RoundRobinRouter
akka.routing.RoundRobinPool
akka.dispatch.Await
scala.concurrent.Await
b)akka.dispatch.Future
scala.concurrent.Future
c)(≤2.4) _system.shutdown()
_system.terminate()
a) Akka version 2.6.xSee the online documentation for further informations:
b) ObjectAwait
is defined in filescala/concurrent/package.scala
since Scala 2.10.
c) Trait/objectFuture
is defined in filescala/concurrent/Future.scala
since Scala 2.10.
[2] FirstAkkaApplication: 2 versions ↩
-
We actually provide two versions of
FirstAkkaApplication
: -
- in
FirstAkkaApplication
the 4 actors are defined as subclasses ofUntypedAbstractActor
and do overwrite methodonReceive(Object message)
.public class MapActor extends UntypedAbstractActor { // ... @Override public void onReceive(Object message) throws Exception { if (message instanceof String) { // ... } else unhandled(message); } }
- in
FirstAkkaApplication2
the 4 actors are defined as subclasses ofakka.actor.AbstractActor
and do implement methodcreateReceive()
.public class MapActor extends AbstractActor { // ... public Receive createReceive() { return receiveBuilder() .match(String.class, word -> { /* ... */ }) .matchAny(message -> { unhandled(message); }) .build(); } }
- in