59
Speedy perception trumps speedy reception – Smart Asynchronous Interactions Lucas Jellema (AMIS) 5th November 2014

Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

  • Upload
    nljug

  • View
    198

  • Download
    4

Embed Size (px)

Citation preview

Page 1: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

Speedy perception trumps speedy reception –

Smart Asynchronous Interactions

Lucas Jellema (AMIS)

5th November 2014

Page 2: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

Key Take Aways

• Performance (perceived)

• Scalability

• Availability

• Security

• Flexibility (design time)

Page 3: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

Conclusions (to be substantiated)

• Asynch improves [perception of] performance– And availability

• Asynch lowers load on resources and enhances security• Asynch decouples logically• Asynch consists of three steps

– Call and hang-up (free up calling thread)– Process in the background– Return the response (using push and/or

poll & correlation) – to the original tier (& session | client | thread)

• Typical Java (EE) mechanisms: JMS, CDI Events, Executor, WebSocket/SSE, (Job) Scheduler

Page 4: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

Terms and Technology

• Multi Tier architecture, Event, Transaction, Pub/Sub, queues, job scheduling, non blocking

• thread | session | client

• HTML(5), JavaScript, AJAX, JSON

• WebSocket, Servlet, CDI (Events)

• EJB (timer), MDB, JMS

• JDBC, Database Job

Page 5: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

Who said this?

• I sneezed and the sun set…

• God

• Superman

• Larry Ellison

• Henk

Page 6: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

Confusion

• Between

– Correlation

– Causality

Page 7: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

We [human brains] think

• Synchronously

– Sequentially Ordered

– Looking for the narrative

Page 8: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

When it is not synchronous..

• Out of the blue

• Act of God

• Deus ex Machina

Page 9: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

When and Why Synchronous?

In real life, when you tell something to a person, you don't mind waiting for answer (beyond some form of acknowledgement that your message was heard and understood) if and when:• You asked a question that you want an answer to• You cannot meaningfully continue with your life until

you receive the response• You do not have to wait too long (a little subjective)• You will lose the opportunity to receive

the answer at all if you do not wait for it• You can afford to wait (because of the time

or cost of resources involved with waiting)

Page 10: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

Asynchronous… in real life

• “I will get back to you”

• Daily newspaper delivery

• DHL delivery of our Amazon order of last Friday

• Death Notification of very old uncle

• Send application letter & CV

• TV presenter talking into camera

• That pretty guy or girl in the barwho said (s)he is surely goingto call you…

Page 11: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

I wonder: if I were to ask a rethorical question

on asynchronous conversations, would I receive answer?

Page 12: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

Why Asynchronous?

• You have better things to do than wait for a reply

• You do not need the reply (any time soon)

• The ‘other side’ cannot spare you the time or resources to continue to wait

• You trust the reply will make itsway to you

Page 13: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

In IT: Why Synchronous all the time?

• Easy – it is usually the default

• How to do asynchronous processing – How to hang up the phone?

– How to return faults in asynchronous cases?

• When asynch – how to establish communication (howdoes invokee find right point of contact in caller)– What to do when not found?

– Responsibility has shifted(and probably rightly so)

• Note: Asynchronous “Event Storms” can overwhelm systems

Page 14: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

Consumption of scarce resources has to be done very carefully

• Threads are expensive and scarce– It is a waste to have them blocked – waiting and

doing nothing while tying up physical resources

– Many jobs, connections and conversations have tobe handled by limited number of threads

• Other scarce resources– JDBC Connections

– HTTP Browser-Server Connections

– Memory, CPU, Battery

Page 15: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

Some things have changedover the years

• Multi core => parallellism• JavaScript – single threaded – you have to use asynch!• Better facilities in programming languages and platforms

– Java Future– HTML5 workers– SSE/WebSockets– Java EE CDI, Asynch EJB, JMS– Database HTTP Call-out & Query Result

Change Notifcation

• Understanding of user experiencerequirements– I am working on it can be just as good

as it is done – compare ordering in a web shop

Page 16: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

