Play framework – The next web server phenomenon

As I have learned, there are more advancements in Web technologies which makes software development much easier, less complicated, and its end product to be highly efficient, stable, robust, fast, reliable and yet takes complex requirements. And top of it all, it should never take the user experience for granted, and keep in mind the developer experience as well.

Play framework is an emerging technology. It is a web server that can be created in Java or Scala. ( Scala is not as popular as Java, but it was created to address Java shortcomings ) It is foreseen to replace the J2EE web server technologies. It is comparable to MongoDB ( or any NoSQL ) , which has very high potential of replacing the SQL.

To code using Play framework, the concept is in MVC but without servlets or any web server, since Play is already the web server. Other MVC frameworks will not be used also ( ex. Spring ). Front end code is still needed such as Javascript ( or CoffeScript, JQuery, AngularJS ) and CSS. Server side logic can be coded using Java or Scala.

Comparison with J2EE: J2EE web technologies have been the standard and reliable web server on most enterprise applications, with Java as the top programming language of choice. J2EE technology is built on top or constructed with other modules to make it functional. A full stack J2EE web application should have a J2EE container / web server, the J2EE application package itself, which is integrated with a MVC framework such as Spring and a Database access component such as Hibernate. Currenty J2EE application have become bloated with layers of modules and log files have too much hierarchy. It is because J2EE has become an increasing version of add-ons and enhancements.

Play framework eliminates all of those J2EE modules. It is not a Servlet technology. Play framework is a Non-Blocking I/O. Non-Blocking I/O means that HTTP request and processing in CPU side of the server is asynchronous, Yet Servlet 3.1 already supports Non-Blocking I/O, Play framework has Websocket support, which is not in Servlet 3.1. And best part of Play framework is the simplicity of the debug logs and application modules, where J2EE has its drawbacks.

I’m really impressed with this new Web Server advancements in Play framework. It is really comparable to the shift from SQL to NoSQL. SQL and NoSQL databases have the same purpose, a Database, yet its design and architecture is completely different. For simplicity, yet have served its function as a Database, NoSQL ( ex. MongoDB ) does it. It is much easier to setup ( ex. in Java, we just need the libraries and codes for transaction ), compared to SQL ( ex. Hiberante ) , which needs code, models, configuration files, and even integration with Spring to make its code less. So in this case, Play framework is like the NoSQL solution to an already legacy code such as J2EE.

Some Cons: Yes Play is better, but it is still new. So there may be some uncertainties hiding in the code. Another drawback of Play is the Scala and its build code ( called SBT , which is similar to Ant / Maven ). Play is built in combination with Java and Scala, So for non-Scala developers, it’s a steep learning curve.

Production components: For production and deployment, Play is integrated with Netty ( HTTP server ) and Akka ( Background Process ). No need of Webservers ( Tomcat, JBoss, IBM Websphere and etc.. ) . Other sub components are Templates, SBT and Websockets ( Websockets and be used as an option only ). Just include front end JS and CSS to be added in Play. Other third party libraries in Java ( if using Play as Java based, not Scala based ) can be added.

Bonus Factors: This technology is really new, with only a few enterprise websites using it, and as of this writing, they only have 5 on their list. See the list here in their home page. LinkedIn is one popular site using Play, so there is no question of its performance. I also checked zaptravel.com , and it loads pretty fast too. Compared to MongoDB which already has numerous users, across all industries, so similarly, in a matter of years, Play framework will get more attention.

Highlights:

– Lightweight
– Has authentication and security
– Can easily be switched from development mode to production mode via a single configuration.
– Has support for Design Patterns ( ex. Singleton and etc .. )
– Not a Servlet
– Not tied up to SQL . Can handle other Database types also
– No need of Web Server ( Or a servlet container ). The Play framework is the server itself.
– Can be scaled for production
– pretty darn straight forward, not so layered.
– Supports Unit test
– Better error detection / Direct to the point compile errors, does not display a full stack trace
– built as non blocking I/O. Adapts to multi core CPU.
– Much better compared to node.js and ruby on rails. Which I still have to find out the reason…

Here’s the explanation why Play framework is really fast.
https://playingscala.wordpress.com/2015/05/01/why-play-framework-is-fast/

Some important links:

1.) routing of http requests:

http://stackoverflow.com/questions/15907996/how-to-get-query-string-parameters-in-java-play-framework?rq=1

2.) basics

https://www.playframework.com/documentation/1.2.x/faq

3. ) philosophy

https://www.playframework.com/documentation/2.3.x/Philosophy

4.) One user even got embarrassed.. or how it is unbelievalbe good.

https://dzone.com/articles/play-framework-now-officially

Advertisements

Mongo DB in Windows: Recovery after improper shutdown

Simple Tip:
When you encounter a problem when Mongo DB will not start up,
Go to the Data folder ( see the data folder here ) and delete the LOCK file. Then restart the MongoDB and check the results. If it still fails, delete all files in the data folder. Yes, but only do this on a Development or Test system.

To properly shutdown MongoDB, use the “Ctrl+C” command in the Windows console where MongoDB is running.

Mongo DB in Java: Get a single database entry Value by Key

How to get a single value String by using the key of a single return from a Database search query?

As an example, we would get each value in a single row. But we first have to search the collection ( or in SQL terms, a table ) where the email address is equal to some value. In this case we use the [email] variable

See the sample Java code below, Then each line is commented with an explanation


