diff --git a/event-driven-architecture/etc/class_diagram.png b/event-driven-architecture/etc/class_diagram.png new file mode 100644 index 000000000..69560e1f4 Binary files /dev/null and b/event-driven-architecture/etc/class_diagram.png differ diff --git a/event-driven-architecture/index.md b/event-driven-architecture/index.md index d5b501c3a..fb438c3e9 100644 --- a/event-driven-architecture/index.md +++ b/event-driven-architecture/index.md @@ -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) \ No newline at end of file +* [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) +* \ No newline at end of file diff --git a/event-driven-architecture/src/main/java/com/iluwatar/eda/App.java b/event-driven-architecture/src/main/java/com/iluwatar/eda/App.java new file mode 100644 index 000000000..e8b8d2d09 --- /dev/null +++ b/event-driven-architecture/src/main/java/com/iluwatar/eda/App.java @@ -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. + *

+ * 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()); + } + +} diff --git a/event-driven-architecture/src/main/java/com/iluwatar/eda/EventDispatcher.java b/event-driven-architecture/src/main/java/com/iluwatar/eda/EventDispatcher.java new file mode 100644 index 000000000..f947773d0 --- /dev/null +++ b/event-driven-architecture/src/main/java/com/iluwatar/eda/EventDispatcher.java @@ -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 { + + private Map, Channel> handlers; + + public EventDispatcher() { + handlers = new HashMap, 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 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); + } +} \ No newline at end of file diff --git a/event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/App.java b/event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/App.java deleted file mode 100644 index e7b8c01bc..000000000 --- a/event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/App.java +++ /dev/null @@ -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()); - } -} diff --git a/event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/EventDispatcher.java b/event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/EventDispatcher.java deleted file mode 100644 index 468160b45..000000000 --- a/event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/EventDispatcher.java +++ /dev/null @@ -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 { - - private Map, Channel> handlers; - - public EventDispatcher() { - handlers = new HashMap, Channel>(); - } - - public void registerChannel(Class contentType, - Channel channel) { - handlers.put(contentType, channel); - } - - public void dispatch(Event content) { - handlers.get(content.getClass()).dispatch(content); - } -} \ No newline at end of file diff --git a/event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/events/UserCreatedEvent.java b/event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/events/UserCreatedEvent.java deleted file mode 100644 index 75dc776de..000000000 --- a/event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/events/UserCreatedEvent.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.iluwatar.eda.advanced.events; - -import com.iluwatar.eda.advanced.events.Event; - -/** - * @author cfarrugia - */ -public class UserCreatedEvent extends Event { -} diff --git a/event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/events/UserUpdatedEvent.java b/event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/events/UserUpdatedEvent.java deleted file mode 100644 index df1c0af43..000000000 --- a/event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/events/UserUpdatedEvent.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.iluwatar.eda.advanced.events; - -import com.iluwatar.eda.advanced.events.Event; - -public class UserUpdatedEvent extends Event { -} diff --git a/event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/framework/Channel.java b/event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/framework/Channel.java deleted file mode 100644 index 20a1cceaa..000000000 --- a/event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/framework/Channel.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.iluwatar.eda.advanced.framework; - - -import com.iluwatar.eda.advanced.events.Event; - -public interface Channel { - void dispatch(E message); -} \ No newline at end of file diff --git a/event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/framework/DynamicRouter.java b/event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/framework/DynamicRouter.java deleted file mode 100644 index 2ae7291f9..000000000 --- a/event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/framework/DynamicRouter.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.iluwatar.eda.advanced.framework; - -public interface DynamicRouter { - void registerChannel(Class contentType, Channel channel); - void dispatch(E content); -} \ No newline at end of file diff --git a/event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/framework/Message.java b/event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/framework/Message.java deleted file mode 100644 index 6243dda97..000000000 --- a/event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/framework/Message.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.iluwatar.eda.advanced.framework; - - -public interface Message { - public Class getType(); -} diff --git a/event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/events/Event.java b/event-driven-architecture/src/main/java/com/iluwatar/eda/event/Event.java similarity index 56% rename from event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/events/Event.java rename to event-driven-architecture/src/main/java/com/iluwatar/eda/event/Event.java index 64b51ea84..5f3db28e8 100644 --- a/event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/events/Event.java +++ b/event-driven-architecture/src/main/java/com/iluwatar/eda/event/Event.java @@ -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 getType() { diff --git a/event-driven-architecture/src/main/java/com/iluwatar/eda/event/UserCreatedEvent.java b/event-driven-architecture/src/main/java/com/iluwatar/eda/event/UserCreatedEvent.java new file mode 100644 index 000000000..14d83a783 --- /dev/null +++ b/event-driven-architecture/src/main/java/com/iluwatar/eda/event/UserCreatedEvent.java @@ -0,0 +1,7 @@ +package com.iluwatar.eda.event; + +/** + * @author cfarrugia + */ +public class UserCreatedEvent extends Event { +} diff --git a/event-driven-architecture/src/main/java/com/iluwatar/eda/event/UserUpdatedEvent.java b/event-driven-architecture/src/main/java/com/iluwatar/eda/event/UserUpdatedEvent.java new file mode 100644 index 000000000..e0e3c3cd6 --- /dev/null +++ b/event-driven-architecture/src/main/java/com/iluwatar/eda/event/UserUpdatedEvent.java @@ -0,0 +1,4 @@ +package com.iluwatar.eda.event; + +public class UserUpdatedEvent extends Event { +} diff --git a/event-driven-architecture/src/main/java/com/iluwatar/eda/framework/Channel.java b/event-driven-architecture/src/main/java/com/iluwatar/eda/framework/Channel.java new file mode 100644 index 000000000..a8dd97044 --- /dev/null +++ b/event-driven-architecture/src/main/java/com/iluwatar/eda/framework/Channel.java @@ -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 { + void dispatch(E message); +} \ No newline at end of file diff --git a/event-driven-architecture/src/main/java/com/iluwatar/eda/framework/DynamicRouter.java b/event-driven-architecture/src/main/java/com/iluwatar/eda/framework/DynamicRouter.java new file mode 100644 index 000000000..751318ada --- /dev/null +++ b/event-driven-architecture/src/main/java/com/iluwatar/eda/framework/DynamicRouter.java @@ -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 { + void registerChannel(Class contentType, Channel channel); + void dispatch(E content); +} \ No newline at end of file diff --git a/event-driven-architecture/src/main/java/com/iluwatar/eda/framework/Message.java b/event-driven-architecture/src/main/java/com/iluwatar/eda/framework/Message.java new file mode 100644 index 000000000..61880e9cd --- /dev/null +++ b/event-driven-architecture/src/main/java/com/iluwatar/eda/framework/Message.java @@ -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 getType(); +} diff --git a/event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/handler/UserCreatedEventHandler.java b/event-driven-architecture/src/main/java/com/iluwatar/eda/handler/UserCreatedEventHandler.java similarity index 52% rename from event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/handler/UserCreatedEventHandler.java rename to event-driven-architecture/src/main/java/com/iluwatar/eda/handler/UserCreatedEventHandler.java index f3f4535d1..b2e831bf8 100644 --- a/event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/handler/UserCreatedEventHandler.java +++ b/event-driven-architecture/src/main/java/com/iluwatar/eda/handler/UserCreatedEventHandler.java @@ -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 { diff --git a/event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/handler/UserUpdatedEventHandler.java b/event-driven-architecture/src/main/java/com/iluwatar/eda/handler/UserUpdatedEventHandler.java similarity index 50% rename from event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/handler/UserUpdatedEventHandler.java rename to event-driven-architecture/src/main/java/com/iluwatar/eda/handler/UserUpdatedEventHandler.java index 8ed5dcf51..c7762d247 100644 --- a/event-driven-architecture/src/main/java/com/iluwatar/eda/advanced/handler/UserUpdatedEventHandler.java +++ b/event-driven-architecture/src/main/java/com/iluwatar/eda/handler/UserUpdatedEventHandler.java @@ -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 { public void dispatch(UserUpdatedEvent message) { diff --git a/event-driven-architecture/src/main/java/com/iluwatar/eda/simple/App.java b/event-driven-architecture/src/main/java/com/iluwatar/eda/simple/App.java deleted file mode 100644 index 1399811bb..000000000 --- a/event-driven-architecture/src/main/java/com/iluwatar/eda/simple/App.java +++ /dev/null @@ -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. - *

- * 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 events = new LinkedList(); - 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); - } - } -} diff --git a/event-driven-architecture/src/main/java/com/iluwatar/eda/simple/Event.java b/event-driven-architecture/src/main/java/com/iluwatar/eda/simple/Event.java deleted file mode 100644 index 1ba04c303..000000000 --- a/event-driven-architecture/src/main/java/com/iluwatar/eda/simple/Event.java +++ /dev/null @@ -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; - } -} diff --git a/event-driven-architecture/src/main/java/com/iluwatar/eda/simple/EventHandler.java b/event-driven-architecture/src/main/java/com/iluwatar/eda/simple/EventHandler.java deleted file mode 100644 index d911c6c8e..000000000 --- a/event-driven-architecture/src/main/java/com/iluwatar/eda/simple/EventHandler.java +++ /dev/null @@ -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()); - } -}