Wait for… what exactly?

Web Application ESB

Document Management

ProcessEngine

Page 17: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

One way (fire and forget) with queue for save keeping

Web Application ESB

Document Management

ProcessEngine

Q

Page 18: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

First wait… then potentially fail(availability chain)

Portal ESB

ERP system

Page 19: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

Asynchronous (background) processing resolves issue

Portal ESB

ERP system

Background process

Page 20: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

Wait with so many consumers(resource depletion)

Web ShopBusiness Tier

(EJB)

Page 21: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

Background Database Jobs and/or Front-End/Write Behind Cache

Web ShopBusiness Tier

(EJB)

Cache

jobs

Page 22: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

Web ApplicationClient (Browser)

Middle Tier

Database

Page 23: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

RestaurantDinner Table

Waiting Staff

Kitchen

Page 24: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

The Synchronous Restaurant

• Hold your breath (and wait)once the order has been placed

• The waiter is exclusivelyassigned to the table and the order

• The waiter goes to the kitchen,hands over the meal order and…waits until the meal items are complete

• Note: one cook works only on a single table’s meal order

• The waiter returns to the table withthe meal – and the guests can exhale

Page 25: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

Synchronous Restaurant as Web AppClient (Browser)

Middle Tier

Database

HTML

Servlet

EJB

TableStored

Procedure

Form Post

Synch Call

Synch CallTransaction Boundary

Synch Call

Page 26: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

screenshot

Page 27: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

Turning the restaurant into an asynchronous place

• Guests can continue conversation after placingtheir order

• Waiters can continue waiting on other tablesonce they have handed off the order– And prepared and delivered drinks

• Cooks can work on multiple meal orders• Meal items can be delivered when prepared

– Appetizers ahead of main course

• Fewer resources are needed, while better service is provided

Page 28: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

Browser

Java Middle Tier

Foreground(User Interface)

Background(blocking UI)

WebWorker(non blocking)

timer

Worker

SynchronousHTTP GET/POST

AsynchronousXMLHTTPRequest

AsynchronousXMLHTTPRequest

Page 29: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

Web Client makes Asynchronous Call –Callback function updates UI

Client (Browser)

Middle Tier

HTML

Servlet

AJAX Call

Lots of SynchronousProcessing

Page 30: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema
Page 31: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

Web Tier makes Asynchronous CallClient (Browser)

Middle Tier

HTML

Servlet

AJAX Call

MDBAsynch

EJB

JMS

Page 32: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

How do you end the synchronous part of a conversation in Java

• Hand off to Executor– Run on a different thread

• Publish to JMS– Consume on different thread

• Invoke Asynchronous EJB– Execute on a different thread

• Start a Java Batch Job• Database Commit• Send Web Socket message

– Send email, write file, put on grid

• Invoke asynchronous Web Service• Note: not CDI events

Page 33: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

Browser

Java Middle Tier

HTTP GET/POST

executor

QuartzScheduler

WorkManager

JMS MDB

MDB

EJB Timer

Java Batch (Java EE 7)

jobJobOperator

EJB

One Way WebService

grid

CDI Bean(Java EE 7) CDI events

vert.x

Page 34: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

Business Tier processing is AsynchronousMiddle Tier

Servlet

MDB EJB

JMS

Database

Table

StoredProcedure

Synch CallSchedule

Job

Page 35: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

Java Middle Tier

WorkManager

JMS MDB

grid

MDB

One Way WebService

DatabaseJDBC HTTP

Emb

edd

ed

PL/SQ

L Ga

tewa

y

AQ

Job Scheduler

FTP/WEBDAV

XML DB

XML DB Repository

events Event Handler

Email

Stored Java (poll job)

File

File Watcher

Na

tive Da

tab

ase

Web

Services

tableasynchcommit

pipe

Global appcontext

Page 36: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

Synchronous Meal Ordering

Middle TierServlet

EJB

Database

Table

Synch CallSchedule

Job

Client (Browser)

HTML

AJAX Call

Page 37: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

Schedule Database Job To Cook

