play framework
DESCRIPTION
This presentation is an introduction to Play Framework 2TRANSCRIPT
![Page 1: Play Framework](https://reader034.vdocuments.us/reader034/viewer/2022051816/54566f60b1af9fc15d8b45a8/html5/thumbnails/1.jpg)
Play FrameworkMahmut Karakaya
![Page 2: Play Framework](https://reader034.vdocuments.us/reader034/viewer/2022051816/54566f60b1af9fc15d8b45a8/html5/thumbnails/2.jpg)
Agenda - Theory
- What Play Solves- Architecture- Play History- Web Evolution
![Page 3: Play Framework](https://reader034.vdocuments.us/reader034/viewer/2022051816/54566f60b1af9fc15d8b45a8/html5/thumbnails/3.jpg)
Agenda - Practice
- Simple MVC operations- NIO - Scala
![Page 4: Play Framework](https://reader034.vdocuments.us/reader034/viewer/2022051816/54566f60b1af9fc15d8b45a8/html5/thumbnails/4.jpg)
Waiting...
![Page 5: Play Framework](https://reader034.vdocuments.us/reader034/viewer/2022051816/54566f60b1af9fc15d8b45a8/html5/thumbnails/5.jpg)
Threads
![Page 6: Play Framework](https://reader034.vdocuments.us/reader034/viewer/2022051816/54566f60b1af9fc15d8b45a8/html5/thumbnails/6.jpg)
Factory classes
![Page 7: Play Framework](https://reader034.vdocuments.us/reader034/viewer/2022051816/54566f60b1af9fc15d8b45a8/html5/thumbnails/7.jpg)
Play is not based on Java EE
![Page 8: Play Framework](https://reader034.vdocuments.us/reader034/viewer/2022051816/54566f60b1af9fc15d8b45a8/html5/thumbnails/8.jpg)
MVC
![Page 9: Play Framework](https://reader034.vdocuments.us/reader034/viewer/2022051816/54566f60b1af9fc15d8b45a8/html5/thumbnails/9.jpg)
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
![Page 10: Play Framework](https://reader034.vdocuments.us/reader034/viewer/2022051816/54566f60b1af9fc15d8b45a8/html5/thumbnails/10.jpg)
Play 0.x - 05.2007
![Page 11: Play Framework](https://reader034.vdocuments.us/reader034/viewer/2022051816/54566f60b1af9fc15d8b45a8/html5/thumbnails/11.jpg)
Play 1.0 - 05.2008
![Page 12: Play Framework](https://reader034.vdocuments.us/reader034/viewer/2022051816/54566f60b1af9fc15d8b45a8/html5/thumbnails/12.jpg)
Play 1.2 - 04.2011
![Page 13: Play Framework](https://reader034.vdocuments.us/reader034/viewer/2022051816/54566f60b1af9fc15d8b45a8/html5/thumbnails/13.jpg)
Play 2.0 - 03.2012
![Page 14: Play Framework](https://reader034.vdocuments.us/reader034/viewer/2022051816/54566f60b1af9fc15d8b45a8/html5/thumbnails/14.jpg)
Notable websites using Play
![Page 15: Play Framework](https://reader034.vdocuments.us/reader034/viewer/2022051816/54566f60b1af9fc15d8b45a8/html5/thumbnails/15.jpg)
The Web Evolved
- Static (just html) - Dynamic (get date from server on page :) ) * Structured (mvc, jsp, jsf ) - Realtime (ajax)
![Page 16: Play Framework](https://reader034.vdocuments.us/reader034/viewer/2022051816/54566f60b1af9fc15d8b45a8/html5/thumbnails/16.jpg)
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)
![Page 17: Play Framework](https://reader034.vdocuments.us/reader034/viewer/2022051816/54566f60b1af9fc15d8b45a8/html5/thumbnails/17.jpg)
1 request = 1 thread
![Page 18: Play Framework](https://reader034.vdocuments.us/reader034/viewer/2022051816/54566f60b1af9fc15d8b45a8/html5/thumbnails/18.jpg)
NIO 1 request != 1 thread
![Page 19: Play Framework](https://reader034.vdocuments.us/reader034/viewer/2022051816/54566f60b1af9fc15d8b45a8/html5/thumbnails/19.jpg)
Demo
- Create new application- Download latest activator
https://www.playframework.com/downloadplay new demo
play ~run
http://localhost:9000
![Page 20: Play Framework](https://reader034.vdocuments.us/reader034/viewer/2022051816/54566f60b1af9fc15d8b45a8/html5/thumbnails/20.jpg)
IDE supportplay eclipse
play idea
![Page 21: Play Framework](https://reader034.vdocuments.us/reader034/viewer/2022051816/54566f60b1af9fc15d8b45a8/html5/thumbnails/21.jpg)
Application Layout
![Page 22: Play Framework](https://reader034.vdocuments.us/reader034/viewer/2022051816/54566f60b1af9fc15d8b45a8/html5/thumbnails/22.jpg)
Controller public class HelloWorld extends Controller{
public static Result index(){
return ok("Hello World");
}
}
![Page 23: Play Framework](https://reader034.vdocuments.us/reader034/viewer/2022051816/54566f60b1af9fc15d8b45a8/html5/thumbnails/23.jpg)
conf / routesGET /hello
controllers.HelloWorld.index()
![Page 24: Play Framework](https://reader034.vdocuments.us/reader034/viewer/2022051816/54566f60b1af9fc15d8b45a8/html5/thumbnails/24.jpg)
Controller - add a parameterpublic static Result helloName(String name){
return ok("Hello World: "+ name);
}
![Page 25: Play Framework](https://reader034.vdocuments.us/reader034/viewer/2022051816/54566f60b1af9fc15d8b45a8/html5/thumbnails/25.jpg)
conf / routes - add a parameterGET /helloName
controllers.HelloWorld.helloName(name)
http://localhost:9000/helloName?
name=TestUser
![Page 26: Play Framework](https://reader034.vdocuments.us/reader034/viewer/2022051816/54566f60b1af9fc15d8b45a8/html5/thumbnails/26.jpg)
conf / routes - add a parameter to URL
GET /helloName/:name
controllers.HelloWorld.helloName(name)
http://localhost:9000/hello/TestUser
![Page 27: Play Framework](https://reader034.vdocuments.us/reader034/viewer/2022051816/54566f60b1af9fc15d8b45a8/html5/thumbnails/27.jpg)
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);
}
}
![Page 28: Play Framework](https://reader034.vdocuments.us/reader034/viewer/2022051816/54566f60b1af9fc15d8b45a8/html5/thumbnails/28.jpg)
conf / routes - parameter type checkGET /hello/:name/:age
controllers.HelloWorld.helloNameAge(name:String,age:Int)
http://localhost:9000/hello/TestUser/10
![Page 29: Play Framework](https://reader034.vdocuments.us/reader034/viewer/2022051816/54566f60b1af9fc15d8b45a8/html5/thumbnails/29.jpg)
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>
}
![Page 30: Play Framework](https://reader034.vdocuments.us/reader034/viewer/2022051816/54566f60b1af9fc15d8b45a8/html5/thumbnails/30.jpg)
Controller - call view public class HelloWorld extends Controller{
public static Result index(String name, int age){
return ok(views.html.helloWorld.render(name,age));
}
}
![Page 31: Play Framework](https://reader034.vdocuments.us/reader034/viewer/2022051816/54566f60b1af9fc15d8b45a8/html5/thumbnails/31.jpg)
Error handling
![Page 32: Play Framework](https://reader034.vdocuments.us/reader034/viewer/2022051816/54566f60b1af9fc15d8b45a8/html5/thumbnails/32.jpg)
WS lib for non-blocking http callpublic 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);
}
![Page 33: Play Framework](https://reader034.vdocuments.us/reader034/viewer/2022051816/54566f60b1af9fc15d8b45a8/html5/thumbnails/33.jpg)
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);
}
});
}
![Page 34: Play Framework](https://reader034.vdocuments.us/reader034/viewer/2022051816/54566f60b1af9fc15d8b45a8/html5/thumbnails/34.jpg)
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));
}
}));}
![Page 35: Play Framework](https://reader034.vdocuments.us/reader034/viewer/2022051816/54566f60b1af9fc15d8b45a8/html5/thumbnails/35.jpg)
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}]
![Page 36: Play Framework](https://reader034.vdocuments.us/reader034/viewer/2022051816/54566f60b1af9fc15d8b45a8/html5/thumbnails/36.jpg)
Scalaobject HelloWorldScala extends Controller {
def index = Action {
Ok("Hello World Scala")
}
}
![Page 37: Play Framework](https://reader034.vdocuments.us/reader034/viewer/2022051816/54566f60b1af9fc15d8b45a8/html5/thumbnails/37.jpg)
Scala Consoleplay console
import controllers.HelloWorld
HelloWorld.hello
![Page 38: Play Framework](https://reader034.vdocuments.us/reader034/viewer/2022051816/54566f60b1af9fc15d8b45a8/html5/thumbnails/38.jpg)
Heroku supports Playheroku create
heroku git:remote -a playAppForHeroku
git push heroku master
![Page 39: Play Framework](https://reader034.vdocuments.us/reader034/viewer/2022051816/54566f60b1af9fc15d8b45a8/html5/thumbnails/39.jpg)
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 :)
![Page 40: Play Framework](https://reader034.vdocuments.us/reader034/viewer/2022051816/54566f60b1af9fc15d8b45a8/html5/thumbnails/40.jpg)
Thank youCodes are available at;
https://github.com/mkarakaya/playDemo.git