It has been a while I got hold of new book on Play framework from Packt publications - Play Framework Cookbook. I just completed reading this book.
Play online documentation is itself awesome - explaining the basics and leaving the readers to explore complex features on these basics. This book complements the online documentation and digs deep on some of the great features of the framework - providing practical techniques with source code.
The book is written by Alexander Reelsen.
Pre-requisites:
This book is intended for experienced Play users - not the beginners. By experienced I mean, users who understand the basics, high level architecture of Play and who have worked on few examples. There is very less introductory stuff.
Review:
The book covers the Play 1.2 version, whereas the current online version available online is 1.2.3. One of the technical review member is none other - Guillaume Bort - the creator of the Play framework. There is no question of technicalities and correctness of the code examples.
There are 7 chapters with an Appendix covering about 60 recipes.
The most useful chapters that I found where:
1. Leveraging Modules - This chapter covers dependency injection with Spring and Guice. There is a recipe of using MongoDB module which I found useful. The recipes are useful and gives a good insight into how modules can provide powerful extensions to Play’s core functionality.
2. Introduction to Writing Modules - This is another useful chapter - which is not easily found on the web. When the project size increases, it is best to split into modules and manage the complexity. This covers Play plugins, understanding events, managing module dependencies.
3. Running in Production - This is an very important chapter which will be most useful for any project team. Almost all the recipes presented in this chapter like - Test automation, SSL for certain controllers, running jobs in distributed environment are very useful and clearly explained.
Criticisms
1. The code examples span multiple pages and not very well formatted. This is nothing to do with the content but with more of formatting and presenting. Just look at any O'reilly book, their editors do an awesome job
2. Few recipes lack good explanation - it could have been improved - For e.g. - "Understanding events" in the "Introduction to Writing Modules"
3. The reader can make out that English is not the first language of the Author. This is some room for improvement in this area.
Conclusion: It is definitely a very useful book for teams who have already started using the framework. As I said earlier - it complements the online documentation. It is a good buy - worth the investment.
Disclosure: I was provided with an eBook copy of the Play Framework Cookbook by Packt Publishing for the purposes of this review.
Tryst with software development, web and Java.. “a man whom life intoxicates, who has no need of wine” (Anaïs Nin).
Showing posts with label play-framework. Show all posts
Showing posts with label play-framework. Show all posts
September 30, 2011
September 21, 2011
Play Framework - Excel report example
It's breeze to create excel reports using Play framework. Let me run thru an example.
Let's create a sample project - excel-example.
However there is one additional library that would be required to ouptut xslx excel formats - which is the newer format depending on Office Open XML documents. This is because xslx is basically a zip format.
Download the ooxml-schemas-1.1.jar from the maven repository and put it in the lib directory of the excel-example project.
Then eclipsify again so that eclispe classpath reflects the new dependencies.
Start the play server.
I have created a simple xslx file which shows displays the list of customers and their phone numbers. You can get the file here. Below is an image for easy reference. Using jxls is a great advantage. Your excel template can contain all required formatting, formulas, macros etc which will be preserved intact. You need to use specific notation (common these days in groovy, ruby language) to indicate placement of data. Refer the below image.
Save this file under under
The home page contains just one link to download the excel report as shown below
Edit the controller Application.java and add a method - customerphonelist() which outputs the excel file as shown below:
Line No 22 to 25 creates a Customer model class and populates it with some random data.
Line No 26 to 30 puts the objects in to the renderArgs map. These objects would be available as part of the actual excel file which can be used to render the dynamic content.
Line No 29 is of special interest which sets the ASYNC to true. This is a very powerful feature of Play framework. This helps in transparent asynchronous rendering of the excel. You don't have to deal with Future, Promise, await, request.new etc and you get the power for nearly free. The existing HTTP request is suspended and then when the excel report generation is done, this thread is resumed and the response sent back to the client. Think about the fact that a large Excel table rendering might take as long as 500ms and blocking request handling thread for half second is NOT acceptable in a high performance web server like play which use only limited thread (N+1) to handle web requests. Read more about this here.
Last step :), Open
That completes the example.
Let's create a sample project - excel-example.
$ play new excel-exampleThen eclipsify and import into eclipse.
$ play eclipsify excel-exampleEdit the dependencies.yml and include the excel module
# Application dependencies
require:
- play 1.2.3
- play -> excel 1.2.3
Run the play dependencies command. Play connects to the web and downloads the excel-module into the modules directory$ play deps excel-example ~ _ _ ~ _ __ | | __ _ _ _| | ~ | '_ \| |/ _' | || |_| ~ | __/|_|\____|\__ (_) ~ |_| |__/ ~ ~ play! 1.2.3, http://www.playframework.org ~ ~ Resolving dependencies using /Users/play/excel-example/conf/dependencies.yml, ~ ~ play->excel 1.2.3 (from playLocalModules) ~ ~ Installing resolved dependencies, ~ ~ modules/excel-1.2.3 -> /Users/play/frameworks/play-1.2.3/modules/excel-1.2.3 ~ ~ Done! ~Plays excel module uses jxls internally which uses Apache POI library - the one and only one library in Java world for excel manipulation. All the dependencies of jxls are bundled with excel-module and dependencies managed.
However there is one additional library that would be required to ouptut xslx excel formats - which is the newer format depending on Office Open XML documents. This is because xslx is basically a zip format.
Download the ooxml-schemas-1.1.jar from the maven repository and put it in the lib directory of the excel-example project.
Then eclipsify again so that eclispe classpath reflects the new dependencies.
$ play eclipsify excel-exampleRefresh the Project in eclipse.
Start the play server.
$ play run excel-example ~ _ _ ~ _ __ | | __ _ _ _| | ~ | '_ \| |/ _' | || |_| ~ | __/|_|\____|\__ (_) ~ |_| |__/ ~ ~ play! 1.2.3, http://www.playframework.org ~ ~ Ctrl+C to stop ~ JPDA port 8000 is already used. Will try to use any free port for debugging Listening for transport dt_socket at address: 50675 00:54:21,975 INFO ~ Starting /Users/play/excel-example 00:54:21,979 INFO ~ Module excel is available (/Users/play/frameworks/play-1.2.3/modules/excel-1.2.3) 00:54:22,603 WARN ~ You're running Play! in DEV mode 00:54:22,769 INFO ~ Listening for HTTP on port 9000 (Waiting a first request to start) ...check at localhost:9000, you should see a welcome page (I'm just using command line to do a quick check).
$ curl http://localhost:9000The next step is to Create Sample Excel template file
I have created a simple xslx file which shows displays the list of customers and their phone numbers. You can get the file here. Below is an image for easy reference. Using jxls is a great advantage. Your excel template can contain all required formatting, formulas, macros etc which will be preserved intact. You need to use specific notation (common these days in groovy, ruby language) to indicate placement of data. Refer the below image.
excel-example/app/views/Application/customerphonelist.xlsxLets edit the default index.html which is under excel-example/app/views/Application/
The home page contains just one link to download the excel report as shown below
#{extends 'main.html' /}
#{set title:'Home' /}
<a href="@{customerphonelist()}"/> Download Customer Phone Numbers Excel Report </a>
#{set title:'Home' /}
<a href="@{customerphonelist()}"/> Download Customer Phone Numbers Excel Report </a>
Edit the controller Application.java and add a method - customerphonelist() which outputs the excel file as shown below:
package controllers;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang.RandomStringUtils;
import models.Customer;
import play.Logger;
import play.modules.excel.RenderExcel;
import play.mvc.Controller;
import play.mvc.With;
@With(ExcelControllerHelper.class)
public class Application extends Controller {
public static void index() {
render();
}
public static void customerphonelist(){
Logger.info("Generating Customer Phone List Excel report ");
request.format = "xlsx";
Date date = new Date();
String user = "Bob";
List customers = new ArrayList();
for (int i = 0 ;i < 10; i++){
customers.add(new Customer("Mr", RandomStringUtils.randomAlphabetic(15), RandomStringUtils.randomNumeric(10)));
}
renderArgs.put("date", date);
renderArgs.put("user", user);
renderArgs.put("customers", customers);
renderArgs.put(RenderExcel.RA_ASYNC, true);
renderArgs.put(RenderExcel.RA_FILENAME, "customer_list_report.xlsx");
render();
Logger.info("Completed Customer Phone List Excel report ");
}
}
Above at line no 11 - @With(ExcelControllerHelper.class)annotation is used. ExcelControllerHelper class is part of play excel module. @With is an interceptor. This interceptor loads the excel template.
Line No 22 to 25 creates a Customer model class and populates it with some random data.
Line No 26 to 30 puts the objects in to the renderArgs map. These objects would be available as part of the actual excel file which can be used to render the dynamic content.
Line No 29 is of special interest which sets the ASYNC to true. This is a very powerful feature of Play framework. This helps in transparent asynchronous rendering of the excel. You don't have to deal with Future, Promise, await, request.new etc and you get the power for nearly free. The existing HTTP request is suspended and then when the excel report generation is done, this thread is resumed and the response sent back to the client. Think about the fact that a large Excel table rendering might take as long as 500ms and blocking request handling thread for half second is NOT acceptable in a high performance web server like play which use only limited thread (N+1) to handle web requests. Read more about this here.
Last step :), Open
http://localhost:9000and click on the Download Customer Phone Numbers Excel Report link. A excel report should be generated and should look some thing like below.
That completes the example.
September 14, 2011
My Choice - Play Framework!
At my work, there was a need to develop the same application for three different platforms - .NET, LAMP and for Java. I was asked to pick a framework to develop this app in Java. It was easy for my other colleagues for .NET and LAMP as there was not much choice - ASP.NET was picked for .NET and Ruby on Rails for the LAMP stack.
When it comes to Java, I need not say more, there are tons of choices. Burden of Choice.
The objectives were clear -
Spring has kind of become the de-facto standard for enterprises, followed by JBoss SEAM.
I have a good experience with Spring and have been an early adopter of this great framework. Spring became popular because of the pit-falls of EJB2 and heavyweight runtime.
But over the past few releases, Spring has grown to be a huge framework. It takes a lifetime to be familiar with all the modules of the Spring and the features it supports. Recent times - Spring is over-engineered, over-complicated and feature creep.
With JBoss SEAM stack, you cannot avoid JSF. I never learnt JSF and would never learn it. There is already enough rants on the web about the design flaws of JSF.
Spring Roo is another great tool to improve the developer productivity but again it's not straightforward. It involves GWT, Spring. Maven etc. Too much. All goes well for a trival hello-world or a simple CRUD app, the moment you try to do a little more complex web-app, things start falling apart.
No doubt these are great frameworks, but I wanted to avoid maven and its pom file nightmares, classpath issues, layers and layers of abstraction, enforced rules, high learning curve etc.
Enter Play framework !
I'm not advocating this is one of the best frameworks, but it truly matches the productivity of RoR, low learning curve, stateless architecture i.e. made for cloud, dramatically reduces the develop/test/deploy cycle, borrows the best things from RoR, DJango and still retaining the best things about Java (rock solid, enterprise strength scalability and security, great external libraries and developer community etc.).
It has been already a month I'm using this great framework and I must say it has been a very memorable experience.
In my next post, I would be giving some nifty features of this framework and more insights
Until next time ..
When it comes to Java, I need not say more, there are tons of choices. Burden of Choice.
The objectives were clear -
- Rapid development (code - deploy - test cycle)
- Deployable on popular Web/App servers (Apache Tomcat, JBoss, Oracle, Websphere etc)
- Deployable on the cloud
- Best engineering practices (unit testing, functional testing, clean code)
Spring has kind of become the de-facto standard for enterprises, followed by JBoss SEAM.
I have a good experience with Spring and have been an early adopter of this great framework. Spring became popular because of the pit-falls of EJB2 and heavyweight runtime.
But over the past few releases, Spring has grown to be a huge framework. It takes a lifetime to be familiar with all the modules of the Spring and the features it supports. Recent times - Spring is over-engineered, over-complicated and feature creep.
With JBoss SEAM stack, you cannot avoid JSF. I never learnt JSF and would never learn it. There is already enough rants on the web about the design flaws of JSF.
Spring Roo is another great tool to improve the developer productivity but again it's not straightforward. It involves GWT, Spring. Maven etc. Too much. All goes well for a trival hello-world or a simple CRUD app, the moment you try to do a little more complex web-app, things start falling apart.
No doubt these are great frameworks, but I wanted to avoid maven and its pom file nightmares, classpath issues, layers and layers of abstraction, enforced rules, high learning curve etc.
Enter Play framework !
I'm not advocating this is one of the best frameworks, but it truly matches the productivity of RoR, low learning curve, stateless architecture i.e. made for cloud, dramatically reduces the develop/test/deploy cycle, borrows the best things from RoR, DJango and still retaining the best things about Java (rock solid, enterprise strength scalability and security, great external libraries and developer community etc.).
It has been already a month I'm using this great framework and I must say it has been a very memorable experience.
In my next post, I would be giving some nifty features of this framework and more insights
Until next time ..
Subscribe to:
Posts (Atom)


