Category Archives: Asynchronous

Why Play Framework is fast?


Published on Typesafe trainer and consultant

Advertisements

Map reduce with Akka and Scala: The famous word count


Knoldus

After working for around a decade with Java and family, recently i tried my hands with Scala and Akka. Yes! changing taste buds is not at all easy. But working with Scala is fun!  Prior to this, every time when i start on building some APIs, First thing which comes in my mind is

Number of java classes or beans. Honestly a POJO is never more than having getters and setters. (Do i need them?)

Scala just made it easy. Let’s see how!

Java

Honestly in previous java blogs i used to skip these setters and getters by simply mentioning as

// getters and setters!

With Scala it’s really easy.

By the way, you can also define a property class for this and access it as property.
There are number of things which makes Scala having an added advantage over Java(e.g. immutability, utility functions etc.) but i will skip these…

View original post 323 more words

Dribbling with Filter.js: client-side JS filtering of JSON objects


Knoldus

Dribbling Filter.js

Play framework with client-side JS filtering of JSON objects and rendering HTML snippets via jQuery.

Big chunk to display? Interactive filtering? Most importantly it has to be really fast. Isn’t it like dribbling against Netherland! Big ground, lots of hooting and most importantly have to be fast and win
 
UI programming is an exciting ground to play, That’s why i chose reactive platform play framework. But problem was how to dribble at client side. Need an api to provide fast filtering of data. Answer is Filter.js

In this post we are integrating Filter.js with Play Framework for reactive data streaming using Ajax and filtering data on client side using Filter.js magic.

Filter.js is client-side JSON objects filter to show/hide HTML elements. Multiple filter criteria can be specified and used in conjunction with each other.

Mustache.js is a logic-less template syntax. It can be used for HTML, config files…

View original post 162 more words

filter.js – Client side search filtering using JSON and jQuery


I am planning to integrate it with Play framework.

Josh Software - Where Programming is an Art!

Speed for search result filtering is critical. Its fine for site users to wait for some time (maybe a few seconds) to load the search results but after that filtering better be fast otherwise people lose interest.

To give a simple example of how things are not user friendly is if you go to ebay.com or ebay.in. Its frankly appalling! For every click and every selection, the entire page refreshes. Though its good for showing Ad impressions (which I hate) when browsing an e-commerce site, the usability is lost.

On the other hand, have a look at snapdeal.com or groupon.com and it tells a different story. They fire Apis to their server to get json data and render that via Javascript. Usability is excellent. (no wonder they are doing so well!)

We did client-side search filtering in a couple of portals we built and realized that we should generalize this. That…

View original post 439 more words

A simple CRUD application in Play! Framework using Kundera and Cassandra as a Database.


playing-kundera-cassandra: Play 2.3.0, Kundera & Cassandra

Knoldus

playing-kundera-cassandra: Play 2.3.0, Kundera & Cassandra

A simple CRUD application in Play! Framework using Kundera and Cassandra as a Database.

  • Accessing a Cassandra database, using Kundera.
  • Achieving, Futures to use more idiomatic error handling.
  • Accessing JS & CSS libraries by WebJars.
  • Bootswatch-United with Twitter Bootstrap 3.1.1 to improve the look and feel of the application

Instructions :-


  • The Github code for the project is at : playing-kundera-cassandra
  • Clone the project into local system
  • To run the Play framework 2.3.0, you need JDK 6 or later
  • Install Typesafe Activator if you do not have it already. You can get it from here: download
  • Install Cassandra if you do not have it already. Instructions
  • Start Cassandra Server and create schema/tables
[default@unknown] create keyspace EmployeeExample;
395c9500-bf8d-3985-95a2-ddc055090131
[default@unknown] use EmployeeExample;
Authenticated to keyspace: EmployeeExample
[default@EmployeeExample] create column family employees with comparator=UTF8Type and default_validation_class=UTF8Type and key_validation_class=UTF8Type;
840d66b1-a54a-329e-9a43-dc9f373e9386
  • Execute activator clean compile to…

View original post 90 more words

playing-reactive-mongo: Classic CRUD application in Play Framework 2.3.0 with ReactiveMongo, WebJars and Bootswatch-United


Knoldus

Play 2.3.0 application with ReactiveMongo – reactive, asynchronous and non-blocking Scala driver for MongoDB.

