You are on page 1of 39

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 your
things
Choose from Choose from
things in the things in the
store store
Pay

Queue up Queue up
(1 counter in (Many counters
the shop) In the shop)
Inventory

Packaging
Pay and delivery
Pay

Traditional Hypermarkets 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
...

i t y
i l
a b
a l
S c
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
User choose Go to
Start
products checkout

No Inform user End

Stock exist?

Take payment
Yes End
from user
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 e-
End
commerce
User choose Go to Take payment
Start
products checkout from user
Why asynchronous
messaging important
Simplified e-
End
commerce
User choose Go to Take payment
Start
products checkout from user

Fire another process

Yes Packaging +
End
Shipment

Verify stock Stock exist?


Email to user –
item would be End
No
a bit late
Long running transaction

Short OLTP
High volume
processes

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
Reimburse 80%
Back to client
Exception!
Ship to client's
premise
Ship back to
Client cancel 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
Are you kidding me?
Image from: http://www.travelblog.org/Europe/Netherlands/North-Holland/Volendam/blog-385862.html
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 End

User choose Go to Take payment


Start
products checkout from user

Fire another process

Yes Packaging +
End
Shipment

Verify stock Stock exist?


Email to user –
item would be End
Actor1 No
a bit late
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”


a t ed
Between the Scala world and MDB
k rel
r Tre sed! ck!
a u
: St ng u k, d
i
I NG s be gee
A RN y i ot a
W alog e n
an ou'r
If y
Combining actors and
message driven beans
Scala world JMS world

Request
Actor javax.jms.MessageListener
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

You might also like