Play Framework 2.1 Architecture

Play 2 is completely different from the previous versions. When the team decided to change the framework I was frustrated just like the rest of the community.. “why would they change something so good” everyone was wondering! Anyway, I decided to follow the new version as I thought they would have a good reason for this radical change, and they did.

Asynchronous Web
Play 1.x  was following the steps of django and rails, too many request serving them as fast as possible. It was working on the assumption that all request will be short (just asking for some data, or sending a form) and for more time consuming requests a scheduled job could be used. The new architecture is based on the actor model, powered by akka. Actors are lightweight entities that processes messages asynchronously. In the new Play every request is considered to be a long lasting one and it seems that the best way to handle persistent connections is the event model.

Architecture Overview
Play 2.x still uses the MVC pattern. There is a Controller that handles the incoming request with the help of the Model and the View it will build and return the response. Model is of course the domain model with the data and logic and the View is the templating system that can be fed with model data to produce the response. What’s interesting about the Model is that it is completely agnostic of the view layer, well, after all that’s the point of mvc.

Just like the previous version Play 2.x comes with an embedded HTTP server which specifically is the jBoss Netty and it’s non-blocking IO server.  Netty is integrated into the framework and it’s completely transparent. The benefits of integrating a nonblocking IO server is that the framework provides an HTTP API that supports out of the box asynchronous web programming.  When an http request comes in Netty will handle it and forward the data to play framework, the framework will processes the data and generate some response which Netty will deliver back to the client.

Incoming Request
The following image illustrates how play handles the normal requests. A client makes a request for a resource in this example the request is the following http://www.example.com/delete/234. After the domain name follows the route which is mapped to an Action in this case it is mapped to the remove action in a Controller class. The Controller has access to the model class and its methods and performs a delete on the data. The model synchronises with the database and returns back to the controller the status of the deletion (success or fail). The controller then renders the view and sends the Response to the client.

play-architecture

Incoming Request Asynchronous Action
The following image illustrates how play handles requests in an asynchronous way. A client makes a request for a resource in this example the request is the following http://www.example.com/delete/234. After the domain name follows the route which is mapped to an Action in this case it is mapped to the remove action in a Controller class. So far nothing has changes from the previous method. The Controller has access to the model class and its methods and performs a delete on the data but in this case the operation last longer because a lot of business rules need to be checked before deletion. To avoid blocking the Controller sends back to the client a Future[Result] and it moves on with other possible requests. At this stage the client is waiting for the actual response. The model method completes and synchronises the state with the database and returns back to the controller the status of the deletion (success or fail). The controller then renders the view and sends the Response to the client fulfilling the future results.
PlayArchtectureAsyncRequest

You can learn a lot more about play framework at http://www.playframework.com/documentation/2.2.x/Home

Advertisements

Getting started with Scala

This is a list of Scala resources to get started and beyond.

1. Setting up the environment (installation guides):
* if you just want to experiment with scala there is no need to setup anything, you can use an online interactive interpreter like http://scalatutorials.com/ and http://www.simplyscala.com/.

Mac OS X guide
Windows 7 guide
Ubuntu guide

2. Editors and IDEs
There are plugins for all major IDEs:

Scala IDE for eclipse
Scala plugin for NetBeans
Scala Plugin for InteliJ IDEA

Syntax highlighting plugins are available for jEdit, Notepad++, TextMate, TextWrangler etc.

3. Online Free Resources
Scala School @ twitter: http://twitter.github.io/scala_school/
http://www.tutorialspoint.com/scala/index.htm this is a tutorial for beginners.
There is a free course at coursera: Functional Programming Principles in Scala don’t miss it but I suggest that you go through the basics first.

4. Scala and Functional Programming Books
– Scala
Programming in Scala 2nd Edition (I really liked this book, but one of my colleagues found it a little to academic and moved on to the next book)
Scala for the Impatient
Scala in Depth
Scala in Action
Testing in Scala (if you are serious about Scala this book is a must)

-Functional Programming
Functional Programming in Scala (I included this book here because it’s mainly about functional programming and less about Scala, it’s a five star recommendation though)
Purely Functional Data Structures (More theoretical and all examples are in ML and Haskel but it’s easy to translate them to Scala)
Functional Programming for the Object-Oriented Programmer (if you come from object oriented world this book will help you)

5. Scala for the Web
Play Framework 2.x (This is my favourite framework, all my web projects are based on play)
Lift (I have never used it in real project but it looks really good)
Scalatra (I don’t know much about it but I will give it a try soon)
Skinny (this is Scala on Rails and it’s in alpha version)

6. Scala Object Relational Mapping
Slick (I have been using slick with play and I’m quite happy but I’m sure all the others are equally good)
Squeryl
Circumflex
Lift ORM (included in the lift framework)

7. Scala for NoSQL
The list on nosqls is really big these are the ones that I have been using
MongoDB
Redis
Cassandra
Hadoop
ElasticSearch

8. Other
Akka (A toolkit – and runtime – for highly concurrent, distributed, and fault tolerant applications)
Scalaz (library for functional programming to complement those provided by Scala itself)

Final note: all software mentioned here is open source so you can get your hands dirty.. fork them and enjoy scala.