Book Image

Learning Play! Framework 2

By : Andy Petrella
Book Image

Learning Play! Framework 2

By: Andy Petrella

Overview of this book

<p>The Learning Play! Framework 2 has been created for web developers that are building web applications. The core idea is to focus on the HTTP features and to enable them through a simplification lens. Building a web application no longer requires a configuration phase, an environment setup, or a long development lifecycle - it's integrated!<br /><br />Learning Play! Framework 2 will enable any web developers to create amazing web applications taking advantage of the coolest features. It's the fastest way to dive into Play!, focusing on the capabilities by using them in a sample application. Although essentially Java based code, a Scala version is presented as well – giving an opportunity to see some Scala in action.<br /><br />After setting up the machine and learning some Scala, you will construct an application which builds from static to dynamic, before introducing a database. <br /><br />Then we'll focus on how data can be consumed and rendered in several ways. This will enable some real time communication through WebSocket and Server-Sent Event – on both server and client sides.</p> <p>The book will end with testing and deployment, which completes any web development project.</p>
Table of Contents (20 chapters)
Learning Play! Framework 2
Credits
About the Author
Acknowledgement
About the Reviewers
www.packtpub.com
Preface
Materials
Index

Underlying ideas and concepts


The Play! Framework internals are based on very light and trivial concepts. One of them is that the application should be separated from the Web by a single thin layer—its API.

Reactive

This framework helps in solving a fundamental problem in web-based applications: the reactivity of a web server within a distributed environment. In the following sections, we'll see which points are critical to building a sustainable and relevant solution.

NIO server

A Play! 2 application is completely stateless, that is, no state can be stored on the server to cover multi-request workflows. This fact will oblige applications to use different patterns or to use specific tools to deal with a state—such as a caching system.

For responsiveness, they took the opportunity to distribute a dedicated server as part of the stack, which is the non-blocking JBoss Netty server. This server is different from others because of its behavior. Indeed, rather than dealing with threads for every single request that is coming, it uses an event-based architecture where events are created when a request is effectively asking something or sending something to the server. That will help the server not to wait for a request to finish before switching to another, or in other words, to block a thread until the request has something to do.

This kind of behavior is contrary to the "one user, one thread" concept, which leads the server to only serve as many users as the number of threads it can deal with, no matter what they are doing.

Asynchronous

But the responsiveness is not yet accomplished if we only tackle it in the core, because a developer could block a thread on an external working process, such as a web service call.

In order to avoid developers doing that, Play! Framework 2 can declare a process to be asynchronous and leaves the server to choose when to process it or when to come back on it. For example, if a process is running on an external machine, but the local thread is simply waiting for an answer, this thread can be retrieved for other tasks until the remote call has returned.

Iteratee

However, that's still not enough, thanks to the Scala language and its ability to use functions as objects. Play! 2 uses Iteratee to add some responsiveness to handle request bodies themselves. So the body to be processed as an XML or a JSON is no more blocking the server because an Iteratee can pause its work until the server, which is responsive in itself, has some data.

Furthermore, it's composable, which adds a new value to the framework, because it prevents a body (that could be large) to be processed several times for transformation purposes (bytes to string to JSON, for instance).

Wrap up

Using Play! Framework 2 and by following their conventions, any application is ready for the Web, or more than that, it is ready for the cloud.