Skip to content

Commit

Permalink
#113 Event Driven Architecture
Browse files Browse the repository at this point in the history
- added class diagram
- added more comments
  • Loading branch information
chris committed Nov 23, 2015
1 parent b4aeca3 commit 321e9d4
Show file tree
Hide file tree
Showing 22 changed files with 123 additions and 158 deletions.
Binary file added event-driven-architecture/etc/class_diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 4 additions & 2 deletions event-driven-architecture/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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)
*
28 changes: 28 additions & 0 deletions event-driven-architecture/src/main/java/com/iluwatar/eda/App.java
Original file line number Diff line number Diff line change
@@ -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());
}

}
Original file line number Diff line number Diff line change
@@ -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);
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.iluwatar.eda.event;

/**
* @author cfarrugia
*/
public class UserCreatedEvent extends Event {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.iluwatar.eda.event;

public class UserUpdatedEvent extends Event {
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
@@ -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> {

Expand Down
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 321e9d4

Please sign in to comment.