Introduction in the play framework

37
Web development with the Play! framework Java web development is fun again Alexander Reelsen [email protected] June 2010 Alexander Reelsen Web development with the Play! framework June 2010 1 / 35

description

Introduction into the play framework - held at Java User Group Munich at 21st June 2010.

Transcript of Introduction in the play framework

Page 1: Introduction in the play framework

Web development with the Play! frameworkJava web development is fun again

Alexander Reelsen

[email protected]

June 2010

Alexander Reelsen Web development with the Play! framework June 2010 1 / 35

Page 2: Introduction in the play framework

Agenda1 Introduction

Overview2 Core concepts

ArchitectureConventionsConfiguring routesModelControllerViewTestingJobsRunning Play in production

3 Ongoing developmentFeatures for Play 1.1Module repositoryDevelopment forecast

Alexander Reelsen Web development with the Play! framework June 2010 2 / 35

Page 3: Introduction in the play framework

Introduction

Agenda1 Introduction

Overview2 Core concepts

ArchitectureConventionsConfiguring routesModelControllerViewTestingJobsRunning Play in production

3 Ongoing developmentFeatures for Play 1.1Module repositoryDevelopment forecast

Alexander Reelsen Web development with the Play! framework June 2010 3 / 35

Page 4: Introduction in the play framework

Introduction Overview

Speaking...

About me

Studied information systems

10 years linux system engineering, converted to software engineering

Just another java developer

Web framework enthusiast

Fed up with complex java environment for simple webapps

Non development life

Basketball/Streetball

Web 2.0

Alexander Reelsen Web development with the Play! framework June 2010 4 / 35

Page 5: Introduction in the play framework

Introduction Overview

Speaking...

About me

Studied information systems

10 years linux system engineering, converted to software engineering

Just another java developer

Web framework enthusiast

Fed up with complex java environment for simple webapps

Non development life

Basketball/Streetball

Web 2.0

Alexander Reelsen Web development with the Play! framework June 2010 4 / 35

Page 6: Introduction in the play framework

Introduction Overview

The web taking over

Ubiquitous web

Web as ubiquitous platform

Providing data, not web pages

Browser as the operating system

JavaScript gains steam, toolkits as well as engines

Web applications are alive

Alexander Reelsen Web development with the Play! framework June 2010 5 / 35

Page 7: Introduction in the play framework

Core concepts

Agenda1 Introduction

Overview2 Core concepts

ArchitectureConventionsConfiguring routesModelControllerViewTestingJobsRunning Play in production

3 Ongoing developmentFeatures for Play 1.1Module repositoryDevelopment forecast

Alexander Reelsen Web development with the Play! framework June 2010 6 / 35

Page 8: Introduction in the play framework

Core concepts Architecture

Play in 10 seconds

Quick overview

No compile, deploy, restart cycle - Fix the bug and hit reload!

Share nothing system - no state in the server

Clean templating system - using groovy

Exact errors (including line numbers, even for templates)

Lots of builtin features for fast development

Pure Java

Starts fast, runs fast

Extensible by modules

Alexander Reelsen Web development with the Play! framework June 2010 7 / 35

Page 9: Introduction in the play framework

Core concepts Architecture

Roots and fundamentals

History

Exists since 2008, by Guillaume Bort from zenexity

Release 1.0 was in October 2009

Current: 1.0.3 + development tree

A framework imposes its own style of architecture

REST as architectural paradigm for ressources

URLs are the entry point (and implicit interface) to your application

Do not work against HTTP (stateless protocol)

Convention over configuration

Only fractions of differences between development and productionmode

Alexander Reelsen Web development with the Play! framework June 2010 8 / 35

Page 10: Introduction in the play framework

Core concepts Architecture

Play architecture

Source: http://www.playframework.org/documentation/1.0.1/mainAlexander Reelsen Web development with the Play! framework June 2010 9 / 35

Page 11: Introduction in the play framework

