39
Kicking butt on concurrent enterprise application with Scala By Azrul MADISA Freelance Java Developer [email protected]

Kicking Butt on Concurrent Enterprise Application with Scala

Embed Size (px)

DESCRIPTION

Kicking Butt on Concurrent Enterprise Application with ScalaMSC Malaysia Open Source Conference 2009 1 June 2009. Developers Trackhttp://www.mscmalaysia.my/opensourcehttp://www.mscoscon.my/http://www.osdc,my/more from AzrulLightning talk MSC Malaysia Open Source Conference 20093 June 2009. 5pm Bronx VArchivehttp://mscoscon.blogspot.com/2009/06/cyber-merdeka-cyber-sovereignity-by.html

Citation preview

Page 1: Kicking Butt on Concurrent Enterprise Application with Scala

Kicking butt on concurrent enterprise application with

Scala

By Azrul MADISA

Freelance Java [email protected]

Page 2: Kicking Butt on Concurrent Enterprise Application with Scala

Plat du jour

The concurrent enterpriseThe next billion users Asynchronous messagingEnter ScalaScala concurrencyScala actorsScala and Message Driven Beans

Page 3: Kicking Butt on Concurrent Enterprise Application with Scala

The ever concurrent enterprise

Service A

Service B

Service C

Service A

Service B

Service C

Service A

Service B

Service C

Traditional Web N.0 (large # of instances)Modern

Service DService D

Service D

Page 4: Kicking Butt on Concurrent Enterprise Application with Scala

Commerce example

Choose your things

Pay

Choose from things in the

store

Queue up (1 counter in

the shop)

Pay

Choose from things in the

store

Queue up (Many counters

In the shop)

Pay

Inventory

Packaging and delivery

Hypermarkets E-CommerceTraditional

Page 5: Kicking Butt on Concurrent Enterprise Application with Scala

Other sectors

TelcoSwitch board → Exchanges →

NGNGovernment

Counter based → E-GovernmentFinance

Counter → ATM → E-BankingOthers

Millitary, logistics, media & content

Page 6: Kicking Butt on Concurrent Enterprise Application with Scala

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

Page 7: Kicking Butt on Concurrent Enterprise Application with Scala

Don't mean to scare ya but...

People are more network centricEvery 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

Page 8: Kicking Butt on Concurrent Enterprise Application with Scala

One word to rule them all ...

Page 9: Kicking Butt on Concurrent Enterprise Application with Scala

One word to rule them all ...

Scala

bility

Page 10: Kicking Butt on Concurrent Enterprise Application with Scala

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.

Help us fight The Borg

Page 11: Kicking Butt on Concurrent Enterprise Application with Scala

Why asynchronous messaging important

Go to checkout

User choose products

Verifystock

Take payment from user

Inform user

Start

Stock exist?

End

End

E-commerce example:

No

Yes

Page 12: Kicking Butt on Concurrent Enterprise Application with Scala

Why asynchronous messaging important

Verify stockMillions of itemsKicker... need to lock

an item while reserving itTo avoid 2 person

buying the same thing

Lengthy => Clients will run away

Page 13: Kicking Butt on Concurrent Enterprise Application with Scala

Why asynchronous messaging important

Simplified e-commerce

Go to checkout

User choose productsStart

End

Take payment from user

Page 14: Kicking Butt on Concurrent Enterprise Application with Scala

Why asynchronous messaging important

Simplified e-commerce

Go to checkout

User choose products

Start

End

Take payment from user

Verify stock

Packaging +Shipment

End

Fire another process

Stock exist?

No

Email to user – item would be

a bit late

Yes End

Page 15: Kicking Butt on Concurrent Enterprise Application with Scala

Long running transaction

High volume OLTPShort processes

High volume Long runningprocesses ??

We somewhat know how to do this

Page 16: Kicking Butt on Concurrent Enterprise Application with Scala

Long running transaction

Package an item

Client pay

Client sign off

Ship to client's premise

Page 17: Kicking Butt on Concurrent Enterprise Application with Scala

Long running transaction

Package an item

Client pay

Client sign off

Ship to client's premise

Client cancelShip back to

vendor's premise

Reimburse 80% Back to client

Exception!

Page 18: Kicking Butt on Concurrent Enterprise Application with Scala

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 ;)

Page 19: Kicking Butt on Concurrent Enterprise Application with Scala

Why not the other JVM languages

JRuby, Jython , <The dynamic language of the day>Duck typing? No thanks

ClojureInteresting but syntax is too

