Monday, 9 February 2015

Spray-can/Akka simple application

Spray?


The spray-can module of spray.io provides a framework with which you can build http servers and clients which easily integrate with your own akka system.

Note that according to the akka roadmap, in future releases of akka, spray will be built in and will be known as akka-http.

The Code



Under the covers


Spray request handling

When you start your spray app, spray generates an HttpListener actor that listens on port 8181 for incoming requests. For each client connection, spray spawns a new HttpServerConnection actor which receives a request from an open connection and passes an HttpEntity message to your listening actor (the first parameter to the Http.Bind method).

Binding

You must call bind from within an actor.

IO(Http) ! Http.Bind(self, interface = "localhost, port=8181")

This threw me for a bit because initially the first parameter I passed to Http.Bind was the actor that is handling my Http requests. However, when you call Http.Bind you send an async message off to trigger the bind, then spray sends a Tcp.Bound message back to the actor from which you called Http.Bind, not the actor that you pass in as the first parameter.

Try it out



curl localhost:8181/ping

No comments:

Post a Comment

Scala with Cats: Answers to revision questions

I'm studying the 'Scala with Cats' book. I want the information to stick so I am applying a technique from 'Ultralearning&#...