Category Archives: Non-Blocking Database Access

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

Advertisements

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/