Core concepts Architecture

Play is a lot of glue code

Includes the following libraries

Hibernate

Oval

Lucene

Google gson

Eclipse compiler

Commons: fileupload, httpclient, email, logging, beanutils

MINA and Asyncweb

EhCache

JAMon

Groovy

Alexander Reelsen Web development with the Play! framework June 2010 10 / 35

Page 12: Introduction in the play framework

Core concepts Architecture

Play specialties

Interesting to know

No support for servlet API (yes, in a web framework)

Sharing objects via memcached through several nodes

Everything is UTF-8

Full text indexing with 2 annotations

No anemic domain model - logic is in the object

DAOs and finders are not external

Textmate, eclipse bundles, also support for IDEA and netbeans

Alexander Reelsen Web development with the Play! framework June 2010 11 / 35

Page 13: Introduction in the play framework

Core concepts Conventions

Application layout

Creating a new app

play new myapp

Application structure

. / c o n f

. / c o n f / r o u t e s

. / c o n f / a p p l i c a t i o n . c o n f

. / c o n f / messages

. / t e s t

. / l i b

. / p u b l i c

. / app

. / app / models

. / app / c o n t r o l l e r s

. / app / v i e w s

Alexander Reelsen Web development with the Play! framework June 2010 12 / 35

Page 14: Introduction in the play framework

Core concepts Conventions

Application layout

Creating a new app

play new myapp

Application structure

. / c o n f

. / c o n f / r o u t e s

. / c o n f / a p p l i c a t i o n . c o n f

. / c o n f / messages

. / t e s t

. / l i b

. / p u b l i c

. / app

. / app / models

. / app / c o n t r o l l e r s

. / app / v i e w s

Alexander Reelsen Web development with the Play! framework June 2010 12 / 35

Page 15: Introduction in the play framework

Core concepts Conventions

Application config file

conf/application.conf

Configure database access

db=fs, db=memdb=mysql:user:pwd@database_name

Any JDBC connection

Specify modules

Supported languages

Logger

memcached setup

mail configuration

mode/system specific settings

Alexander Reelsen Web development with the Play! framework June 2010 13 / 35

Page 16: Introduction in the play framework

Core concepts Configuring routes

The conf/routes file

Interface contract to the outer world

GET / A p p l i c a t i o n . i n d e xGET / u s e r /{ username} A p p l i c a t i o n . showUserPOST / u s e r A p p l i c a t i o n . c r e a t e U s e rDELETE / u s e r /{ username} A p p l i c a t i o n . d e l e t e U s e r

GET / p u b l i c s t a t i c D i r : p u b l i c

Alexander Reelsen Web development with the Play! framework June 2010 14 / 35

Page 17: Introduction in the play framework

Core concepts Model

Designing a domain model

A simple user

@Entity

public class User extends Model {

@Required

@Column(unique = true)

public String username;

@Required

@Email

public String email;

@Required

public String password;

public void setPassword(String password) {

this.pass = Crypto.passwordHash(pass);

}

}

Alexander Reelsen Web development with the Play! framework June 2010 15 / 35

Page 18: Introduction in the play framework

Core concepts Model

The Model class is a helper

Finders and Entity actions

// Query by property

User user = User.find("byUsername", username).first();

// Calls a setter

user.password = "foobar";

user.save();

List<User> users = User.findAll();

users.get(0).delete();

// JPA queries are possible as well, so are joins