This is a classic CRUD application, backed by a MongoDB database. It demonstrates:

  • Achieving, Futures to use more idiomatic error handling.
  • Accessing a MongoDB database, using ReactiveMongo.
  • Achieving, table pagination and sorting functionality.
  • Replaced the embedded JS & CSS libraries with WebJars.
  • Play and Scala-based template engine implementation
  • Bootswatch-United with Twitter Bootstrap 3.1.1 to improve the look and feel of the application

Instructions :-


  • The Github code for the project is at : playing-reactive-mongo
  • Clone the project into local system
  • To run the Play framework 2.3.0, you need JDK 6 or later
  • Install Typesafe Activator if you do not have it already. You can get it from here: download
  • Execute activator clean compile to build the product
  • Execute activator run to execute the product
  • playing-reactive-mongo should now be accessible at localhost:9000

References :-


View original post 68 more words

Play Framework 2.2 with WebJars client-side web libraries


WebJars are client-side web libraries (e.g. jQuery & Bootstrap) packaged into JAR (Java Archive) files.

WebJars can be added as dependencies to an app by simply adding them to the build.sbt file. There is also a helper library named webjars-play that makes it easy to reference WebJar assets. Here is an example build.sbt file with webjars-play and the bootstrap WebJar as dependencies.

Do the following steps to implement WebJars client-side web libraries with Play Framework:

  1. Add following entry into build.sbt file.
"org.webjars" %% "webjars-play" % "2.2.2-1",
"org.webjars" % "bootstrap" % "3.1.0",
"org.webjars" % "bootswatch-yeti" % "3.1.1",
"org.webjars" % "html5shiv" % "3.7.0",
"org.webjars" % "respond" % "1.4.2"

2. Add following entry into  conf/routes file.

# Map the webjar static assets to the /webjars URL
GET     /webjars/*file                    controllers.WebJarAssets.at(file)

3. Add following entry into app/views/main.scala.html file.

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" media="screen" href="@routes.WebJarAssets.at(WebJarAssets.locate("bootswatch-yeti", "bootstrap.min.css"))">

<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
  <script src="@routes.WebJarAssets.at(WebJarAssets.locate("html5shiv.js"))"></script>
  <script src="@routes.WebJarAssets.at(WebJarAssets.locate("respond.min.js"))"></script>
<![endif]-->

<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="@routes.WebJarAssets.at(WebJarAssets.locate("jquery.min.js"))"></script>
        
<!-- Latest compiled and minified JavaScript -->
<script src="@routes.WebJarAssets.at(WebJarAssets.locate("bootswatch-yeti", "bootstrap.min.js"))"></script>

Check the application and the code base on bellow links:

The live application is currently hosted at : http://employee-self-service.herokuapp.com/
The Github code for the project is at : https://github.com/knoldus/Employee-Self-Service

This is the fourth edition of this application. If you have any changes then feel free to send in pull requests and we would do the merges 🙂 Stay tuned.

For more information about WebJars visit the website: http://www.webjars.org

Play Framework: Reactive and Non-Blocking Database Access


If we want to make a web application request as Reactive and Non-Blocking Database  Access than we need to do the following things:

  • Returning futures

While we were using the Action.apply builder methods to build actions until now,    to send an asynchronous result, we need to use the Action.async buider method:

import play.api.libs.concurrent.Execution.Implicits.defaultContext

def list =Action.async {
    val futureData = scala.concurrent.Future{ DATABASE_CALL}
    futureData.map(data => Ok("Got Data: "+ data))
}
  • Handling time-outs properly

It is often useful to handle time-outs properly, to avoid having the web browser block and wait if something goes wrong. You can easily compose a promise with a promise timeout to handle these cases:

import play.api.libs.concurrent.Execution.Implicits.defaultContext
import play.api.libs.concurrent.Promise
import scala.concurrent.duration._

def list = Action.async {
     val futureData = scala.concurrent.Future{ DATABASE_CALL}
     val timeoutFuture = Promise.timeout("Oops got error",10.second)
     Future.firstCompletedOf(Seq(futureData, timeoutFuture)).map {
        case data: CASE_CLASS => Ok("Got data: "+ data)
        case error: String=> InternalServerError(error)
     }
}

Check the live application and the code base on bellow links:

  • The live application is currently hosted at :

http://employee-self-service.herokuapp.com/

  • The Github code for the project is at :

https://github.com/knoldus/Employee-Self-Service

For more information please check the given link :

http://blog.knoldus.com/2014/03/31/employee-self-service-2/