Kicking butt on concurrent enterprise application with Scala

By Azrul MADISA Freelance Java Developer azrulhasni@gmail.com

Plat du jour
The concurrent enterprise The next billion users Asynchronous messaging Enter Scala Scala concurrency Scala actors Scala and Message Driven Beans

The ever concurrent enterprise
Service A Service A Service A

Service B Service B Service B Service C Service C Service C Service D Service D Service D

Traditional

Modern

Web N.0 (large # of instances)

Commerce example
Choose from things in the store Choose from things in the store Choose your things

Pay

Queue up (1 counter in the shop)

Queue up (Many counters In the shop)

Inventory

Pay Traditional

Pay Hypermarkets

Packaging and delivery E-Commerce

Other sectors
Telco Switch board → Exchanges → NGN Government Counter based → E-Government Finance Counter → ATM → E-Banking Others Millitary, logistics, media & content

The next billion users
Sum of Bandwidth: Google, Twitter, Facebook etc. HPC (High performance Computing) Genomics, Animation *Prise (Enterprise) Cloud based, Saas,

* http://blogs.sun.com/Gregp/entry/a_word_or_two_on

Don't mean to scare ya but...
People are more network centric
Every mobile device is a potential terminal to your services How many mobile devices are there? A couple of billion ?

People are not the only ones accessing your services
[Please hum the X-Files theme song in your head right now]

=> Computing demand grows faster than Moore's law
Also known as the red shift

One word to rule them all ...

One word to rule them all ...

c S

l a

b a

l i

y t i

What technology will allow this?
Criteria Play well with current enterprise systems
Play well on the JVM

Fault tolerent Share-nothing philosophy
Asynchronous messaging No side effect (Maybe functional)

Easy to learn (tool support, language LAF) Performance – At least as good as current offerings Take advantage of multi-core tech.

Why asynchronous messaging important
E-commerce example:
Verify stock Start User choose products Go to checkout

No Stock exist? Yes

Inform user

End

Take payment from user

End

Why asynchronous messaging important
Verify stock
Millions of items Kicker... need to lock an item while reserving it
To avoid 2 person buying the same thing

Lengthy => Clients will run away

Why asynchronous messaging important
Simplified ecommerce
Start User choose products Go to checkout Take payment from user End

Why asynchronous messaging important
Simplified ecommerce
Start User choose products Fire another process Yes Verify stock Stock exist? No Email to user – item would be a bit late End Packaging + Shipment End Go to checkout Take payment from user End

Long running transaction
High volume Short processes OLTP

We somewhat know how to do this

High volume

Long running processes

?

Long running transaction
Client pay

Package an item

Ship to client's premise

Client sign off

Long running transaction
Client pay

Package an item
Exception! Reimburse 80% Back to client

Ship to client's premise
Client cancel

Ship back to vendor's premise

Client sign off

Enter Scala
Created by Martin Odersky Made is Swiss (no, it's not a knife nor a kind of cheese) Run on Java JVM Java is king of the enterprise, running on JVM ensures adoption. Can use and inherit Java classes. Also runs on .Net CLR Object oriented and functional at the same time Statically typed with type inference Recently used by Twitter on the back-end ... replacing that other you-know-which language ;)

Why not the other JVM languages
JRuby, Jython , <The dynamic language of the day>
Duck typing? No thanks

Clojure
Interesting but syntax is too “lisp-y” - My personal opinion

Plain old Java
Image from: http://www.travelblog.org/Europe/Netherlands/North-Holland/Volendam/blog-385862.html

Are you kidding me?

Scala concurrency: Immutable variables
Immutable variables
Variable that cannot change its value Useful in concurrent apps
State cannot be inconsistent

Easily declare variables as immutable The “val” keyword automatically makes variable immutable
val movie = new Movie(“Wolverine”)

Scala concurrency: Immutable variables
Can Java do this?
Java does this on String For other object:
Tedious since Java don't have operator overloading

Scala concurrency: Easy singleton
Often times, we need one single object per application E.g. Some utility class to access the database Scala provide easy singleton:
object MySingleton{ //singleton's body }

The singleton's constructor is fully synchronized

Scala concurrency: Easy singleton
Can we do this in Java
Sure, but, again, it's tedious You have to create all the boilerplat code yourself Refer to the Gang Of Four book

Scala concurrency: Actors
Scala has actors
… and no, they're not as bad as Paris Hilton

Scala concurrency: Actors
Actors Actors are objects Receive message asynchronously Reply with a Future Future = placeholder of future result Concurrency without threading Event-driven Unlike real-world actors, Scala actors are
Cheap Totally ignored by politicians

Scala concurrency: Actors
Event driven architecture
Unlike threads, actors are event-driven
Only become alive when there are messages to be processed. Execute on a thread pool With message passing, number of locks are low
Great for multicore processors

Scala concurrency: Actors
MainApp
Start User choose products Fire another process Yes Verify stock Stock exist? No Email to user – item would be a bit late End Packaging + Shipment End Go to checkout Take payment from user End

Actor1

Scala concurrency: Actors
case class MyMessage(myPaylod:String) //declare type of message object Actor1 extends Actor { def act = { loop { react { case MyMessage(“Continue e-commerce process”) => //Manage Stock verification, Packaging and Delivery here … //closing curly braces } object MainApp extends Application { //Do your business logic here, once you get to Payment ... Actor1.start Actor1 ! MyMessage(“Continue”) //send message to actor Actor1 }

Scala concurrency: Actors
Can we do this in Java
In plain old Java, thread can be used Thread can not be created in a Java EE container Have to use JMS
OpenMQ ActiveMQ Message Driven Beans Message Driven Pojo with Spring

Scala concurrency: Reliability
Short-comings of Scala messages
They do not live in a transaction They cannot travel through a network

There are projects to address this short coming
Scala OTP (Equivalent to Erlang OTP) ActiveObject

Another approach is to combine Scala and Java EE

Combining actors and message driven beans
Scala Actors Message driven bean

`

Combining actors and message driven beans
Scala Actors Message driven bean

`

Bridging agent acting like a “worm-hole” Between the Scala world and MDB

d ate el kr ! re ! r T used uck ta d : S eing ek, e NG NI is b t a g R y WA alog e no an ou'r If y

Combining actors and message driven beans
Scala world Actor javax.jms.MessageListener JMS world Request Queue

WormholeActor MDB

Scala object

Spring's Message Driven Pojo Response Queue

Performance

Shamelessly plucked from http://shootout.alioth.debian.org/u32q/benchmark.php?test=binarytrees&lang=all

Tools
Netbeans, Eclipse and IDEA plugin

My wish list
Scala actors working in XA Developing EJB 3.1 in Scala Seamlessly portable Scala apps to .Net Better IDE support Scala actors that can travel through a grid/cloud/network Pi-calculus Scala + SOA An international Scala conf. in Malaysia A magic flying talking pony err... car

Other interesting stuff
Lift
Scala web framework based on actors

Scala books

Testing
ScalaTest

References
The Scala official website scala-lang.org Ted Neward's Scala For Busy Java Developer Daniel Spiewak's Scala for Java Refugees Debasish Ghosh's Scala Actors 101 Various blogs, forums, people (too many to name … )

End note
Scala is a sharp weapon to use in the coming “highly concurrent” age Hope to get you excited over Scala Play with it Test it Use it Twitt/blog about it Get something moving in Malaysia

Sign up to vote on this title
UsefulNot useful