Professional Documents
Culture Documents
(Download PDF) Akka in Action 1St Edition Raymond Roestenburg Online Ebook All Chapter PDF
(Download PDF) Akka in Action 1St Edition Raymond Roestenburg Online Ebook All Chapter PDF
Roestenburg
Visit to download the full and correct content document:
https://textbookfull.com/product/akka-in-action-1st-edition-raymond-roestenburg/
More products digital (pdf, epub, mobi) instant
download maybe you interests ...
https://textbookfull.com/product/akka-essentials-1st-edition-
kumar-gupta-munish/
https://textbookfull.com/product/corruption-1st-edition-raymond-
fisman/
https://textbookfull.com/product/reactive-web-applications-
covers-play-akka-and-reactive-streams-1st-edition-manuel-
bernhardt/
https://textbookfull.com/product/docker-in-action-1st-edition-
jeff-nickoloff/
Scalatra in Action 1st Edition Dave Hrycyszyn
https://textbookfull.com/product/scalatra-in-action-1st-edition-
dave-hrycyszyn/
https://textbookfull.com/product/mesos-in-action-1st-edition-
roger-ignazio/
https://textbookfull.com/product/nim-in-action-1st-edition-
dominik-picheta/
https://textbookfull.com/product/redux-in-action-1st-edition-
marc-garreau/
https://textbookfull.com/product/elm-in-action-1st-edition-
richard-feldman/
Raymond Roestenburg
Rob Bakker
Rob Williams
MANNING
Akka in Action
RAYMOND ROESTENBURG
ROB BAKKER
ROB WILLIAMS
MANNING
SHELTER ISLAND
For online information and ordering of this and other Manning books, please visit
www.manning.com. The publisher offers discounts on this book when ordered in quantity.
For more information, please contact
Special Sales Department
Manning Publications Co.
20 Baldwin Road
PO Box 761
Shelter Island, NY 11964
Email: orders@manning.com
Many of the designations used by manufacturers and sellers to distinguish their products are
claimed as trademarks. Where those designations appear in the book, and Manning
Publications was aware of a trademark claim, the designations have been printed in initial caps
or all caps.
Recognizing the importance of preserving what has been written, it is Manning’s policy to have
the books we publish printed on acid-free paper, and we exert our best efforts to that end.
Recognizing also our responsibility to conserve the resources of our planet, Manning books
are printed on paper that is at least 15 percent recycled and processed without the use of
elemental chlorine.
ISBN 9781617291012
Printed in the United States of America
1 2 3 4 5 6 7 8 9 10 – EBM – 21 20 19 18 17 16
brief contents
1 ■ Introducing Akka 1
2 ■ Up and running 28
3 ■ Test-driven development with actors 49
4 ■ Fault tolerance 66
5 ■ Futures 92
6 ■ Your first distributed Akka app 118
7 ■ Configuration, logging, and deployment 147
8 ■ Structural patterns for actors 166
9 ■ Routing messages 188
10 ■ Message channels 213
11 ■ Finite-state machines and agents 233
12 ■ System integration 254
13 ■ Streaming 281
14 ■ Clustering 322
15 ■ Actor persistence 354
16 ■ Performance tips 388
17 ■ Looking ahead 416
iii
contents
preface xi
acknowledgments xii
about this book xiii
1 Introducing Akka
1.1 What is Akka? 4
1
messages 17
1.6 Actors: one programming model to rule up and out 18
An asynchronous model 19 ■
Actor operations 20
1.7 Akka actors 23
ActorSystem 23 ActorRef, mailbox, and
■
v
vi CONTENTS
2 Up and running
2.1
28
Clone, build, and test interface 29
Build with sbt 30 ■
Fast-forward to the GoTicks.com REST
server 31
2.2 Explore the actors in the app 34
Structure of the app 35 The actor that handles the sale:
■
4 Fault tolerance
4.1
66
What fault tolerance is (and what it isn’t) 66
Plain old objects and exceptions 68 ■
Let it crash 73
4.2 Actor lifecycle 76
Start event 77 Stop event 77 Restart event 78
■ ■
lifecycle 81
4.3 Supervision 82
Supervisor hierarchy 83 ■
Predefined strategies 85
Custom strategies 86
4.4 Summary 91
5 Futures
5.1
92
Use cases for futures 93
5.2 In the future nobody blocks 97
Promises are promises 101
5.3 Futuristic errors 104
CONTENTS vii
13 Streaming
13.1
281
Basic stream processing 282
Copying files with sources and sinks 286 Materializing ■
a BidiFlow 299
13.2 Streaming HTTP 302
Receiving a stream over HTTP 302 Responding with ■
14 Clustering 322
14.1 Why use clustering? 323
14.2 Cluster membership 325
Joining the cluster 325 ■
Leaving the cluster 332
14.3 Clustered job processing 337
Starting the cluster 340 Work distribution using
■
16 Performance tips
16.1
388
Performance analysis 389
System performance 389 ■
Performance parameters 391
16.2 Performance measurement of actors 393
Collect mailbox data 394 ■
Collecting processing data 400
16.3 Improving performance by addressing bottlenecks 401
16.4 Configure dispatcher 403
Recognizing thread pool problems 403 Using multiple ■
17 Looking ahead
17.1
416
akka-typed module 417
17.2 Akka Distributed Data 420
17.3 Summary 420
index 423
preface
Writing good, concurrent, and distributed applications is hard. Having just finished a
project that demanded a lot of low-level concurrency programming in Java, I was on
the lookout for simpler tools for the next project, which promised to be even more
challenging.
In March 2010 I noticed a tweet by Dean Wampler that made me look into Akka:
After some investigation into the source code and building a prototype, we decided to
use Akka. It was immediately apparent that this new programming model would really
simplify the problems we experienced in the previous project.
I convinced Rob Bakker to join me in a bleeding-edge technology adventure, and
together we took the leap to build our first project with Scala and Akka. We reached
out early to Jonas Bonér (creator of Akka) for help, and later found out that we were
among the first-known production users of Akka. We completed the project, and
many others followed; the benefits of using Akka were obvious every time.
In those days, there wasn’t a lot of information available online, so I decided to
start blogging about it as well as contribute to the Akka project.
I was completely surprised when I was asked to write this book. I asked Rob Bakker
if he wanted to write the book together. Later, we realized we needed more help, and
Rob Williams joined us. He had been building projects with Java and Akka.
We’re happy that we could finally finish this book and write about a version of Akka
(2.4.9) that really provides a comprehensive set of tools for building distributed and
concurrent applications. We’re grateful that so many MEAP readers gave us feedback
over time. The tremendous support from Manning Publications was invaluable for us
as first-time authors.
One thing that we all agreed on and had experienced before using Akka is that
writing distributed and concurrent applications on the JVM needed better, simpler
tools. We hope that we will convince you that Akka provides just that.
RAYMOND ROESTENBURG
xi
acknowledgments
It took a lot of time to write this book. During that time, many people have helped us
out and we thank them for the time they contributed. To all the readers who bought
the MEAP edition of the book, thank you for all the feedback that greatly improved
this book and for your ongoing patience over the years. We hope you will enjoy the
final result and that you learned a lot during the MEAP process.
Special thanks go out to members of the Akka core team, specifically Jonas Bonér,
Viktor Klang, Roland Kuhn, Patrik Nordwall, Björn Antonsson, Endre Varga, and
Konrad Malawski, who all provided inspiration and invaluable input.
We also want to thank Edwin Roestenburg and CSC Traffic Management in the
Netherlands, who trusted us enough to start using Akka for mission-critical projects
and provided an incredible opportunity for us to gain our initial experience with
Akka. We also want to thank Xebia for the work hours Ray could spend on the book
and for providing an incredible workplace for furthering experience with Akka.
We thank Manning Publications for placing their trust in us. This is our first book,
so we know this was a high-risk venture for them. We want to thank the following staff
at Manning for their excellent work: Mike Stephens, Jeff Bleiel, Ben Berg, Andy
Carroll, Kevin Sullivan, Katie Tennant, and Dottie Marsico.
Our thanks to Doug Warren, who gave all chapters a thorough technical proof-
read. Many other reviewers provided us with helpful feedback during the writing and
development process: Andy Hicks, David Griffith, Dušan Kysel, Iain Starks, Jeremy
Pierre, Kevin Esler, Mark Janssen, Michael Schleichardt, Richard Jepps, Robin Percy,
Ron Di Frango, and William E. Wheeler.
Last but not least, we want to thank the significant people in our lives who sup-
ported us as we worked on the book. Ray thanks his wife Chanelle, and Rob Williams
thanks his mom, Gail, and Laurie.
xii
about this book
This book introduces the Akka toolkit and explains its most important modules. We
focus on the actor programming model and the modules that support actors for
building concurrent and distributed applications. Throughout the book, we take time
to show how code can be tested, which is an important aspect of day-to-day software
development. We use the Scala programming language in all our examples.
After the basics of coding and testing actors, we look at all the important aspects
that you will encounter when building a real-world application with Akka.
Intended audience
This book is intended for anyone who wants to learn how to build applications with
Akka. The examples are in Scala, so it’s expected that you already know some Scala or
are interested in learning some Scala as you go along. You’re expected to be familiar
with Java, as Scala runs on top of the JVM.
Roadmap
The book includes seventeen chapters.
Chapter 1 introduces Akka actors. You’ll learn how the actor programming model
solves a couple of key issues that traditionally make scaling applications very hard.
Chapter 2 dives directly into an example HTTP service built with Akka to show how
quickly you can get a service up and running in the cloud. It gives a sneak peek into
what you’ll learn in chapters to come.
Chapter 3 is about unit testing actors using ScalaTest and the akka-testkit module.
Chapter 4 explains how supervision and monitoring make it possible to build reli-
able, fault-tolerant systems out of actors.
Chapter 5 introduces futures, extremely useful and simple tools for combining
function results asynchronously. You’ll also learn how to combine futures and actors.
Chapter 6 is about the akka-remote module, which makes it possible to distribute
actors across a network. You’ll also learn how you can unit test distributed actor systems.
xiii
xiv ABOUT THIS BOOK
Chapter 7 explains how the Typesafe Config Library is used to configure Akka. It
also details how you can use this library to configure your own application components.
Chapter 8 details structural patterns for actor-based applications. You’ll learn how
to implement a couple of classic enterprise integration patterns.
Chapter 9 explains how to use routers. Routers can be used for switching, broad-
casting, and load balancing messages between actors.
Chapter 10 introduces the message channels that can be used to send messages
from one actor to another. You’ll learn about point-to-point and publish-subscribe
message channels for actors. You’ll also learn about dead-letter and guaranteed-
delivery channels.
Chapter 11 discusses how to build finite state machine actors with the FSM module
and also introduces agents that can be used to share state asynchronously.
Chapter 12 explains how to integrate with other systems. In this chapter, you’ll
learn how to integrate with various protocols using Apache Camel and how to build
an HTTP service with the akka-http module.
Chapter 13 introduces the akka-stream module. You’ll learn how to build stream-
ing applications with Akka. This chapter details how to build a streaming HTTP service
that processes log events.
Chapter 14 explains how to use the akka-cluster module. You’ll learn how to
dynamically scale actors in a network cluster.
Chapter 15 introduces the akka-persistence module. In this chapter, you’ll learn
how to record and recover durable state with persistent actors and how to use the
cluster singleton and cluster sharding extensions to build a clustered shopping cart
application.
Chapter 16 discusses key parameters of performance in actor systems and provides
tips on how to analyze performance issues.
Chapter 17 looks ahead to two upcoming features that we think will become very
important: the akka-typed module that makes it possible to check actor messages at
compile time, and the akka-distributed-data module, which provides distributed in-
memory state in a cluster.
Software requirements
Scala is used in all examples, and all code is tested with Scala 2.11.8. You can find Scala
here: http://www.scala-lang.org/download/.
ABOUT THIS BOOK xv
Be sure to install the latest version of sbt (0.13.12 as of this writing); if you have an
older version of sbt installed, you might run into issues. You can find sbt here: http://
www.scala-sbt.org/download.html.
Java 8 is required by Akka 2.4.9, so you’ll need to have it installed as well. It can be
found here: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-down-
loads-2133151.html.
Author Online
Purchase of Akka in Action includes free access to a private web forum run by Manning
Publications, where you can make comments about the book, ask technical questions,
and receive help from the authors and from other users. To access the forum and sub-
scribe to it, point your web browser to https://www.manning.com/books/akka-in-
action. This page provides information on how to get on the forum after you’re regis-
tered, what kind of help is available, and the rules of conduct on the forum.
Manning’s commitment to our readers is to provide a venue where a meaningful
dialogue between individual readers and between readers and the authors can take
place. It isn’t a commitment to any specific amount of participation on the part of the
authors, whose contribution to the AO forum remains voluntary (and unpaid). We
suggest you try asking the authors some challenging questions, lest their interest stray!
The AO forum and the archives of previous discussions will be accessible from the
publisher’s website as long as the book is in print.
ROB WILLIAMS is the founder of ontometrics, a practice focused on Java solutions that
include machine learning. He first used actor-based programming a decade ago and
has used it for several projects since.
maps and atlases, especially of North America. His work as a mapmaker sparked an
interest in local dress customs of the lands he surveyed and mapped, an interest that is
brilliantly displayed in this four-volume collection.
Fascination with faraway lands and travel for pleasure were relatively new phenom-
ena in the late-eighteenth century, and collections such as this one were popular,
introducing both the tourist as well as the armchair traveler to the inhabitants of
other countries. The diversity of the drawings in Jefferys’ volumes speaks vividly of the
uniqueness and individuality of the world’s nations some 200 years ago. Dress codes
have changed since then, and the diversity by region and country, so rich at the time,
has faded away. It is now often hard to tell the inhabitant of one continent from
another. Perhaps, trying to view it optimistically, we have traded a cultural and visual
diversity for a more varied personal life, or a more varied and interesting intellectual
and technical life.
At a time when it is hard to tell one computer book from another, Manning cele-
brates the inventiveness and initiative of the computer business with book covers
based on the rich diversity of regional life of two centuries ago, brought back to life by
Jefferys’ pictures.
12 CHAPTER 1 Introducing Akka
Method
Google Network Google
call
contacts client contacts server
TeamFinder
Outlook Outlook
Method contacts client contacts server
call Network
We find out that one of the services fails often and in the worst possible way—we get
long timeouts, or traffic has slowed down to only a few bytes per minute. And because
the web services are accessed one after the other, waiting for a response, the lookup
fails after a long time even though many valid suggestions could have been made to
the user from the service that worked just fine.
Even worse, though we collected our database methods in DAOs and the contacts
lookup in a TeamFinder object, the controllers are calling these methods like any
other. This means that sometimes a user lookup ends up right between two database
methods, keeping connections open longer than we want, eating up database
resources. If the TeamFinder fails, everything else that’s part of the same flow in the
application fails as well. The controller will throw an exception and won’t be able to
continue. How do we safely separate the TeamFinder from the rest of the code?
It’s time for another rewrite, and it doesn’t look like the complexity is improving.
In fact, we’re now using four programming models: one for the in-memory threads,
one for the database operations, one for the Mentions message queue, and one for
the contacts web services.
How do we move from 3 servers to, say, 10, and then to 100 servers, if this should
be required? It’s obvious that this approach doesn’t scale well: we need to change
direction with every new challenge.
In the next section, you’ll find out if there’s a design strategy that doesn’t require
us to change direction with every new challenge.
Another random document with
no related content on Scribd:
that they implied was, if not acknowledged, at any rate felt, witness
the tapestry backgrounds with which the real depth of space was
covered up by a pious awe that disguised what it dared not exhibit.
We have seen how just at this time, when the Faustian (German-
Catholic) Christianity attained to consciousness of itself through the
institution of the sacrament of Contrition—a new religion in the old
garb—the tendency to perspective, colour, and the mastering of
aerial space in the art of the Franciscans[313] transformed the whole
meaning of painting.
The Christianity of the West is related to that of the East as the
symbol of perspective to the symbol of gold-ground—and the final
schism took place almost at the same moment in Church and in Art.
The landscape-background of the depicted scene and the dynamic
infiniteness of God were comprehended at the same moment; and,
simultaneously with the gold ground of the sacred picture, there
vanished from the Councils of the West that Magian, ontological
problem of Godhead which had so passionately agitated Nicæa,
Ephesus, Chalcedon and all the Councils of the East.