#113 Event Driven Architecture

- added class diagram
- added more comments
This commit is contained in:
chris 2015-11-23 11:20:20 +01:00
parent b4aeca3aa0
commit 321e9d4191
22 changed files with 123 additions and 158 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB

View File

@ -16,7 +16,7 @@ permalink: /patterns/event-driven-architecture
**Real world examples:**
* A Loan Application has been accepted/rejected (commercial business).
* A Loan Application has been accepted/rejected (Commercial Business).
* A new Rostering Schedule is ready for distribution to all crew (Airline Management System).
* An Illegal Trade Pattern has been detected (Trading Fraud Detection System).
* A simulated car has hits another simulated car (Commercial Racing Game).
@ -28,4 +28,6 @@ permalink: /patterns/event-driven-architecture
* [Event-driven architecture - Wikipedia](http://www.computerweekly.com/feature/Write-through-write-around-write-back-Cache-explained)
* [Fundamental Components of an Event-Driven Architecture](http://giocc.com/fundamental-components-of-an-event-driven-architecture.html)
* [Real World Applications/Event Driven Applications](https://wiki.haskell.org/Real_World_Applications/Event_Driven_Applications)
* [Real World Applications/Event Driven Applications](https://wiki.haskell.org/Real_World_Applications/Event_Driven_Applications)
* [Event-driven architecture definition](http://searchsoa.techtarget.com/definition/event-driven-architecture)
*

View File

@ -0,0 +1,28 @@
package com.iluwatar.eda;
import com.iluwatar.eda.event.Event;
import com.iluwatar.eda.event.UserCreatedEvent;
import com.iluwatar.eda.event.UserUpdatedEvent;
import com.iluwatar.eda.handler.UserCreatedEventHandler;
import com.iluwatar.eda.handler.UserUpdatedEventHandler;
/**
* An event-driven architecture (EDA) is a framework that orchestrates behavior around the production,
* detection and consumption of events as well as the responses they evoke.
* An event is any identifiable occurrence that has significance for system hardware or software.
* <p/>
* The example below we uses an {@link EventDispatcher} to link/register {@link Event} objects to
* their respective handlers Once an {@link Event} is dispatched,
* it's respective handler is invoked and the {@link Event} is handled accordingly
*/
public class App {
public static void main(String[] args) {
EventDispatcher dispatcher = new EventDispatcher();
dispatcher.registerChannel(UserCreatedEvent.class, new UserCreatedEventHandler());
dispatcher.registerChannel(UserUpdatedEvent.class, new UserUpdatedEventHandler());
dispatcher.dispatch(new UserCreatedEvent());
dispatcher.dispatch(new UserUpdatedEvent());
}
}

View File

@ -0,0 +1,39 @@
package com.iluwatar.eda;
import com.iluwatar.eda.event.Event;
import com.iluwatar.eda.framework.Channel;
import com.iluwatar.eda.framework.DynamicRouter;
import java.util.HashMap;
import java.util.Map;
/**
* The {@link Event Dispatcher} handles routing of {@link Event} messages to associated channels.
* A {@link HashMap} is used to store the association between events and their respective handlers.
*/
public class EventDispatcher implements DynamicRouter<Event> {
private Map<Class<? extends Event>, Channel> handlers;
public EventDispatcher() {
handlers = new HashMap<Class<? extends Event>, Channel>();
}
/**
* Links an {@link Event} to a specific {@link Channel}
* @param contentType The {@link Event} to be registered
* @param channel The {@link Channel} that will be handling the {@link Event}
*/
public void registerChannel(Class<? extends Event> contentType,
Channel channel) {
handlers.put(contentType, channel);
}
/**
* Dispathes an {@link Event} depending on it's type.
* @param content The {@link Event} to be dispatched
*/
public void dispatch(Event content) {
handlers.get(content.getClass()).dispatch(content);
}
}

View File

@ -1,18 +0,0 @@
package com.iluwatar.eda.advanced;
import com.iluwatar.eda.advanced.events.Event;
import com.iluwatar.eda.advanced.events.UserCreatedEvent;
import com.iluwatar.eda.advanced.events.UserUpdatedEvent;
import com.iluwatar.eda.advanced.handler.UserCreatedEventHandler;
import com.iluwatar.eda.advanced.handler.UserUpdatedEventHandler;
public class App {
public static void main(String[] args) {
EventDispatcher dispatcher = new EventDispatcher();
dispatcher.registerChannel(UserCreatedEvent.class, new UserCreatedEventHandler());
dispatcher.registerChannel(UserUpdatedEvent.class, new UserUpdatedEventHandler());
dispatcher.dispatch(new UserCreatedEvent());
dispatcher.dispatch(new UserUpdatedEvent());
}
}

View File

@ -1,26 +0,0 @@
package com.iluwatar.eda.advanced;
import com.iluwatar.eda.advanced.events.Event;
import com.iluwatar.eda.advanced.framework.Channel;
import com.iluwatar.eda.advanced.framework.DynamicRouter;
import java.util.HashMap;
import java.util.Map;
public class EventDispatcher implements DynamicRouter<Event> {
private Map<Class<? extends Event>, Channel> handlers;
public EventDispatcher() {
handlers = new HashMap<Class<? extends Event>, Channel>();
}
public void registerChannel(Class<? extends Event> contentType,
Channel channel) {
handlers.put(contentType, channel);
}
public void dispatch(Event content) {
handlers.get(content.getClass()).dispatch(content);
}
}

View File

@ -1,9 +0,0 @@
package com.iluwatar.eda.advanced.events;
import com.iluwatar.eda.advanced.events.Event;
/**
* @author cfarrugia
*/
public class UserCreatedEvent extends Event {
}

View File

@ -1,6 +0,0 @@
package com.iluwatar.eda.advanced.events;
import com.iluwatar.eda.advanced.events.Event;
public class UserUpdatedEvent extends Event {
}

View File

@ -1,8 +0,0 @@
package com.iluwatar.eda.advanced.framework;
import com.iluwatar.eda.advanced.events.Event;
public interface Channel<E extends Event> {
void dispatch(E message);
}

View File

@ -1,6 +0,0 @@
package com.iluwatar.eda.advanced.framework;
public interface DynamicRouter<E extends Message> {
void registerChannel(Class<? extends E> contentType, Channel channel);
void dispatch(E content);
}

View File

@ -1,6 +0,0 @@
package com.iluwatar.eda.advanced.framework;
public interface Message {
public Class<? extends Message> getType();
}

View File

@ -1,6 +1,6 @@
package com.iluwatar.eda.advanced.events;
package com.iluwatar.eda.event;
import com.iluwatar.eda.advanced.framework.Message;
import com.iluwatar.eda.framework.Message;
public class Event implements Message {
public Class<? extends Message> getType() {

View File

@ -0,0 +1,7 @@
package com.iluwatar.eda.event;
/**
* @author cfarrugia
*/
public class UserCreatedEvent extends Event {
}

View File

@ -0,0 +1,4 @@
package com.iluwatar.eda.event;
public class UserUpdatedEvent extends Event {
}

View File

@ -0,0 +1,9 @@
package com.iluwatar.eda.framework;
/**
* Channels are delivery points for messages.
* Every {@link Channel} is responsible for a single type of message
*/
public interface Channel<E extends Message> {
void dispatch(E message);
}

View File

@ -0,0 +1,11 @@
package com.iluwatar.eda.framework;
/**
* A {@link DynamicRouter} is responsible for selecting the proper path of a {@link Message}
* Messages can be associated to Channels through the registerChannel method and dispatched by calling
* the dispatch method.
*/
public interface DynamicRouter<E extends Message> {
void registerChannel(Class<? extends E> contentType, Channel channel);
void dispatch(E content);
}

View File

@ -0,0 +1,9 @@
package com.iluwatar.eda.framework;
/**
* A {@link Message} is an object with a specific type that is associated to a
* specific {@link Channel}
*/
public interface Message {
Class<? extends Message> getType();
}

View File

@ -1,10 +1,10 @@
package com.iluwatar.eda.advanced.handler;
package com.iluwatar.eda.handler;
import com.iluwatar.eda.advanced.events.UserCreatedEvent;
import com.iluwatar.eda.advanced.framework.Channel;
import com.iluwatar.eda.event.UserCreatedEvent;
import com.iluwatar.eda.framework.Channel;
/**
* @author cfarrugia
* Handles the {@link UserCreatedEvent} message
*/
public class UserCreatedEventHandler implements Channel<UserCreatedEvent> {

View File

@ -1,8 +1,11 @@
package com.iluwatar.eda.advanced.handler;
package com.iluwatar.eda.handler;
import com.iluwatar.eda.advanced.events.UserUpdatedEvent;
import com.iluwatar.eda.advanced.framework.Channel;
import com.iluwatar.eda.event.UserUpdatedEvent;
import com.iluwatar.eda.framework.Channel;
/**
* Handles the {@link UserUpdatedEvent} message
*/
public class UserUpdatedEventHandler implements Channel<UserUpdatedEvent> {
public void dispatch(UserUpdatedEvent message) {

View File

@ -1,38 +0,0 @@
package com.iluwatar.eda.simple;
import java.util.LinkedList;
import java.util.Queue;
/**
* Event-driven architecture (EDA) is a software architecture pattern promoting
* the production, detection, consumption of, and reaction to events.
* <p/>
* This main class publishes events to queue. Each event on the queue is consumed
* and handled depending on the type defined in the {@link Event}
*/
public class App {
public static void main(String args[]) {
//create a list of events having different types
//add these events to a simple queue
Queue<Event> events = new LinkedList<Event>();
events.add(new Event('A', "Hello"));
events.add(new Event('B', "event-driven"));
events.add(new Event('A', "world!"));
Event e;
//the event loop will go through the list of events
//and handle each one depending on it's type
while (!events.isEmpty()) {
e = events.remove();
//handle events depending on their type
if (e.type == 'A')
EventHandler.handleEventA(e);
if (e.type == 'B')
EventHandler.handleEventB(e);
}
}
}

View File

@ -1,15 +0,0 @@
package com.iluwatar.eda.simple;
/**
* The Event class defines the type of event and data related to the event.
*/
public class Event {
public char type;
public String data;
public Event(char type, String data){
this.type = type;
this.data = data;
}
}

View File

@ -1,15 +0,0 @@
package com.iluwatar.eda.simple;
/**
* The {@link EventHandler} class handles performs actions on {@link Event} objects
*/
public class EventHandler {
public static void handleEventA(Event e){
System.out.println(e.data);
}
public static void handleEventB(Event e){
System.out.println(e.data.toUpperCase());
}
}