kicking butt on concurrent enterprise application with scala by azrul hasni madisa

Post on 11-Jun-2015

463 Views

Category:

Documents

3 Downloads

Preview:

Click to see full reader

DESCRIPTION

Kicking Butt on Concurrent Enterprise Application With Scala By Azrul Hasni MadisaMSC Malaysia Open Source Conference 2009

TRANSCRIPT

Kicking butt on concurrent enterprise application with

Scala

By Azrul MADISA

Freelance Java Developerazrulhasni@gmail.com

Plat du jour

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

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

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

Other sectors

TelcoSwitch board → Exchanges →

NGNGovernment

Counter based → E-GovernmentFinance

Counter → ATM → E-BankingOthers

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

One word to rule them all ...

One word to rule them all ...

Scala

bility

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

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

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

Why asynchronous messaging important

Simplified e-commerce

Go to checkout

User choose productsStart

End

Take payment from user

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

Long running transaction

High volume OLTPShort processes

High volume Long runningprocesses ??

We somewhat know how to do this

Long running transaction

Package an item

Client pay

Client sign off

Ship to client's premise

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!

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

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

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

Scala concurrency: Immutable variables

Can Java do this?Java does this on StringFor 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 JavaSure, but, again, it's tediousYou have to create all the boilerplat

code yourselfRefer 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 objectsReceive message asynchronouslyReply with a Future

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

areCheapTotally ignored by politicians

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

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

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

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

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

WARNING: Star T

rek related

analogy is being used!

If you're

not a geek, d

uck!

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

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

LiftScala 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 itTest itUse itTwitt/blog about it

Get something moving in Malaysia

Visit my blog ejn3.blogspot.com

My contact: azrulhasni@gmail.com

top related