Reactive Application

Over the past few years, cloud computing has started to take an important role in our lives. Cloud computing boosted the use of web application. Every organization from small size startup to an older giant like IBM, Oracle, Microsoft every one start creating their application on the web or started exposing their old legacy tool over the web using  SaaS, PaaS or IaaS.

Thinks changes very quickly for the Web server and Application server, older thread per request model failed drastically, old web servers like Tomcat, glassfish and Apache web are not able to fully utilize the newer multicore processor and cheaper main memory.

A reactive web server like Netty is the answer to the new high availability, less memory requirement, and fully CPU utilization.

The reactive manifesto states systems that have Responsive, Resilient, Elastic and Message Driven and all these aspects are already recognized individually, should be called reactive system.

Systems built as Reactive Systems are more flexible, loosely-coupled and scalable. This makes them easier to develop and amenable to change. They are significantly more tolerant of failure and when failure does occur they meet it with elegance rather than disaster. Reactive Systems are highly responsive, giving users effective interactive feedback.

Reactive Systems are[*]:

Responsive: The system responds in a timely manner if at all possible. It can detect error quickly and deal with it efficiently.
Resilient: The system stays responsive in the face of failure. Resilience is achieved by replication, containment, isolation and delegation.
Elastic: The system stays responsive under a varying workload. Reactive Systems can react to changes in the input rate by increasing or decreasing the resources allocated to service these inputs. 
Message Driven: Reactive Systems rely on asynchronous message-passing to establish a boundary between components that ensures loose coupling, isolation and location transparency.  Non-blocking communication allows recipients to only consume resources while active, leading to less system overhead.

Screenshot 2017-04-08 04.18.30

Play Framework uses Netty as a web server. The Play framework has been designed from the scratch to fulfill the need of a reactive application. Play Framework is capable of providing real-time behavior to users even in highly loaded traffic. It fulfills the requirements of the reactive manifesto.

Download Activator

Lightbend Activator helps you to start with Play Framework, Akka, and Scala projects. You can download it from this Activator Zip file link or visit https://www.lightbend.com for the latest version.

Environment Variable Setup

You can set Activator bin location in .bashrc file in Ubuntu, .bash_profile in RedHat and in .profile file in Mac OS, for ease of use.

export ACTIVATOR_HOME = "/Users/rahulkumar/work/activator-dist-1.3.12"

export PATH=$PATH:$ACTIVATOR_HOME/bin

Do source to the current configuration file, which will execute the contents of a script in the current shell.

source .bashrc

Now you are set to use activator in any directory from any new shell session.

From command line type following command.

activator new

You will then be prompted to enter an application name and the template name to start with

Fetching the latest list of templates...

Browse the list of templates: http://lightbend.com/activator/templates
Choose from these featured templates or enter a template name:
1) minimal-akka-java-seed
2) minimal-akka-scala-seed
3) minimal-java
4) minimal-scala
5) play-java
6) play-scala
(hit tab to see a list of all templates)

select 6 for play-scala project and enter a project name, after activator will create a project directory with a basic code.

 LICENSE Licence information, You can change it as per requirement
 README  → Here You can write about project details or any Info as per need.
 app  → Core of Play application contails all server side code bases.
 bin  → Play code base comes with activator binary, so we can use this to run app.
 build.sbtSimple scala sbt build file, add external libries dependencies here.
 confAll project configuratin file you will find here. 
 libexecContains activator-launch-1.3.12.jar, it is used by activator internally.
 projectContains project build properties file and plugins conf file.
 publicAll static assets you can fild here like html, javascript, css, images etc.
 test  → You can put all test case here for app.

IDE Set Up

Eclipse

Integration with Eclipse requires sbteclipse 4.0.0 or newer.

Add the following into project/plugins.sbt file.

addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "4.0.0")

You must compile your project before running the eclipse command. Activator uses eclipse To transform a Play application into a working Eclipse project.

sbt compile
sbt eclipse

Now You can open a project in eclipse IDE.

IntelliJ IDEA

IntelliJ IDEA lets you quickly create a Play application without using any extra configuration or command. Just make sure that the latest Scala Plugin is installed and enabled in IntelliJ IDEA.

Now you can Open Project wizard, select Import Project.

Run Play  project

To run, test, compile and help, commands are respectively below

$ run
$ test
$ compile
$ help
$ cd project_folder
$ sbt run
http://localhost:9000

Anatomy of a Play application code

Play Framework is a REST first web framework, we can create REST web service with it in very less time.

You can use controllers package to put all API functions.

def index = Action {
  Ok(views.html.index("Your new application is ready."))
}

Play framework exposes each function using Routes file.

GET     /                                   controllers.HomeController.index
POST    /api/v0/signUp                      controllers.admin.CredentialsAuthController.signUp
DELETE  /api/v0/deleteReport/:reportId      controllers.scheduledreport.ReportController.deleteReport(reportId: String)
PUT    /api/v0/updateUser                   controllers.admin.AdminController.updateUser

View package contains all html files, Play comes with Twirl, a powerful Scala-based template engine, whose design was inspired by ASP.NET Razor.

@(message: String)
@main("Welcome to Play") {
message
}

All Public assets like Javascript, CSS, and Images You can put in the public folder and can be accessed through Play’s Router class.

<link rel="stylesheet" media="screen" href="@routes.Assets.versioned("stylesheets/main.css")">
<link rel="shortcut icon" type="image/png" href="@routes.Assets.versioned("images/favicon.png")">

 

You can clone a seed project from here source code.

 

 

 

Posted by:Rahul Kumar

Rahul Kumar working as a Technical lead at Bangalore, India. He has more than 5 years of experience in distributed system design with Java, Scala, Akka toolkit & Play Framework. He developed various real-time data analytics applications using Apache Hadoop, Mesos ecosystem projects, and Apache Spark. He loves to design products around big data and with high velocity streaming data. He had given a couple of talks on Apache Spark, Reactive system and Actor Model in LinuxCon North America, Cassandra summit & Apache Bigdata Summits.

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