“lisp-y” - My personal opinion

Plain old JavaAre you kidding me?Image from: http://www.travelblog.org/Europe/Netherlands/North-Holland/Volendam/blog-385862.html

Page 20: Kicking Butt on Concurrent Enterprise Application with Scala

Scala concurrency: Immutable variables

Immutable variablesVariable that cannot change its

valueUseful in concurrent apps

State cannot be inconsistent

Easily declare variables as immutable

The “val” keyword automatically makes variable immutable

val movie = new Movie(“Wolverine”)

val movieThatMakesMePuke = movie

Page 21: Kicking Butt on Concurrent Enterprise Application with Scala

Scala concurrency: Immutable variables

Can Java do this?Java does this on StringFor other object:

Tedious since Java don't have operator overloading

Page 22: Kicking Butt on Concurrent Enterprise Application with Scala

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

Page 23: Kicking Butt on Concurrent Enterprise Application with Scala

Scala concurrency: Easy singleton

Can we do this in JavaSure, but, again, it's tediousYou have to create all the boilerplat

code yourselfRefer to the Gang Of Four book

Page 24: Kicking Butt on Concurrent Enterprise Application with Scala

Scala concurrency: Actors

Scala has actors… and no, they're not as

bad as Paris Hilton

Page 25: Kicking Butt on Concurrent Enterprise Application with Scala

Scala concurrency: Actors

Actors Actors are objectsReceive message asynchronouslyReply with a Future

Future = placeholder of future resultConcurrency without threadingEvent-drivenUnlike real-world actors, Scala actors

areCheapTotally ignored by politicians

Page 26: Kicking Butt on Concurrent Enterprise Application with Scala

Scala concurrency: Actors

Event driven architectureUnlike threads, actors are

event-drivenOnly become alive when

there are messages to be processed.

Execute on a thread poolWith message passing,

number of locks are lowGreat for multicore processors

Page 27: Kicking Butt on Concurrent Enterprise Application with Scala

Scala concurrency: Actors

Go to checkout

User choose products

Start

End

Take payment from user

Verify stock

Packaging +Shipment

End

Fire another process

Stock exist?

No

Email to user – item would be

a bit late

Yes End

MainApp

Actor1

Page 28: Kicking Butt on Concurrent Enterprise Application with Scala

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

}

Page 29: Kicking Butt on Concurrent Enterprise Application with Scala

Scala concurrency: Actors

Can we do this in JavaIn plain old Java, thread can be usedThread can not be created in a Java

EE containerHave to use JMS

OpenMQActiveMQMessage Driven BeansMessage Driven Pojo with Spring

Page 30: Kicking Butt on Concurrent Enterprise Application with Scala

Scala concurrency: Reliability

Short-comings of Scala messagesThey do not live in a transaction

They cannot travel through a networkThere are projects to address this

short comingScala OTP (Equivalent to Erlang OTP)ActiveObject

Another approach is to combine Scala and Java EE

Page 31: Kicking Butt on Concurrent Enterprise Application with Scala

Combining actors and message driven beans

`

Scala Actors Message driven bean

Page 32: Kicking Butt on Concurrent Enterprise Application with Scala

Combining actors and message driven beans

`

Scala Actors Message driven bean

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

WARNING: Star T

rek related

analogy is being used!

If you're

not a geek, d

uck!

Page 33: Kicking Butt on Concurrent Enterprise Application with Scala

Combining actors and message driven beans

WormholeActor

Actor javax.jms.MessageListener

Spring's Message Driven Pojo

JMS worldScala world

Request Queue

MDB

Scala object

Response Queue

Page 34: Kicking Butt on Concurrent Enterprise Application with Scala

Performance

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

Page 35: Kicking Butt on Concurrent Enterprise Application with Scala

Tools

Netbeans, Eclipse and IDEA plugin

Page 36: Kicking Butt on Concurrent Enterprise Application with Scala

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

Page 37: Kicking Butt on Concurrent Enterprise Application with Scala

Other interesting stuff

LiftScala web framework based on

actors

Scala books

Testing ScalaTest

Page 38: Kicking Butt on Concurrent Enterprise Application with Scala

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 … )

Page 39: Kicking Butt on Concurrent Enterprise Application with Scala

End note

Scala is a sharp weapon to use in the coming “highly concurrent” age

Hope to get you excited over Scala Play with itTest itUse itTwitt/blog about it

Get something moving in Malaysia

Visit my blog ejn3.blogspot.com

My contact: [email protected]