#113 Event Driven Architecture

Adds more Javadoc
This commit is contained in:
cfarrugia 2015-11-28 13:12:16 +01:00
parent fc70a706c6
commit b8b94b697a
12 changed files with 112 additions and 61 deletions

View File

@ -19,4 +19,28 @@
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.15</version>
<executions>
<execution>
<id>validate</id>
<goals>
<goal>check</goal>
</goals>
<phase>validate</phase>
<configuration>
<configLocation>checkstyle.xml</configLocation>
<encoding>UTF-8</encoding>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -7,22 +7,21 @@ 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
* 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());
}
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

@ -8,32 +8,36 @@ import java.util.HashMap;
import java.util.Map;
/**
* The {@link Event Dispatcher} handles routing of {@link Event} messages to associated channels.
* 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;
private Map<Class<? extends Event>, Channel> handlers;
public EventDispatcher() {
handlers = new HashMap<Class<? extends Event>, Channel>();
}
public EventDispatcher() {
handlers = new HashMap<>();
}
/**
* 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);
}
/**
* 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);
}
/**
* Dispatches 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

@ -2,8 +2,25 @@ package com.iluwatar.eda.event;
import com.iluwatar.eda.framework.Message;
/**
* The {@link Event} class serves as a base class for defining custom events happening with your
* system. In this example we have two types of events defined.
* <ul>
* <li>{@link UserCreatedEvent} - used when a user is created</li>
* <li>{@link UserUpdatedEvent} - used when a user is updated</li>
* </ul>
* Events can be distinguished using the {@link #getType() getType} method.
*/
public class Event implements Message {
public Class<? extends Message> getType() {
return getClass();
}
/**
* Returns the event type as a {@link Class} object
* In this example, this method is used by the {@link com.iluwatar.eda.EventDispatcher} to
* dispatch events depending on their type.
*
* @return the Event type as a {@link Class}.
*/
public Class<? extends Message> getType() {
return getClass();
}
}

View File

@ -1,7 +1,8 @@
package com.iluwatar.eda.event;
/**
* @author cfarrugia
* The {@link UserCreatedEvent} class should should be dispatched whenever a user has been created.
* This class can be extended to contain details about the user has been created.
*/
public class UserCreatedEvent extends Event {
}

View File

@ -1,4 +1,8 @@
package com.iluwatar.eda.event;
/**
* The {@link UserUpdatedEvent} class should should be dispatched whenever a user has been updated.
* This class can be extended to contain details about the user has been updated.
*/
public class UserUpdatedEvent extends Event {
}

View File

@ -1,9 +1,11 @@
package com.iluwatar.eda.framework;
import com.iluwatar.eda.event.Event;
/**
* Channels are delivery points for messages.
* Every {@link Channel} is responsible for a single type of message
* 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);
void dispatch(E message);
}

View File

@ -2,10 +2,12 @@ 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.
* 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);
void registerChannel(Class<? extends E> contentType, Channel<?> channel);
void dispatch(E content);
}

View File

@ -1,9 +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}
* 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();
Class<? extends Message> getType();
}

View File

@ -4,11 +4,10 @@ import com.iluwatar.eda.event.UserCreatedEvent;
import com.iluwatar.eda.framework.Channel;
/**
* Handles the {@link UserCreatedEvent} message
* Handles the {@link UserCreatedEvent} message.
*/
public class UserCreatedEventHandler implements Channel<UserCreatedEvent> {
public void dispatch(UserCreatedEvent message) {
System.out.println("User Created!");
}
public void dispatch(UserCreatedEvent message) {
System.out.println("User Created!");
}
}

View File

@ -4,11 +4,10 @@ import com.iluwatar.eda.event.UserUpdatedEvent;
import com.iluwatar.eda.framework.Channel;
/**
* Handles the {@link UserUpdatedEvent} message
* Handles the {@link UserUpdatedEvent} message.
*/
public class UserUpdatedEventHandler implements Channel<UserUpdatedEvent> {
public void dispatch(UserUpdatedEvent message) {
System.out.println("User Updated!");
}
public void dispatch(UserUpdatedEvent message) {
System.out.println("User Updated!");
}
}

View File

@ -244,8 +244,8 @@
<configuration>
<configLocation>checkstyle.xml</configLocation>
<encoding>UTF-8</encoding>
<consoleOutput>false</consoleOutput>
<failsOnError>false</failsOnError>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
</configuration>
</execution>
</executions>