Play 2.0: Building Web Application using Scala

For people in hurry, here is the code and some steps to run few demo samples.

Disclaimer: I am still learning Scala and Play 2.0, please point to me if something is incorrect.

Few years back I had tried Scala and it was difficult for me to learn it with the lack of good IDE and an integrated stack for building any application primarily Web Application. After the advent of Play 2.0, Scala IDE for eclipse and Play 2.0 integration with Eclipse things have improved. Lift is also another framework to do Scala Web Application development

If you see the net you will see quite a few articles comparing Java with Scala like, From Scala back to Java and Scala or Java? Exploring myths and facts, There are also articles comparing Scala to other languages Ruby like, Scala, is it comparable to Ruby?. Primarily the debate is about if Scala is ready for Enterprise development.

In this blog I will call an external REST call using Akka Actor and display results using Scala. While building this sample application, I will consider few factors and how Play 2.0 and other tools help in this.

Scala IDE support

Refer the blog Setup and use Play framework 2.0 in Scala IDE 2.0. It has a good writeup on how we create a Scala project in Play 2.0, how we Eclipsify, how we import a project in Eclipse and start development. The IDE has a good intellisense support. When I run the application in STS IDE, I had to add target/scala-2.9.1/src_managed/main in the build path.

Database access support

Refer the blog Tutorial: Play Framework 2 with Scala, Anorm, JSON, CoffeeScript, jQuery & Heroku. This has a good writeup on how we write a simple Database CRUD based application in Scala. It also demonstrates how we can return JSON object to the browser. Play 2.0 is bundled with Anorm a lightweight Object mapper. This is not a ORM tool, this is just a simple data retriever and object mapper. If you see the application I demonstrated the usage. Refer the .scala class models.Bar for more details. Below is a simple code snippet of this,

def findAll(): Seq[Bar] = {
DB.withConnection { implicit connection =>
SQL("select * from bar").as(Bar.simple *)

Akka/Actor: taking advantage of asynchronous model

As mentioned in my earlier blog, Harnessing New Java Web Development stack: Play 2.0, Akka, Comet, Play 2.0 uses Netty an asynchronous event-driven network application framework for high performance Web Application. In the below snipped, I will demonstrate how we retrieve a remote webservice payload in a non blocking. I have not benchmarked this yet.

def listProducts() = Action {
val system = ActorSystem("MySystem")
val myActor = system.actorOf(Props[ProductJsonActor], name = "myactor")

Async {
implicit  val timeout= Timeout(20.seconds)

(myActor ? "hello").mapTo[String] {
response => Ok(response).as("application/json")

def receive = {
case _ => {
val response = WS.url("").get()
val body = response.await(5000).get.body
log.debug("body****" + body)
sender ! body

Language capabilities: OOP vs Functional Programming

There are good blogs comparing Functional Programming and OOP Language like Java. The concept of Functional Programming is not that easy to understand in the beginning, but once we get used to it, we can write good compact code.


Language capability wise Scala has good support. Java 8 has lot of features which are similar to Scala and if you get into Scala bandwagon you will not be left out to take advantage of the new capabilities of Java. Here is a good comparison of Java 8 vs Scala. With Play 2.0 I clearly demonstrated how to build a decent Web application. Hope this blog helped you.

About these ads

One thought on “Play 2.0: Building Web Application using Scala


    Thanks for ones marvelous posting! I definitely enjoyed reading it, you’re a great author. I will make sure to bookmark your blog and may come back later on. I want to encourage you to definitely continue your great work, have a nice holiday weekend!


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 )

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