Introduction to Play 2 for Java
Java has enough web frameworks, so why should you learn a new one? Play is not a typical Java web framework: It does not support Java EE; it does not support servlets; Play applications are not deployed to a servlet container or application server; and it isn’t even written in Java, so what makes it special?
Play changes the way that Java developers think about developing web applications: The Java Servlet API is very Java-centric whereas Play is very web-centric. The paradigm shift is to transition from thinking in terms of Java APIs and the abstraction layer on top of HTTP that it provides and instead thinking in terms of HTTP directly. Play allows you to think about web resources and the HTTP verbs used to manipulate these resources; Play allows you to build applications quickly and redeploy applications on-the-fly rather than going through a lengthy build and deployment cycle; and Play runs in a stand-alone JVM and provides the management capabilities that you need to maintain your application.
When I started exploring the Play 2 Framework, the main thing that intrigued me was its support for asynchronous messaging. In a typical web application, the web container maintains a thread pool and each request is handled by a single thread. This means that if you have 50 threads, then you can handle 50 simultaneous requests. Play abstracts the threading model from you and allows you to communicate with other components asynchronously. If you need to wait for a response from another component, your application can give up its thread and allow another request to use it until the response arrives, at which point Play will give you a thread to complete your response and return the result to the caller. So in other words, if your application makes calls to other components, then 50 threads does not equate to support for 50 simultaneous requests; you can support far more than that! This is one of the reasons that LinkedIn decided to choose the Play Framework, and knowing its scalability requirements, their decision lends a lot of credibility to Play.
About Play
The first version of the Play framework was written in Java, but the second version was rewritten in Scala (and still works equally well with Java applications). Play implements an asynchronous message-passing paradigm on top of Akka and is part of the Typesafe stack of technologies. It is part of a new domain of application frameworks designed for building reactive applications. A reactive application has the following properties:
- Event-driven: Reactive applications are event-driven and support parallel and asynchronous processing of messages or events.
- Scalable: Reactive applications are designed to scale elastically, such as in a cloud-based environment, so that more compute instances can be used during peak load and less compute instances can be used during light periods.
- Resilient: Reactive applications are designed to recover from errors and automatically repair themselves to preserve the business function they are solving.
- Responsive: Reactive applications are usually single-page web applications providing instant feedback.
These are ambitious goals and, as such, require a new approach to solving problems - fortunately Play and Akka provide this much needed new approach. This article series can help you leverage these new technologies to develop reactive applications.
Setting Up a Play Environment
In this section we set up a Play environment and build our first Play application. First, download Play from their website; you have a couple options:
- Typesafe Activator: This is a utility that helps you get Play, Akka, and Scala set up.
- Zip file: A self-contained zip file with the Play Framework and runtime environment.
For this example we’ll just download and decompress the zip file. After you have this directory set up, you might want to add the Play home directory to your PATH environment variable. In Windows you can do this through the Control Panel, System, and Advanced System Settings. On Linux and Mac you can do this by modifying your .bash_profile (Mac) or .bashrc (Linux) file. You can test your configuration by executing the play command:
Stevens-MacBook-Pro:~ shaines$ play _ _ __ | | __ _ _ _ | '_ \| |/ _' | || | | __/|_|\____|\__ / |_| |__/ play 2.2.2 built with Scala 2.10.3 (running Java 1.7.0_25), http://www.playframework.com This is not a play application! Use `play new` to create a new Play application in the current directory, or go to an existing application and launch the development console using `play`. You can also browse the complete documentation at http://www.playframework.com. Stevens-MacBook-Pro:~ shaines$
You should see something similar to this output if your environment is set up correctly. (The first time you run play you might see it downloading resources.) Note that this output identifies that “This is not a play application!” so we’ll need to create a new play application.