4. What is Akka?
• Toolkit for building ‘reactive’ applications
• At its core a JVM implementation of ‘actor pattern’
• Grown to include:
• IO/Networking
• Streams
• http
• agents
• Written in Scala, supports Java (Java 8 support ‘experimental' in some cases)
• http://akka.io
5. Actor Model
• Created by
Ericsson for
Erlang (OTP)
• Hierarchical,
highly resilient,
message passing
• Millions of actors
in one system
(300 bytes each)
6. akka-streams
• A ‘reactive’ data streaming API
• Built on akka-actors
• Gives you methods to deal with back-pressure
• Key terms
• Stream - active process that involves moving & transforming data
• Element - processing unit of a stream
• Back-pressure - flow control; notify consumers/producers of current ability
• Graph - stream processing topology
• Processing Stage - building blocks that build up a graph: i.e. map, filter, etc.
7. Akka Streams Model
• Source - processing stage with
exactly one output
• Sink - processing stage with
exactly one input
• Flow - processing stage with
exactly one input and output
• Runnable Graph - a flow with
both ends attached to a Source
and a Sink which can be run()
• Learn more! http://doc.akka.io/
docs/akka/2.4.7/scala/stream/
stream-flows-and-basics.html
8. akka-http
• Full server and client side implementation of HTTP on top of akka-actor & akka-stream
• Not a web framework!
• General toolkit for providing and consuming HTTP services
• Provides:
• a high level and low level API
• Routing DSL
• web socket support (for client & server side!!)
• an awesome testkit!
• Provided for Scala & Java 8
• Formerly known as Spray
9. Akka HTTP Model
Concept Description
Http
Provides helpers to create the streams for processing server responses or client requests (http or
https supported)
HttpRequest
HTTP request. Requires at least an URI to instantiate and will default to method GET; can choose
alternate HttpMethod and attach Headers and a request entity
HttpResponse
Contains information related to the HTTP response, including content types, headers, status code
and response entity if applicable.
HttpEntity
Represents the body of the request or response as a Source[ByteString, Any]; commonly un-
marshalled to the desired type; Stream is processed asynchronously!
Headers
Strongly typed instances of all known HTTP 1.1 headers; Custom headers can be added by
extending appropriate API; no simple string headers! (error prone)
ContentTypes
Strongly typed instances of all commonly used ContentTypes; as with headers, custom
ContentTypes can be registered with the system; no simple string values
MediaTypes
Strongly typed instances of all commonly used MediaTypes; custom MediaTypes can be registered
with the system; no simple string values
Un-Marshalling
Key to turning your request/response entities to/from bytes for transport by the system. Out of the
box support for basics (i.e. String); modules add support for others (i.e. JSON)
RequestContext Wraps the HttpRequest and provides other helpers for working with requests
11. There is a better way…
• Routing DSL to the rescue…
• (Example)
12. Akka HTTP - Routes
• Server side code simplified with Routes
• A route is an alias for a function that turns a RequestContext into a
RouteResult
• type Route = RequestContext => Future[RouteResult]
• A route can do the following when it receives a request:
• complete the request
• reject the request (I don’t handle this request)
• fail the request (an actual error has occurred)
• do any type of asynchronous processing and return a Future[RouteResult]
13. Akka HTTP - The Routing
Tree
• Routes can be composed to build a tree
• Three basic operations
• transformation - delegates to an inner
route, but changes some property of the
request or response before it hands it off
• filtering - only processes routes
satisfying some criteria; rejects all others
• chaining - tries a second route if the first
was rejected
• ~ is the chaining operator
• put most specific cases up front, general
cases in the back (i.e. route 3 is a ‘catch all’
route)
14. Akka HTTP - Directives
• Directive is a building block for a route
• akka-http provides a number of pre-defined directives
• Custom directives can be built by composing existing directives with operators or by
extending the API
• form: name(args) { extractions => // inner route }
Operator Purpose Example
~ chain val route = a {} ~ b{} ~ c{}
| or val getOrPut = get | put
& and val getFoo = path(“foo”) & get
15. Akka HTTP - Commonly
Used Directives
Directive Description
path(pm : PathMatcher)
Matches the given path give the pattern in the
PathMatcher
get, put, post, delete, head, patch Common HTTP Methods
complete Completes a request; does not handle Futures
onComplete, onSuccess Completes a request and handles Futures
extract*
Extract various parts of the HttpResponse..
i.e. extractClientIP, extractHost, etc.
reject Rejects a route with a specific Rejection
entity
the HTTP Request entity; will un-marshall it to the given
type.
authenticate*
Numerous directives to support various types of HTTP
authentication, ie. OAuth2, Basic.
parameters Extract values for vary query parameters
16. Akka HTTP Examples
• Simple Routing DSL Example
• HTTP Microservice Example
• Simple Client Side GET
• Simple Client Side POST
17. Akka HTTP RouteTestkit
• If you build with the Routing DSL, use the test kit
to validate that it works as expected!
• ScalaTest based
• Makes use of the RequestBuilder to quickly build
up tests for your routes
• Example…
18. Akka HTTP - Much More!
• There’s a lot more…
• Custom Marshalling/Un-marshalling
• Case class extractions & validation
• Custom Headers (i.e X-CUSTOM…)
• Custom ContentTypes/MediaTypes
• Custom Rejection Handlers
• HTTPS
• WebSockets (client and server)
• Stand-Alone HTTP Layer Usage (Bidi)