//This will store the single row result in a setter-getter object
Account newAccount = new Account();

//create the "where" query to search an email.
//"email" first parameter is the key. [email] second parameter is //the search string, which is a variable.
BasicDBObject whereQuery = new BasicDBObject();
whereQuery.put("email", email);

//find the collection row, using the where condition created.
DBCursor cursor = accountsColl.find(whereQuery);

//loop the result using a DB cursor
while(cursor.hasNext()) {

 //store the "cursor.next()" value in another object,
 //since directly accessing the result will cause 
 //a NoElementException when this type of loop is used
 DBObject tobj = cursor.next();

 //Also store the Strings in a separate objects
 String tempName = (String)tobj.get("name");
 String tempEmail = (String)tobj.get("email");
 String tempPass = (String)tobj.get("password");

 //set the results to the Account Object
 newAccount.setName(tempName);
 newAccount.setEmail(tempEmail);
 newAccount.setPassword(tempPass);

}

return newAccount;

MongoDB in Java: Search for a value in a nested entry

Given the MongoDB entry below, how do we search for “occupation: doctor”?

{
"name" : Maren,
"age" : 29,
"details" : {
 address : "Geronimo Road"
 occupation: "doctor"
 }
}

{
"name" : Skoo,
"age" : 35, 
"details" : {
 address : "West Village, Hudensta"
 occupation: "warrior"
 }
}

{
"name" : Doby,
"age : 31,
"details" : {
 address : "99 Jump Street"
 occupation: "doctor"
 }
}

The result should give us the complete data for “Maren” and “Doby”.
“Skoo” should not appear in the search result.

Solution:
In MongoDB Java, accessing data inside a nested object must be accessed using the “details” key. We must first know the “details” key in order for us to access the “address” and “occupation” keys, which is under the root object. The Java code solution is provided as follows.

BasicDBObject searchCriteria = new BasicDBObject();
		
searchCriteria.put("details.occupation", "doctor");
DBObject result = collection.findOne(searchCriteria);
		
System.out.println(result);

Credits to this blog post below, I got the idea here:
http://rstechjournal.blogspot.com/2012/09/mongodb-search-nested-value-with-java.html

Starting up with MongoDB

Here’s how to install MongoDB in Windows OS

1. Download here:
http://www.mongodb.org/downloads

2. Type “wmic os get osarchitecture” in ( cmd ) command line to determine if 32 or 64 bit for Windows.

3. Download takes approximately 20 mins or less.

Note: size of file is 132 MB

4. Install

5. Make sure to run this command >
“C:\Program Files\MongoDB 2.6 Standard\bin\mongod.exe” –dbpath C:\MongoTest\data ( change the appropriate paths )

If you don’t do this command, then you will encounter this error. See below:

install_error

6. After 5 is completed you will get this screen, and the \data folder will have these contents

after_create_data_folder_command

data_folder_contents

7. run mongo.exe open the open the MongoDB shell. This should be the result.

mongo_shell_connected

8. This is all set, now we can create a Java Application that can access the MongoDB

Installation is described here more in detail:

http://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows/

MongoDB and Java

This time, there is a rise of the NoSQL database. For a lot years, database has always been associated with SQL language, but there’s an emerging technology that does not uses SQL at all. Yes its a database that’s a form of simplified SQL. For example, NoSQL has:

1. No JOINS

2. Do not need to create tables to insert data.

3. JSON style storage

4. Using a lot of “.” annotation to query deep data.

MongoDB uses the following Data Models.

1. Embedded Data Models.

The models follow a JSON style format just like this.

{
 _id : 21
 user:
 contact: {
  phone: 212-345
  email: ahdn@example.com
 },
 access: {
  level: 1
  group: dev
 }
}

Above is a two layered embedded document, we can create this to 3 or 4 layers. Here’s an example of 3 layers. 1st layer is the “_id”, second is the “contact”, third is the “email”

{
_id : 21
user: abc
contact: {
 phone: 212-345
 email: {
  personal: ahdn@example.com
  office: ahdn@office.com
  },
 }
}

2. Normalized Data Models

below is an example of Normalized Data Models, which uses id for referencing.

=================================

{
 _id : 21
user: “abc”
}

{
_id : 1
 user_id: 21
level: 1
group: “dev”
}

{

_id : 1
user_id: 21
phone: “212-345”
email: “ahdn@example.com”
}

=================================

Integration with Java

( improved ! )

To use MongoDB in Java, it is needed to import ‘com.mongodb.*’ libraries. It can be downloaded here: https://github.com/mongodb/mongo-java-driver/downloads .

To use MongoDB, here are the steps what to include in the Java Class:

1. Install a running MongoDB

2. Create a connection to the Database. Use MongoClient Object.

example:  MongoClient mongoClient = new MongoClient( “localhost” );

2. Use DBCollection Object to insert a collection.

example: DBCollection collection = db.getCollection(“dummyColl”);

3. Use DBCursor Object to find the Document ( refers to the data set inserted )

example:

BasicDBObject document = new BasicDBObject();
        document.put(“database”, “testDB”);
        document.put(“table”, “hosting”);

collection.insert(document);

More references here:

http://blog.mongolab.com/2012/08/why-is-mongodb-wildly-popular/

http://www.tutorialspoint.com/mongodb/mongodb_java.htm

http://docs.mongodb.org/manual/core/data-model-design/

http://www.mkyong.com/mongodb/java-mongodb-insert-a-document/