Employee-Self-Service: Reactive and Non-Blocking Database Access using Play Framework and Anorm – (Part-3)


Last week, We have added Reactive and Non-Blocking behaviour in Employee-Self-Service  application.

Now we have implemented Database Access as Reactive and Non-Blocking:

  • Before

def list() = Action.async { implicit request =>
   Employee.list match {
    case Right(data) => Promise.timeout(Ok(html.list(data)), 1 seconds)
    case Left(error) => Promise.timeout(Ok(html.list(Nil)), 1 seconds)
   }
}
  • After

def list() = Action.async { implicit request =>
 val futureEmpList = scala.concurrent.Future { Employee.list }
 val timeoutFuture = Promise.timeout("Oops", 10.second)
 Future.firstCompletedOf(Seq(futureEmpList, timeoutFuture)).map {
   case data: List[Employee] => Ok(html.list(data))
   case t: String =>
      Logger.error("Problem found in employee list process")
      InternalServerError(t)
   }
}

Details: 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 index =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 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 third 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.

Previous blogs related to this application:

1. https://blog.knoldus.com/2014/03/24/employee-self-service-building-reactive-play-application-with-anorm-sql-data-access/

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

About Anand Kumar Singh

Lead Software Engineer at Rklick Solutions LLC. He is having 4+ years of experience in Java, Grails/Groovy and Scala language. Anand also knows about Javascript, jQuery, CSS and other UI design technologies. He is SCJP and OCA certified developer. Anand is passionate to work with upcoming technologies and bleeding platforms providing the new way to programming & development. He is actively involved in development & maintenance of several software projects. He has been enjoying Scala for more than a year now. Anand completed his MCA from IGNOU. As a Developer, Anand is continuing his journey by actively collaborating with other Developers in his field. Anand is actively contributing on the ScalaGeek platform. He likes to spend his free time in Cooking, watching Discovery for new Technology and visiting new places with friends.
This entry was posted in Non-Blocking, Play Framework, Reactive, Scala, Web and tagged , , , . Bookmark the permalink.

One Response to Employee-Self-Service: Reactive and Non-Blocking Database Access using Play Framework and Anorm – (Part-3)

  1. Pingback: Employee-Self-Service: Reactive and Non-Blocking Database Access using Play Framework and Anorm – (Part-4) | Knoldus

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s