List<String> names = User.find("select u.username from User u

order by u.username desc").fetch();

Alexander Reelsen Web development with the Play! framework June 2010 16 / 35

Page 19: Introduction in the play framework

Core concepts Controller

Calling business logic

User controller

public class Application extends Controller {

public static void index() {

List<User> users = User.findAll();

render(users);

}

public static void showUser(String username) {

User user = User.find("byUsername", username).first();

notFoundIfNull(user);

render(user);

}

...

Alexander Reelsen Web development with the Play! framework June 2010 17 / 35

Page 20: Introduction in the play framework

Core concepts Controller

Calling business logic

User controller (ctd.)

...

public static void deleteUser(String username) {

User user = User.find("byUsername", username).first();

notFoundIfNull(user);

user.delete();

Application.index();

}

public static void createUser(@Valid User user) {

if (validation.hasErrors()) {

flash.error("Invalid user data");

Application.index();

}

user = user.save();

Application.showUser(user.username)

}

}

Alexander Reelsen Web development with the Play! framework June 2010 18 / 35

Page 21: Introduction in the play framework

Core concepts Controller

Calling business logic

Example: Accessing the session

public class AuthController extends Controller {

@Before(unless = "login")

public static void checkSession() {

if (!request.session.contains("username")) {

forbidden("You are not authorized");

}

}

public void login(String username, String password) {

String pass = Crypto.passwordHash(password);

User user = User.find("byUsernameAndPassword",

username, pass).first();

notFoundIfNull(user);

request.session.put("username", user);

Application.index();

}

}

Alexander Reelsen Web development with the Play! framework June 2010 19 / 35

Page 22: Introduction in the play framework

Core concepts View

The templating system

List users (app/views/Application/index.html)

#{ extends ’main.html ’ /}

#{set title:’Index ’ /}

<ul>

#{list items:users , as:’user ’}

<li>

#{a @Application.showUser(user.username )}

${user.username}

#{/a}

with email address ${user.email}

</li>

#{/ list}

</ul>

Alexander Reelsen Web development with the Play! framework June 2010 20 / 35

Page 23: Introduction in the play framework

Core concepts View

The templating system

Add user

#{form @Application.createUser ()}

<div>Username:

<input type="text" name="user.username" />

</div>

<div>Password:

<input type="pass" name="user.password" />

</div>

<div>Email:

<input type="text" name="user.email" />

</div>

<input type="submit" value="Add user" />

#{/ form}

Alexander Reelsen Web development with the Play! framework June 2010 21 / 35

Page 24: Introduction in the play framework

Core concepts View

The templating system

More tags

doLayout, extends, include

if, ifnot, else, elseif

&{’i18nVariable’} out of conf/messages.de

Always access to: session, flash, request, params, lang,

messages, out, play

Alexander Reelsen Web development with the Play! framework June 2010 22 / 35

Page 25: Introduction in the play framework

Core concepts View

The templating system

Extending objects using mixins

public class SqrtExtension extends JavaExtensions {

public static Double sqrt(Number number) {

return Math.sqrt(number.doubleValue());

}

}

The template code

<div>

Square root of x value is: ${x.sqrt ()}

</div>

Alexander Reelsen Web development with the Play! framework June 2010 23 / 35

Page 26: Introduction in the play framework

Core concepts Testing

Testing

Providing test data

YAML formatted file provides testdata

User ( a l e r e e ) :username : a l rpassword : t e s te m a i l : a l e x a n d e r @ r e e l s e n . n et

Loading test data...

@Before

public void setUp() {

Fixtures.deleteAll();

Fixtures.load("data.yml");

}

Alexander Reelsen Web development with the Play! framework June 2010 24 / 35

Page 27: Introduction in the play framework

Core concepts Testing

Testing

Unit tests

Standard junit tests

Extend from UnitTest, which needs a JPA environment

Functional tests

Integration tests

Checks the external responses (http response)

Selenium tests

GUI tests

Very nice controllable, playback recorder

Possibility of doing step-by-step slow debugging

Alexander Reelsen Web development with the Play! framework June 2010 25 / 35

Page 28: Introduction in the play framework

Core concepts Testing

Testing

CI with Calimoucho

Poor mans hudson

Checks out the project and runs play auto-test, which needs agraphical layer for selenium tests

Check it under http://integration.playframework.org

Code coverage with cobertura

Enable the cobertura module in application.conf

Run the tests, check the results

Alexander Reelsen Web development with the Play! framework June 2010 26 / 35

Page 29: Introduction in the play framework

Core concepts Jobs

Jobs - being asynchronous

Doing the right thing at the right time

Scheduled jobs (housekeeping)

Bootstrap jobs (initial data providing)

Suspendable requests (rendering a PDF report without blocking theconnection thread pool)

/* @Every("1h") */

@OnApplicationStart

public class LoadDataJob extends Job {

public void doJob() {

/* .. do whatever you want */

}

}

Alexander Reelsen Web development with the Play! framework June 2010 27 / 35

Page 30: Introduction in the play framework

Core concepts Running Play in production

Putting play into production

The setup

A redirector like nginx or apache is preferred

Also eliminates the need to serve static files

Redirect to different nodes would be the main task

Profile per nodes possible (very useful for server farms)

Alexander Reelsen Web development with the Play! framework June 2010 28 / 35

Page 31: Introduction in the play framework

Core concepts Running Play in production

Monitoring play application

Partial Output of play status

Monitors:

Application.showLatestRecipesRss(), ms. ->

4120 hits; 41.0 avg; 20.0 min; 260.0 max;

/app/views/Application/showLatestRecipesRss.html, ms. ->

4120 hits; 34.9 avg; 19.0 min; 235.0 max;

Datasource:

Job execution pool:

Scheduled jobs:

Alexander Reelsen Web development with the Play! framework June 2010 29 / 35

Page 32: Introduction in the play framework

Ongoing development

Agenda1 Introduction

Overview2 Core concepts

ArchitectureConventionsConfiguring routesModelControllerViewTestingJobsRunning Play in production

3 Ongoing developmentFeatures for Play 1.1Module repositoryDevelopment forecast

Alexander Reelsen Web development with the Play! framework June 2010 30 / 35

Page 33: Introduction in the play framework

Ongoing development Features for Play 1.1

Play 1.1 is on its way

Core features

Bug fixes

Scala support

More modules through module repository

Alexander Reelsen Web development with the Play! framework June 2010 31 / 35

Page 34: Introduction in the play framework

Ongoing development Module repository

Useful modules

Slowly but steadily growing

Scala, Scalate, Akka

PDF, excel modules

Guice and Spring modules

Netty and grizzly support

GWT support, GAE support

Extended CSS, SASS

Ivy and Maven support

Siena, Ebean ORM, MongoDB

Database migration module

Hosting: stax, playapps

Alexander Reelsen Web development with the Play! framework June 2010 32 / 35

Page 35: Introduction in the play framework

Ongoing development Development forecast

TODO

Open issues

NoSQL support (Siena, MongoDB)

Amazon Cloud Integration

Hosting platform (playapps.net has just launched)

Lucene Solr Support for shared environments

Tighter integration with JavaScript Toolkits like Dojo

Far more modules - check out the rich grails ecosystem

The first book... is coming!

Alexander Reelsen Web development with the Play! framework June 2010 33 / 35

Page 36: Introduction in the play framework

Finish

Done!

Thanks for listening.Questions?

Job offers regarding Java Scalability or new technologies like Play and/orMongoDB?

Talk to me or send mail to [email protected]

Alexander Reelsen Web development with the Play! framework June 2010 34 / 35

Page 37: Introduction in the play framework

Finish

URLs

Further information

This presentation: http://www.slideshare.net/areelsen/

http://www.playframework.org

http://twitter.com/playframework

http://it-republik.de/jaxenter/artikel/

Webanwendungen-mit-dem-Java-Framework-Play!-2813.html

http://www.bookware.de/kaffeeklatsch/archiv/

KaffeeKlatsch-2009-11.pdf

http://www.bookware.de/kaffeeklatsch/archiv/

KaffeeKlatsch-2009-12.pdf

http://www.zenexity.fr/frameworks/

anatomie-dun-framework-de-developpement-web/

Alexander Reelsen Web development with the Play! framework June 2010 35 / 35