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")

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}>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 :
The Github code for the project is at :

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:



About the Author: Anand Kumar Singh

Hi, I’m Anand Singh and I specialize in Computer Software. I’m passionate about what I do, and I love to help people. Nothing is more fulfilling than being part of a team with similar interests, and an organization that values its employees. I'm Lead Software Engineer at Sapient. Having 5+ years of experience in Java, Grails/Groovy and Scala language. Anand also knows about Javascript, jQuery, CSS and other UI design technologies. Certified developer in: * Principles of Reactive Programming – Coursera * Functional Programming Principles in Scala – Coursera * Functional Program Design in Scala – Coursera * Introduction to Big Data with Apache Spark – edX * Oracle Certified Associate (OCA) * Sun Certified Java Programmer (SCJP) I'm 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, I'm continuing my journey by actively collaborating with other Developers in my field. I'm actively contributing on the TechMonad & Playing Scala platform. I likes to spend his free time in Cooking, watching Discovery for new Technology and visiting new places with friends.

One comment

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s