Introduction to Play 2

19
INTRODUCTION TO PLAY FRAMEWORK 2.0 Luciano Fiandesio Founder and Architect @ Aestas IT http://aestasit.com @koevet Friday, May 4, 12

description

Introduction to Play 2 framework made on JUG.fi on 3rd of May 2012

Transcript of Introduction to Play 2

Page 1: Introduction to Play 2

INTRODUCTION TO PLAY FRAMEWORK 2.0

Luciano Fiandesio Founder and Architect @ Aestas IT

http://aestasit.com@koevet

Friday, May 4, 12

Page 2: Introduction to Play 2

http://aestasit.com

•The Play! framework project by Zenexity started in 2007, release 1.0 was out in 2008.

• Play! started to allow Scala development since version 1.1.

•Version 2.0, released on March 2012, was completely rewritten in Scala. Uses the Akka framework as backend.

•Version 2 is part of the Typesafe framework, which includes Scala and Akka.

HISTORY

Friday, May 4, 12

Page 3: Introduction to Play 2

http://aestasit.com

• Play! is a stateless full web development stack that works with Scala and Java

• Built and focused around HTTP - no need to hide it!

•Allows an asynchronous HTTP programming model (comet, websocket, streams)

• Scalable and high performance (Akka!)

•Type safe and everything gets compiled (code, js, css, routes)

WHAT IS PLAY ?

Friday, May 4, 12

Page 4: Introduction to Play 2

http://aestasit.com

• Statelessness promotes horizontal scalability

•No session, only cache. Memcache for scalability

•No need to replicate the state, just use a load balancer

•The web is stateless, so Play! just matches the web architecture

PLAY IS STATELESS

Friday, May 4, 12

Page 5: Introduction to Play 2

http://aestasit.com

• Probably the biggest “selling point” of the Play! framework.

•No need to redeploy, just click “refresh” on your browser (Rails anyone?)

•What can be reloaded?

• Java/Scala classes

• configuration files

• templates

DEVELOPING WITH PLAY

Friday, May 4, 12

Page 6: Introduction to Play 2

http://aestasit.com

• Play! handles URLs in a REST-like manner: resources!

• http://localhost:9000/user/100

• http//localhost:9000/users

•The app’s url can be easily bookmarked, mailed and tweeted

•All app’s url are configured in one file: conf/routes

URLS MATTERS!

Friday, May 4, 12

Page 7: Introduction to Play 2

http://aestasit.com

SHOW ME THE ERROR

Friday, May 4, 12

Page 8: Introduction to Play 2

http://aestasit.com

SHOW ME ALL ERRORS!

•The compilers also shows compilation errors for

•Templates

• Javascript/Coffee script

•CSS (when using the Less framework)

Friday, May 4, 12

Page 9: Introduction to Play 2

http://aestasit.com

ACTIONS AND TEMPLATES

Friday, May 4, 12

Page 10: Introduction to Play 2

http://aestasit.com

ACTIONS AND TEMPLATES

• Play! templates get compiled to Scala classes, therefore they share a lot with Scala.

•Templates are type safe. They are like giant functions with parameters.

Calling a template!

Friday, May 4, 12

Page 11: Introduction to Play 2

http://aestasit.com

ACTIONS AND TEMPLATES

products.scala.html

Friday, May 4, 12

Page 12: Introduction to Play 2

http://aestasit.com

ACTIONS AND TEMPLATES

Friday, May 4, 12

Page 13: Introduction to Play 2

http://aestasit.com

FLASH & SESSION

• Play! has a Session, but as it’s based on Cookie and can only contain String (up to 4K)

•There is also a request-scope session, called flash good for displaying messages upon a successful request.

Friday, May 4, 12

Page 14: Introduction to Play 2

http://aestasit.com

BEAN FOREVER

• Play! supports multiple datasources and it is possible to use a Connection object (if you are old school).

•ORM is supported too, with Ebean (http://www.avaje.org/) and JPA.

• Ebean has great support through the Model superclass. Define your entity and you are good to go.

Friday, May 4, 12

Page 15: Introduction to Play 2

http://aestasit.com

BEAN FOREVER

A model class

Friday, May 4, 12

Page 16: Introduction to Play 2

http://aestasit.com

BEAN FOREVER

Friday, May 4, 12

Page 17: Introduction to Play 2

http://aestasit.com

CACHE

• Play! has a default cache mechanism based on EHCache - but is pluggable.

•Cache can be accessed via API and even from the templates.

• Basic Annotation based caching supported:

Friday, May 4, 12

Page 18: Introduction to Play 2

http://aestasit.com

SWEDISH DELIGHT

• Play! and Akka are totally integrated.

•You can access an Actor system and run code asynchronously

•Akka can also be used when dealing with asynchronous request/response (like Websocket).

• Bottom line: it is very easy to create asynchronous applications with Play 2

Friday, May 4, 12

Page 19: Introduction to Play 2

http://aestasit.com

GET THINGS STRAIGHT

•Deploy of a Play! 2 app on Tomcat or any other container doesn’t work (so far).

•Many people on the newsgroup are complaining about the Scala syntax for templates

• Scala compilation time is 3X slower than Java (some say 5X)

•ORM has less features than Play 1

•No plugin eco-system (yet - is coming)

Friday, May 4, 12