Play Framework
Mahmut Karakaya
Agenda
- Theory
- What Play Solves
- Architecture
- Play History
- Web Evolution
Agenda
- Practice
- Simple MVC operations
- NIO
- Scala
Waiting...
Threads
Factory classes
Play is not based on Java EE
MVC
Play
- full stack web framework for JVM
- fun and high productive
- change code hit reload ( forget jRebel )
- browser error reporting
- easy cloud deployment (e.g. Heroku)
- stateless
Play 0.x - 05.2007
Play 1.0 - 05.2008
Play 1.2 - 04.2011
Play 2.0 - 03.2012
Notable websites using Play
The Web Evolved
- Static (just html)
- Dynamic (get date from server on page :) )
* Structured (mvc, jsp, jsf )
- Realtime (ajax)
Realtime Web
- Polling (req - res)
- Long polling (req - res when changed)
- Server sent events (req - res -res -res)
- Websocket (req - res - res - req -res
whatever)
1 request = 1 thread
NIO 1 request != 1 thread
Demo
- Create new application
- Download latest activator
https://www.playframework.com/download
play new demo
play ~run
http://localhost:9000
IDE support
play eclipse
play idea
Application Layout
Controller
public class HelloWorld extends Controller{
public static Result index(){
return ok("Hello World");
}
}
conf / routes
GET /hello
controllers.HelloWorld.index()
Controller - add a parameter
public static Result helloName(String name){
return ok("Hello World: "+ name);
}
conf / routes - add a parameter
GET /helloName
controllers.HelloWorld.helloName(name)
http://localhost:9000/helloName?
name=TestUser
conf / routes - add a parameter to URL
GET /helloName/:name
controllers.HelloWorld.helloName(name)
http://localhost:9000/hello/TestUser
Controller - parameter type check
public class HelloWorld extends Controller{
public static Result helloNameAge(String name, int
age){
return ok("Hello World "+name +” you are ”
+age);
}
}
conf / routes - parameter type check
GET /hello/:name/:age
controllers.HelloWorld.helloNameAge(name:String,age:Int)
http://localhost:9000/hello/TestUser/10
Add a view
@(name: String, age:Int)
@main("Welcome to Play") {
<html>
<head></head>
<body>
<p>
Hello <b> @name</b>, you are <b>@age</b> years old
</p>
</body>
</html>
}
Controller - call view
public class HelloWorld extends Controller{
public static Result index(String name, int age){
return ok(views.html.helloWorld.render(name,age));
}
}
Error handling
WS lib for non-blocking http call
public static Result callGoogle() {
play.libs.F.Promise<play.libs.WS.Response> response=WS.url("http://google.com" ).get();
Promise<Result> result= response.map(toResult);
return async(result);
}
NIO parallel call example - non blocking call
public static Promise<Timing> timedRequest( final String url){
final long start = System.currentTimeMillis();
Promise<Response> res= WS.url(url).get();
return res.map(new Function<Response,Timing> (){
public Timing apply (Response response){
long latency=System.currentTimeMillis() -start;
return new Timing(url,latency,start);
}
});
}
NIO parallel call example - non blocking call
public static Result index(){
Promise<List<Timing>> all= Promise.waitAll(
Timing.timedRequest("http://www.yahoo.com"),
Timing.timedRequest("http://www.google.com"),
Timing.timedRequest("http://www.bing.com")
);
return async(all.map(new play.libs.F.Function<List<Timing>,Result>(){
public Result apply(List<Timing> timings){
return ok (play.libs.Json.toJson(timings));
}
}));}
NIO parallel call example - result
[{"url":"http://www.yahoo.com" ,"latency":1542,"start":1408296474891},
{"url":"http://www.google.com" ,"latency":366,"start":1408296474912},
{"url":"http://www.bing.com" ,"latency":437,"start":1408296474913}]
Scala
object HelloWorldScala extends Controller {
def index = Action {
Ok("Hello World Scala")
}
}
Scala Console
play console
import controllers.HelloWorld
HelloWorld.hello
Heroku supports Play
heroku create
heroku git:remote -a playAppForHeroku
git push heroku master
Reference
- https://www.playframework.com/
- Play For Scala (Manning)
- Learning Play! Framework 2 (Andy Petrella)
- Play For Java (Manning)
- People like Sadek Drobi, Yevgeniy Brikman,
James Ward :)
Thank you
Codes are available at;
https://github.com/mkarakaya/playDemo.git