Table

Synch CallSchedule

Job

Page 38: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

Cook Job to ExecuteTable

StoredProcedure

Page 39: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema
Page 40: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

Asynchronous Response up the tiers

Page 41: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

Returning the Asynchronous Response

• Push when ready– Contact the requesting

tier• Against the grain

– Deliver to the requestingcomponent• Which session, thread, client

• Poll periodically until the response is available– Get hold of the right response – outside

the scope of the original conversation

Page 42: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

Business Tier processing is AsynchronousMiddle Tier

Timer EJB

JMS

Database

Table

StoredProcedure

poll for ready meal items

Page 43: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

Pushing & Polling – DB => Java EE

Java Middle Tier

JMS

MDB

MDB

DatabaseJDBC HTTP

AQ

StoredProcedure

FTP/WEBDAV

XML DB Repository

Email

Mail sending

File

File Writer

table

pipe

Global appcontext

HTTP Caller

DB

Qu

ery Resu

ltC

ha

ng

e No

tificatio

n

po

lling

threa

d

table

TCP/IP

Socket

servlet

AsynchronousWeb Service Response

HTTP

WebSocket

TCP

Web SocketEndpoint

Page 44: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

Correlation: “I am calling back regarding…”

• Correlation

– Response contains an identifier that is known tothe polling thread

– or that can be used to connect to the originalcaller (session, client, thread)

Page 45: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

Asynchronously delivered meal item comes with table number

Middle Tier

Timer EJB

JMS

Database

Table

StoredProcedure

poll for ready meal items

Page 46: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

Timer EJB and Polling Kitchen

Timer EJB

poll for ready meal items

Page 47: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

Timer EJB, Polling Kitchen andPublishing Meal Items

Timer EJB

JMS

poll for ready meal items

Page 48: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

Java Middle Tier

LongPoll

Repeated/Long Poll

Web Socket

Web SocketEndpoint

ServerSent Event

Cache Event Handler

HTTP Session

listeners

JMS

MDBEJB

PiggyBack

Page 49: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

A waiter delivers the meal itemsClient (Browser)

Middle Tier

HTML

WebSocketServer

WebSockethandler

MDBJMS

CDI Event

Page 50: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

Consume JMS and Publish CDI EventsMiddle Tier

WebSocketServer

MDBJMS

CDI Event

Page 51: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

Initialization of WebSocketClient (Browser)

Middle Tier

HTML

WebSocketServer

WebSockethandler

Page 52: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

Send Meal Item from CDI Event toWeb Socket channel for table

Client (Browser)

Middle Tier

HTML

WebSocketServer

WebSockethandler

CDI Event

Page 53: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

Demo of Asynchronous Restaurant

Page 54: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

Demo of Asynchronous Restaurant

Page 55: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

Demo of Asynchronous Restaurant

Page 56: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

Demo of Asynchronous Restaurant

Page 57: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

Cut off points, thread jumps, ends-of-conversations, asynchronization points

Middle Tier

Database

Client (Browser)

AJAX

JMS

WebSocket

Job

JMS

WebSocket

CDIEvent

Page 58: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

Conclusions

• Asynch improves [perception of] performance– And availability

• Asynch lowers load on resources & enhances security• Asynch decouples logically• Asynch consists of three steps

– Call and hang-up (free up calling thread)– Process in the background– Return the response (using push and/or

poll & correlation) – to the original tier (& session | client | thread)

• Typical Java (EE) mechanisms: JMS, CDI Events, Executor, WebSocket/SSE, (Job) Scheduler

Page 59: Speedy perception trumps speedy reception–smart asynchronous interactions - Lucas Jellema

Code on GitHub:https://github.com/lucasjellema/DinnerAtSpeedyJoes

Slide on Slideshare and NLJUG websitehttp://www.slideshare.net/lucasjellema/speedy-perception-trumps-

speedy-receptionsmart-asynchronous-interactions-j-fall2014lucasjellema

Contact me:lucas.jellema @ amis.nl

Blog: http://technology.amis.nl