Skip to content

Commit 321e9d4

Browse files
author
chris
committed
iluwatar#113 Event Driven Architecture
- added class diagram - added more comments
1 parent b4aeca3 commit 321e9d4

22 files changed

Lines changed: 123 additions & 158 deletions
37.3 KB
Loading

event-driven-architecture/index.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ permalink: /patterns/event-driven-architecture
1616

1717
**Real world examples:**
1818

19-
* A Loan Application has been accepted/rejected (commercial business).
19+
* A Loan Application has been accepted/rejected (Commercial Business).
2020
* A new Rostering Schedule is ready for distribution to all crew (Airline Management System).
2121
* An Illegal Trade Pattern has been detected (Trading Fraud Detection System).
2222
* A simulated car has hits another simulated car (Commercial Racing Game).
@@ -28,4 +28,6 @@ permalink: /patterns/event-driven-architecture
2828

2929
* [Event-driven architecture - Wikipedia](http://www.computerweekly.com/feature/Write-through-write-around-write-back-Cache-explained)
3030
* [Fundamental Components of an Event-Driven Architecture](http://giocc.com/fundamental-components-of-an-event-driven-architecture.html)
31-
* [Real World Applications/Event Driven Applications](https://wiki.haskell.org/Real_World_Applications/Event_Driven_Applications)
31+
* [Real World Applications/Event Driven Applications](https://wiki.haskell.org/Real_World_Applications/Event_Driven_Applications)
32+
* [Event-driven architecture definition](http://searchsoa.techtarget.com/definition/event-driven-architecture)
33+
*
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.iluwatar.eda;
2+
3+
import com.iluwatar.eda.event.Event;
4+
import com.iluwatar.eda.event.UserCreatedEvent;
5+
import com.iluwatar.eda.event.UserUpdatedEvent;
6+
import com.iluwatar.eda.handler.UserCreatedEventHandler;
7+
import com.iluwatar.eda.handler.UserUpdatedEventHandler;
8+
9+
/**
10+
* An event-driven architecture (EDA) is a framework that orchestrates behavior around the production,
11+
* detection and consumption of events as well as the responses they evoke.
12+
* An event is any identifiable occurrence that has significance for system hardware or software.
13+
* <p/>
14+
* The example below we uses an {@link EventDispatcher} to link/register {@link Event} objects to
15+
* their respective handlers Once an {@link Event} is dispatched,
16+
* it's respective handler is invoked and the {@link Event} is handled accordingly
17+
*/
18+
public class App {
19+
20+
public static void main(String[] args) {
21+
EventDispatcher dispatcher = new EventDispatcher();
22+
dispatcher.registerChannel(UserCreatedEvent.class, new UserCreatedEventHandler());
23+
dispatcher.registerChannel(UserUpdatedEvent.class, new UserUpdatedEventHandler());
24+
dispatcher.dispatch(new UserCreatedEvent());
25+
dispatcher.dispatch(new UserUpdatedEvent());
26+
}
27+
28+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.iluwatar.eda;
2+
3+
import com.iluwatar.eda.event.Event;
4+
import com.iluwatar.eda.framework.Channel;
5+
import com.iluwatar.eda.framework.DynamicRouter;
6+
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
10+
/**
11+
* The {@link Event Dispatcher} handles routing of {@link Event} messages to associated channels.
12+
* A {@link HashMap} is used to store the association between events and their respective handlers.
13+
*/
14+
public class EventDispatcher implements DynamicRouter<Event> {
15+
16+
private Map<Class<? extends Event>, Channel> handlers;
17+
18+
public EventDispatcher() {
19+
handlers = new HashMap<Class<? extends Event>, Channel>();
20+
}
21+
22+
/**
23+
* Links an {@link Event} to a specific {@link Channel}
24+
* @param contentType The {@link Event} to be registered
25+
* @param channel The {@link Channel} that will be handling the {@link Event}
26+
*/
27+
public void registerChannel(Class<? extends Event> contentType,
28+
Channel channel) {
29+
handlers.put(contentType, channel);
30+
}
31+
32+
/**
33+
* Dispathes an {@link Event} depending on it's type.
34+
* @param content The {@link Event} to be dispatched
35+
*/
36+
public void dispatch(Event content) {
37+
handlers.get(content.getClass()).dispatch(content);
38+
}
39+
}

event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/App.java

Lines changed: 0 additions & 18 deletions
This file was deleted.

event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/EventDispatcher.java

Lines changed: 0 additions & 26 deletions
This file was deleted.

event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/events/UserCreatedEvent.java

Lines changed: 0 additions & 9 deletions
This file was deleted.

event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/events/UserUpdatedEvent.java

Lines changed: 0 additions & 6 deletions
This file was deleted.

event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/framework/Channel.java

Lines changed: 0 additions & 8 deletions
This file was deleted.

event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/framework/DynamicRouter.java

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)