by January 2, 2013 0 comments



[image_library_tag 953/66953, border=”0″ align=”right” hspace=”4″ vspace=”4″ ,default]

Play is an open source web application framework built around ‘convention over configuration’ software designing paradigm, which means that you will not lose out on scalability or flexibility. Coding is fun as the developer will have to make lesser number of decisions, making the design simpler. It is heavily inspired by Ruby on Rails and Django, and is a pure Java and Scala framework and allows you to keep your preferred development tools and libraries. If you already use Java or Scala as a development platform you don’t need to switch to another language, IDE or libraries. Play leverages the power of Java to build web applications in an environment that is not Java Enterprise Edition-centric.


Why Play?


This is the moot question. And the answer is fairly simple. Play provides the facility to use any of its projects on your intended IDE. You can deploy the ‘war’ file on any Java EE container. It follows the MVC (Model-View-Controller) architecture for designing and processing. Then, how is it different from other regular J2EE frameworks? Let’s find out:


1. Play 2 is fully RESTful: There is no Java EE session per connection. This can make Play 2 more outwardly-scalable than many other frameworks.


2. Less configuration: Simply download, unpack and develop.


3. Faster testing: No need to deploy to an application server, just edit the code and press the refresh button on the browser.


4. Integrated unit testing: JUnit and Selenium support is included in the core.


5. Rarely a developer will need to import any third party library.


6. All controller entry points and business logic methods are declared as static.


7. As it uses JBoss Netty as its web server, Play can service long requests asynchronously rather than tying up HTTP threads doing business logic like Java EE frameworks that don’t use the asynchronous support offered by Servlet 3.0.8. With a modular architecture like Rails and Django, Play comes with the concept of modules.


8. Native Scala support: Play 2 uses Scala internally, but exposes a complete Java API and is completely interoperable with Java.


Road to app development


For the Play framework 2.0, you need to have JDK 6 or later. Just make sure that there are java and javac commands in the current path.


1. Download the binary package and extract the archive to a location where you have both read and write access.


2. In the command prompt, launch the play framework:


$ play help


3. For launching a new application, type the command:


$ play new pcqlab


4. When prompted select option 2 to create a Java application.


5. Now, if you want to make use of IDE, run the appropriate command. For instance, in case of Eclipse, run:


$ play eclipsify


6. Now start the Play app by running:


$ play run


After that open the following URL in your browser to verify whether the app is working or not.

http://localhost:9000/


In the newly created pcqlab directory create a new git repository by running:


git init


Add the files to the git repo and commit them:


git add .git commit -m init


Routing HTTP requests



[image_library_tag 955/66955, border=”0″ align=”middle” hspace=”4″ vspace=”4″ ,default][image_library_tag 955/66955, border=”0″ align=”middle” hspace=”4″ vspace=”4″ ,default]


Play routes HTTP requests to a controller using the routes defined in the conf/routes file. The routes file maps HTTP verbs and URL patterns to controller methods. The route that matched the request you just made has been defined in the “routes” file with:


GET/ controllers.Application.index()

This means that whenever an HTTP GET request for the URL / comes in, it will be routed to the method named index on the controllers.Application class. Add a new route to handle GET requests to /foo with a call to controllers.Application.index() by adding the following line to the conf/routes file:


GET /foo controllers.Application.index()

Commit and verify your changes:


git commit -am “added new route”

Create a new Java class to hold Task objects in a package named models by creating a new app/models/Task.java file:


package models;
import play.db.ebean.Model;

import javax.persistence.Entity;

import javax.persistence.Id;

public class Task extends Model {

public String id;

public String contents;

}


Heroku


Heroku is a multi-language cloud application platform, or platform-as-a-service. It allows developers to deploy, scale, and manage their apps without needing to think about servers or systems administration. It works in a systematic and procedural manner to run and deploy a web application.


How does it work?


There are a series of steps to be followed in order to build and operate the app. First you need to deploy the app. No special tools needed, just a plain ‘git push’. Deployment is instant, whether your app is big or small. Then add on the resources and integrate third party services with one click. Heroku’s large catalog of third party add-on services is constantly growing, and includes services like databases, caching, monitoring, performance management, email, transcoding, search, billing, and many more. After which you need to type suitable commands for activities like deployment & release management, collaboration, routing etc.


Deploy your app on the Cloud with Heroku


Developing an app on Play is such a joy. And deploying it over the cloud just adds on its extensibility. For deploying the app, there is a certain procedure that needs to be adopted. Do the following to run your app on Heroku:


1. First commit and verify your changes of the app with these commands:


git add pcqlab


git commit -m “add pcqlab for Heroku”

Now, here onwards do as follows:


2. Install ‘git’ and the ‘Heroku Toolbelt’.


3. Sign up for Heroku account.


4. Login to Heroku from the command line: Heroku login


5. Create a new app on Heroku (from within the pcqlab directory):


Heroku create


6. Upload the app to Heroku (from within the pcqlab directory)


Git push heroku master


Now, heroku will build your app and you can run it.


7. Open the application, now running on the cloud, in your browser:


Heroku open

Now, the application is developed and successfully deployed over the cloud. We enjoyed it, now its your chance to give it a try. Do let us know about your experience by writing to pcquest@cybermedia.co.in.

No Comments so far

Jump into a conversation

No Comments Yet!

You can be the one to start a conversation.

Your data will be safe!Your e-mail address will not be published. Also other data will not be shared with third person.