Documente Academic
Documente Profesional
Documente Cultură
Event-driven
Architectures
Tom Van Cutsem
Overview
Event-driven Architectures
2
2006 Tom Van Cutsem - Programming Technology Lab
return return
fire event
3
2006 Tom Van Cutsem - Programming Technology Lab
Event-driven Model
4
2006 Tom Van Cutsem - Programming Technology Lab
Event-driven Model
Events
4
2006 Tom Van Cutsem - Programming Technology Lab
Event-driven Model
Event Queue
Events
4
2006 Tom Van Cutsem - Programming Technology Lab
Event-driven Model
Event Queue
Events
Event Loop
while (true) {
Event e = eventQueue.next();
switch (e.type) {
...
}
}
4
2006 Tom Van Cutsem - Programming Technology Lab
Event-driven Model
Event Queue
Events
void onKeyPressed(KeyEvent e) {
// process the event
}
4
2006 Tom Van Cutsem - Programming Technology Lab
Examples
GUI Frameworks (e.g. Java AWT)
Operating Systems
5
2006 Tom Van Cutsem - Programming Technology Lab
Event-loop Concurrency
6
2006 Tom Van Cutsem - Programming Technology Lab
Event-loop Concurrency
6
2006 Tom Van Cutsem - Programming Technology Lab
Event-loop Concurrency
6
2006 Tom Van Cutsem - Programming Technology Lab
Event-loop Concurrency
6
2006 Tom Van Cutsem - Programming Technology Lab
Event-loop Concurrency
No locks, no deadlocks
6
2006 Tom Van Cutsem - Programming Technology Lab
Event-driven
Programming
2006 Tom Van Cutsem - Programming Technology Lab
Return values
DeliveryService CustomerService
requestAddress
shipToRequest
void processDelivery(Order o) {
// request customers address
Address a = customerService.requestAddress(o.customerId));
courier.shipToRequest(o, a);
}
8
2006 Tom Van Cutsem - Programming Technology Lab
Callbacks
Dealing with asynchronous return values
void processDelivery(Order o) {
// store order to retrieve it later
DeliveryService CustomerService orders.add(o);
// request customers address
customerService.receive(
requestAddress new RequestAddress(o.orderId, o.customerId));
}
replyAddress
shipToRequest
9
2006 Tom Van Cutsem - Programming Technology Lab
Callbacks
Dealing with asynchronous return values
void processDelivery(Order o) {
// store order to retrieve it later
DeliveryService CustomerService orders.add(o);
// request customers address
customerService.receive(
requestAddress new RequestAddress(o.orderId, o.customerId));
}
9
2006 Tom Van Cutsem - Programming Technology Lab
10
2006 Tom Van Cutsem - Programming Technology Lab
Futures
Placeholders for asynchronous return values
DeliveryService CustomerService
void processDelivery(Order o) {
Future addressFuture = customerService.receive(
f := requestAddress
new RequestAddress(o.customerId));
// do things that dont require address
f.get()
f.resolve(address) Address adr = (Address) addressFuture.get();
courier.receive(new ShipToRequest(o, adr));
}
shipToRequest
11
2006 Tom Van Cutsem - Programming Technology Lab
Asynchronous Futures
Subscription of listeners that are executed
when return value is available
12
2006 Tom Van Cutsem - Programming Technology Lab
Event-driven
Architecture
2006 Tom Van Cutsem - Programming Technology Lab
Event-driven Architecture
A program is composed of services
orderProcessed orderPayed
Billing Delivery
Service Service
14
2006 Tom Van Cutsem - Programming Technology Lab
Channels
Point-to-point: fixed endpoints
Example: Model-View-Controller
15
2006 Tom Van Cutsem - Programming Technology Lab
Channels
Point-to-point: fixed endpoints
Example: Model-View-Controller
Model
Votes
yes 41
no 44
abstain 15
15
2006 Tom Van Cutsem - Programming Technology Lab
Channels
Point-to-point: fixed endpoints
Example: Model-View-Controller
Model Views
45
30
Votes 15
0
yes 41 15%
no 44 41%
44%
abstain 15
0 33 67 100
15
2006 Tom Van Cutsem - Programming Technology Lab
Channels
Point-to-point: fixed endpoints
Example: Model-View-Controller
Model Views
45
subscribe 30
Votes 15
0
yes 41 15%
no 44 41%
44%
abstain 15
0 33 67 100
15
2006 Tom Van Cutsem - Programming Technology Lab
Channels
Point-to-point: fixed endpoints
Example: Model-View-Controller
Model Views
45
30
Votes 15
0
yes 41 15%
no 44 41%
44%
abstain 15
publish events
0 33 67 100
15
2006 Tom Van Cutsem - Programming Technology Lab
Composing Services
Service Repository
Root
Topic hierarchy:
Orders Payment
abstraction
16
2006 Tom Van Cutsem - Programming Technology Lab
EDA: Benefits
Services are highly reusable
Service
Old Replacement
Service Service
synchronize
retire
17
2006 Tom Van Cutsem - Programming Technology Lab
EDA: Benefits
Unit Testing: testing services in isolation
Provider Client
Service Service
Service
under test
Mock-up
Test Driver
service
18
2006 Tom Van Cutsem - Programming Technology Lab
EDA: Benefits
Temporal decoupling:
19
2006 Tom Van Cutsem - Programming Technology Lab
EDA: Benefits
Adaptor services easily introduced:
logging events
authenticating events
...
20
2006 Tom Van Cutsem - Programming Technology Lab
EDA: Drawbacks
21
2006 Tom Van Cutsem - Programming Technology Lab
EDA: Drawbacks
22
2006 Tom Van Cutsem - Programming Technology Lab
Failure Handling
Pessimistic synchronization (e.g. 2PC protocol)
Conclusions
Event-driven programming = programming
without a call stack
24
2006 Tom Van Cutsem - Programming Technology Lab
References
Enterprise Integration Patterns The Power of Events
Gregor Hohpe and Bobby Woolf David Luckham
Addison-Wesley Addison-Wesley