Play with-the-framework

27
with the framework K.Gautam

description

A presentation about play framework that was presented at Java user group meeting

Transcript of Play with-the-framework

Page 1: Play with-the-framework

with the frameworkK.Gautam

Page 2: Play with-the-framework

About Play!Launched in 2008Open SourceJava

Page 3: Play with-the-framework

About play!Stateless server side architecture

Bonus points if you guess relation between the picture and this slide

Page 4: Play with-the-framework

No servlet API !

About play !

Naan Appadiye Shock Aaiten

Page 5: Play with-the-framework

About play !Full stack web framework

Page 6: Play with-the-framework

About play !Inspired by

Page 7: Play with-the-framework

PhilosophySimpleEfficientDRYConvention over configurationShare nothing

Page 8: Play with-the-framework

Why no servlets ?

Database

Session

Session

Thread Pool

requests

Page 9: Play with-the-framework

In play !

Database

Worker Threads

Page 10: Play with-the-framework

Play is stateless in the server

Browser

STATEPlay server

Play server

Database

Encrypted cookie

Page 11: Play with-the-framework

Sessions and CacheSession is play! can store only 4KTo store objects use Cache

Page 12: Play with-the-framework

Getting startedModelsControllersViews

Page 13: Play with-the-framework

ModelsSupports JPA

@Entitypublic class Product { public String name; public Integer price;}

Page 14: Play with-the-framework

Controllerspackage controllers;import models.Client;import play.mvc.Controller;public class Clients extends Controller { public static void show(Long id) { Client client = Client.findById(id); render(client); } public static void delete(Long id) { Client client = Client.findById(id); client.delete(); } }

Page 15: Play with-the-framework

ViewsGroovy template engineExpressions: ${…}

<h1>Client ${client.name}</h1>

Scripts: %{…}%

%{ fullName = client.name.toUpperCase()+' '+client.forname;}% <h1>Client ${fullName}</h1>

Page 16: Play with-the-framework

Views contd..Actions: @{…}

<h1>Client ${client.name}</h1><p> <a href="@{Clients.showAccounts(client.id)}">All accounts</a></p><hr /><a href="@{Clients.index()}">Back</a>

Page 17: Play with-the-framework

JobsPlaying with Jobspackage jobs; import play.jobs.*; public class MyJob extends Job { public void doJob() { // execute some

application logic here ... } }

Image courtesy http://static.themetapicture.com/media/funny-Steve-Jobs-cool-photo.jpg

Page 18: Play with-the-framework

Resulting Jobspackage jobs; import play.jobs.*; public class MyJob extends Job<String> { public String doJobWithResult() { // execute some application logic here ... return result; } }

Page 19: Play with-the-framework

Timed Jobsimport play.jobs.*; @Every("1h")public class Bootstrap extends Job { public void doJob() { List<User> newUsers =

User.find("newAccount = true") .fetch();

for(User user : newUsers) { Notifier.sayWelcome(user); } }}

Page 20: Play with-the-framework

Asynchronous HTTPpublic static void generatePDF(Long reportId) { Promise<InputStream> pdf =

new ReportAsPDFJob(report).now();

InputStream pdfStream = await(pdf); renderBinary(pdfStream);}

Page 21: Play with-the-framework

Websocketspublic static void echo() { while(inbound.isOpen()) { WebSocketEvent e = await(inbound.nextEvent()); for(String quit: TextFrame.and(Equals("quit")).match(e)) { outbound.send("Bye!"); disconnect(); } for(String msg: TextFrame.match(e)) { outbound.send("Echo: %s", frame.textData); } for(WebSocketClose closed: SocketClosed.match(e)) { Logger.info("Socket closed!"); } }}

Page 22: Play with-the-framework

Dependency Managementdependencies.yml# Application dependenciesrequire: - play 1.2 - com.google.guava -> guava r07

play dependencies --sync

Page 23: Play with-the-framework

Deployment optionsplay start mysuperwebapp

Page 24: Play with-the-framework

JEE Application servers

play war myapp -o myapp.war

Page 25: Play with-the-framework

Cloud based hosting● AWS elastic Beanstalk● Google appEngine● Heroku● Cloud Foundry● Openshift DIY

● May be even Windows Azure

Page 26: Play with-the-framework

Copyright NoticeI do not own any of the images.All images are copyright of the respective owners.

I just found them on the internet

If you are the owner of any of the images thank you for your permission.

Page 27: Play with-the-framework

Thank you !