Web application development using Play
Framework (with Java)
Tehran JUG meeting February-2015
By: Saeed Zarinfam
History
ïŹ Play 1 Created By Guillaume Bort at Zengularity SA
in 2007
ïŹ Play 2 release in 2012 by Typesafe (rewrite using
Scala)
ïŹ Play 1 vs Play 2
ïŹ Part of Typesafe Activator
Introduction
‱ Inspired by Ruby on Rails and Django
‱ Run on JVM (use java library)
‱ MVC
‱ Stateless
‱ Hot code reloading (in development)
‱ Convention over configuration
‱ Lightweight
‱ Asynchronous and Non-blocking I/O
‱ Java 8 support
Architecture
‱ MVC architecture
‱ Netty as web server (no servlet container)
‱ Sbt as build tools (Ivy dependency manager)
‱ Stateless
Requirements
ïŹ Install Typesafe Activator (or sbt)
ïŹ
JDK 6 or later
ïŹ
activator new my-first-app play-java
ïŹ Open project in IDE (Eclipse and Intellij)
ïŹ
Run using
– command line (activator run)
– Activator console (activator)
– Activator UI (activator ui)
– Interactive Console (Only in Scala)
Activator UI
Project structure
‱ App
‱ Public
‱ Conf
‱ build.sbt
‱ Scala and Java
‱ ...
Route
‱ Scala syntax
‱ Optional parameters
‱ Reverse routing
‱ REST friendly
Controller
‱ Actions return Result
‱ Static method
‱ Use dependency injection (JSR 330 in Play 3)
‱ HTTP manipulation (body parsers)
– REST service (JSON and XML)
View
ïŹ Type safe template engine
ïŹ Scala Template (Twirl inspired by ASP.NET
Razor)
ïŹ Http form handling
Session
ïŹ Session data are not stored in the server
ïŹ Session data added to each subsequent HTTP
Request, using Cookie.
ïŹ Cookie are signed with a secret key so the
client can’t modify the cookie data or it will be
invalidated
ïŹ Use cache for sever side session data
Assets
ïŹ Public Assets
ïŹ WebJars
ïŹ Assets controller
ïŹ Etag
ïŹ
Gzip
ïŹ Caching
ïŹ Managed Assets
ïŹ CoffeeScript
ïŹ LESS CSS
ïŹ
Sbt-web plugins
ïŹ
js-engine plugin (able to execute plugins written to the Node API either
within the JVM via the excellent Trireme)
Hot code reloading (in
development)
ïŹ It is possible because Play is stateless and tightly integration between Play and sbt
ïŹ Classloader hierarchy manage by sbt
ïŹ
Tricky (You don't need JRebel)
ïŹ For each request reloader check if there's any code changes
ïŹ Compile changed codes
ïŹ remove the old application classloader
ïŹ create a new application classloader with the updated classes.
ïŹ restart application (only application, no need to restart the JVM)
Accessing Database
ïŹ JDBC
ïŹ JPA (2.0)
ïŹ Ebeans (default ORM)
ïŹ Database evolutions
ïŹ Connection Pool
ïŹ Transaction
ïŹ In-memory H2 database
Asynchronous
ïŹ Actions are asynchronous by default
ïŹ Each open connection does not need a thread
ïŹ application code should avoid blocking in
controllers
Other APIs
ïŹ
Integration with Akka
ïŹ
WebSockets and Comet sockets
ïŹ
Cache API
ïŹ
Internationalization
ïŹ
Interceptors
ïŹ
Filters(only in Scala)
ïŹ
JSON API (jackson)
ïŹ
XML API
ïŹ
Job scheduling (Using Akka)
ïŹ
Logging API (Logback)
Testing
ïŹ
Ready to use application stubs for test (several helper methods)
ïŹ
Junit
ïŹ
FEST assertions
ïŹ
Mock
ïŹ
Test every part of your application (router, action, controller and ...)
ïŹ
Testing with a browser using FluentLenium ( Selenium WebDriver
wrapper)
Plugins
ïŹ Several plugins
– Mail
– Open ID
– CRUD
– Security
Deployment
ïŹ Compile all class and assets in application
ïŹ Activator start
ïŹ Activator stage (without any dependency on Play)
ïŹ Activator dist (produces a ZIP file without any
dependency on Play)
ïŹ SBT Native Packager plugin(activator universal:package-
bin)
– MSI, OS X disk image, RPM, DEB and 

ïŹ Publishing to a Maven (or Ivy) repository
Scaleability
ïŹ Scale horizontally easily (using load balancer)
ïŹ Integrate with Akka clustring and remoting
features
ïŹ Asynchronous from bottom to top
Reactive
ïŹ
Play Iteratee library (Scala Library)
ïŹ
ReactiveMongo
Non-blocking
ïŹ ReturnPromise<Result>Promise<Result>
ïŹ Intensive computation will just be run on another thread
(thread pool in separate execution context ).
ïŹ The web client will be blocked while waiting for the
response, but nothing will be blocked on the server
‫ŰȘێک۱‏
zarinfam.s@gmail.com
http://javabyab.com

Web application development using Play Framework (with Java)

  • 1.
    Web application developmentusing Play Framework (with Java) Tehran JUG meeting February-2015 By: Saeed Zarinfam
  • 2.
    History ïŹ Play 1Created By Guillaume Bort at Zengularity SA in 2007 ïŹ Play 2 release in 2012 by Typesafe (rewrite using Scala) ïŹ Play 1 vs Play 2 ïŹ Part of Typesafe Activator
  • 3.
    Introduction ‱ Inspired byRuby on Rails and Django ‱ Run on JVM (use java library) ‱ MVC ‱ Stateless ‱ Hot code reloading (in development) ‱ Convention over configuration ‱ Lightweight ‱ Asynchronous and Non-blocking I/O ‱ Java 8 support
  • 4.
    Architecture ‱ MVC architecture ‱Netty as web server (no servlet container) ‱ Sbt as build tools (Ivy dependency manager) ‱ Stateless
  • 5.
    Requirements ïŹ Install TypesafeActivator (or sbt) ïŹ JDK 6 or later ïŹ activator new my-first-app play-java ïŹ Open project in IDE (Eclipse and Intellij) ïŹ Run using – command line (activator run) – Activator console (activator) – Activator UI (activator ui) – Interactive Console (Only in Scala)
  • 6.
  • 7.
    Project structure ‱ App ‱Public ‱ Conf ‱ build.sbt ‱ Scala and Java ‱ ...
  • 8.
    Route ‱ Scala syntax ‱Optional parameters ‱ Reverse routing ‱ REST friendly
  • 9.
    Controller ‱ Actions returnResult ‱ Static method ‱ Use dependency injection (JSR 330 in Play 3) ‱ HTTP manipulation (body parsers) – REST service (JSON and XML)
  • 10.
    View ïŹ Type safetemplate engine ïŹ Scala Template (Twirl inspired by ASP.NET Razor) ïŹ Http form handling
  • 11.
    Session ïŹ Session dataare not stored in the server ïŹ Session data added to each subsequent HTTP Request, using Cookie. ïŹ Cookie are signed with a secret key so the client can’t modify the cookie data or it will be invalidated ïŹ Use cache for sever side session data
  • 12.
    Assets ïŹ Public Assets ïŹWebJars ïŹ Assets controller ïŹ Etag ïŹ Gzip ïŹ Caching ïŹ Managed Assets ïŹ CoffeeScript ïŹ LESS CSS ïŹ Sbt-web plugins ïŹ js-engine plugin (able to execute plugins written to the Node API either within the JVM via the excellent Trireme)
  • 13.
    Hot code reloading(in development) ïŹ It is possible because Play is stateless and tightly integration between Play and sbt ïŹ Classloader hierarchy manage by sbt ïŹ Tricky (You don't need JRebel) ïŹ For each request reloader check if there's any code changes ïŹ Compile changed codes ïŹ remove the old application classloader ïŹ create a new application classloader with the updated classes. ïŹ restart application (only application, no need to restart the JVM)
  • 14.
    Accessing Database ïŹ JDBC ïŹJPA (2.0) ïŹ Ebeans (default ORM) ïŹ Database evolutions ïŹ Connection Pool ïŹ Transaction ïŹ In-memory H2 database
  • 15.
    Asynchronous ïŹ Actions areasynchronous by default ïŹ Each open connection does not need a thread ïŹ application code should avoid blocking in controllers
  • 16.
    Other APIs ïŹ Integration withAkka ïŹ WebSockets and Comet sockets ïŹ Cache API ïŹ Internationalization ïŹ Interceptors ïŹ Filters(only in Scala) ïŹ JSON API (jackson) ïŹ XML API ïŹ Job scheduling (Using Akka) ïŹ Logging API (Logback)
  • 17.
    Testing ïŹ Ready to useapplication stubs for test (several helper methods) ïŹ Junit ïŹ FEST assertions ïŹ Mock ïŹ Test every part of your application (router, action, controller and ...) ïŹ Testing with a browser using FluentLenium ( Selenium WebDriver wrapper)
  • 18.
    Plugins ïŹ Several plugins –Mail – Open ID – CRUD – Security
  • 19.
    Deployment ïŹ Compile allclass and assets in application ïŹ Activator start ïŹ Activator stage (without any dependency on Play) ïŹ Activator dist (produces a ZIP file without any dependency on Play) ïŹ SBT Native Packager plugin(activator universal:package- bin) – MSI, OS X disk image, RPM, DEB and 
 ïŹ Publishing to a Maven (or Ivy) repository
  • 20.
    Scaleability ïŹ Scale horizontallyeasily (using load balancer) ïŹ Integrate with Akka clustring and remoting features ïŹ Asynchronous from bottom to top
  • 21.
    Reactive ïŹ Play Iteratee library(Scala Library) ïŹ ReactiveMongo
  • 22.
    Non-blocking ïŹ ReturnPromise<Result>Promise<Result> ïŹ Intensivecomputation will just be run on another thread (thread pool in separate execution context ). ïŹ The web client will be blocked while waiting for the response, but nothing will be blocked on the server
  • 23.