Play Framework

  • 1.
  • 2.
    Agenda - Theory - WhatPlay Solves - Architecture - Play History - Web Evolution
  • 3.
    Agenda - Practice - SimpleMVC operations - NIO - Scala
  • 4.
  • 5.
  • 6.
  • 7.
    Play is notbased on Java EE
  • 8.
  • 9.
    Play - full stackweb framework for JVM - fun and high productive - change code hit reload ( forget jRebel ) - browser error reporting - easy cloud deployment (e.g. Heroku) - stateless
  • 10.
    Play 0.x -05.2007
  • 11.
    Play 1.0 -05.2008
  • 12.
    Play 1.2 -04.2011
  • 13.
    Play 2.0 -03.2012
  • 14.
  • 15.
    The Web Evolved -Static (just html) - Dynamic (get date from server on page :) ) * Structured (mvc, jsp, jsf ) - Realtime (ajax)
  • 16.
    Realtime Web - Polling(req - res) - Long polling (req - res when changed) - Server sent events (req - res -res -res) - Websocket (req - res - res - req -res whatever)
  • 17.
    1 request =1 thread
  • 18.
    NIO 1 request!= 1 thread
  • 19.
    Demo - Create newapplication - Download latest activator https://www.playframework.com/download play new demo play ~run http://localhost:9000
  • 20.
  • 21.
  • 22.
    Controller public class HelloWorldextends Controller{ public static Result index(){ return ok("Hello World"); } }
  • 23.
    conf / routes GET/hello controllers.HelloWorld.index()
  • 24.
    Controller - adda parameter public static Result helloName(String name){ return ok("Hello World: "+ name); }
  • 25.
    conf / routes- add a parameter GET /helloName controllers.HelloWorld.helloName(name) http://localhost:9000/helloName? name=TestUser
  • 26.
    conf / routes- add a parameter to URL GET /helloName/:name controllers.HelloWorld.helloName(name) http://localhost:9000/hello/TestUser
  • 27.
    Controller - parametertype check public class HelloWorld extends Controller{ public static Result helloNameAge(String name, int age){ return ok("Hello World "+name +” you are ” +age); } }
  • 28.
    conf / routes- parameter type check GET /hello/:name/:age controllers.HelloWorld.helloNameAge(name:String,age:Int) http://localhost:9000/hello/TestUser/10
  • 29.
    Add a view @(name:String, age:Int) @main("Welcome to Play") { <html> <head></head> <body> <p> Hello <b> @name</b>, you are <b>@age</b> years old </p> </body> </html> }
  • 30.
    Controller - callview public class HelloWorld extends Controller{ public static Result index(String name, int age){ return ok(views.html.helloWorld.render(name,age)); } }
  • 31.
  • 32.
    WS lib fornon-blocking http call public static Result callGoogle() { play.libs.F.Promise<play.libs.WS.Response> response=WS.url("http://google.com" ).get(); Promise<Result> result= response.map(toResult); return async(result); }
  • 33.
    NIO parallel callexample - non blocking call public static Promise<Timing> timedRequest( final String url){ final long start = System.currentTimeMillis(); Promise<Response> res= WS.url(url).get(); return res.map(new Function<Response,Timing> (){ public Timing apply (Response response){ long latency=System.currentTimeMillis() -start; return new Timing(url,latency,start); } }); }
  • 34.
    NIO parallel callexample - non blocking call public static Result index(){ Promise<List<Timing>> all= Promise.waitAll( Timing.timedRequest("http://www.yahoo.com"), Timing.timedRequest("http://www.google.com"), Timing.timedRequest("http://www.bing.com") ); return async(all.map(new play.libs.F.Function<List<Timing>,Result>(){ public Result apply(List<Timing> timings){ return ok (play.libs.Json.toJson(timings)); } }));}
  • 35.
    NIO parallel callexample - result [{"url":"http://www.yahoo.com" ,"latency":1542,"start":1408296474891}, {"url":"http://www.google.com" ,"latency":366,"start":1408296474912}, {"url":"http://www.bing.com" ,"latency":437,"start":1408296474913}]
  • 36.
    Scala object HelloWorldScala extendsController { def index = Action { Ok("Hello World Scala") } }
  • 37.
    Scala Console play console importcontrollers.HelloWorld HelloWorld.hello
  • 38.
    Heroku supports Play herokucreate heroku git:remote -a playAppForHeroku git push heroku master
  • 39.
    Reference - https://www.playframework.com/ - PlayFor Scala (Manning) - Learning Play! Framework 2 (Andy Petrella) - Play For Java (Manning) - People like Sadek Drobi, Yevgeniy Brikman, James Ward :)
  • 40.
    Thank you Codes areavailable at; https://github.com/mkarakaya/playDemo.git