Kicking Butt on Concurrent Enterprise Application with Scala
MSC Malaysia Open Source Conference 2009 1 June 2009. Developers Track
http://www.mscmalaysia.my/opensource
http://www.mscoscon.my/
http://www.osdc,my/
more from Azrul
Lightning talk MSC Malaysia Open Source Conference 2009
3 June 2009. 5pm Bronx V
Archive
http://mscoscon.blogspot.com/2009/06/cyber-merdeka-cyber-sovereignity-by.html
Kicking Butt on Concurrent Enterprise Application with Scala
1. Kicking butt on concurrent
enterprise application with
Scala
By
Azrul MADISA
Freelance Java Developer
azrulhasni@gmail.com
2. Plat du jour
The concurrent
enterprise
The next billion users
Asynchronous
messaging
Enter Scala
Scala concurrency
Scala actors
Scala and Message
Driven Beans
3. 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)
4. 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
5. Other sectors
Telco
Switch board → Exchanges →
NGN
Government
Counter based → E-Government
Finance
Counter → ATM → E-Banking
Others
Millitary, logistics, media &
content
6. 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
7. 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
9. One word to rule them all
...
i t y
i l
a b
a l
S c
10. 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.
11. 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
12. 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
13. Why asynchronous
messaging important
Simplified e-
End
commerce
User choose Go to Take payment
Start
products checkout from user
14. 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
15. Long running transaction
Short OLTP
High volume
processes
We somewhat know
how to do this
High volume
Long running
processes ?
17. 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
18. 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
;)
19. 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
20. 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”)
22. 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
23. 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
25. 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
26. 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
27. 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
28. 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
}
29. 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
30. 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
32. Combining actors and
message driven beans
Scala Actors Message driven bean
`
Bridging agent acting like a “worm-hole” d
Between the Scala world and MDB el ate
re kr !
!
ta r T used uck
d
: S eing ek,
NG e
R NI is b t a g
y
WA alog e no
an ou'r
If y
33. 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
36. 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
38. 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 … )
39. 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