parent
fc70a706c6
commit
b8b94b697a
@ -19,4 +19,28 @@
|
|||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</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>
|
</project>
|
@ -7,13 +7,12 @@ import com.iluwatar.eda.handler.UserCreatedEventHandler;
|
|||||||
import com.iluwatar.eda.handler.UserUpdatedEventHandler;
|
import com.iluwatar.eda.handler.UserUpdatedEventHandler;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An event-driven architecture (EDA) is a framework that orchestrates behavior around the production,
|
* An event-driven architecture (EDA) is a framework that orchestrates behavior around the
|
||||||
* detection and consumption of events as well as the responses they evoke.
|
* production, detection and consumption of events as well as the responses they evoke. An event is
|
||||||
* An event is any identifiable occurrence that has significance for system hardware or software.
|
* any identifiable occurrence that has significance for system hardware or software. <p/> The
|
||||||
* <p/>
|
* example below we uses an {@link EventDispatcher} to link/register {@link Event} objects to their
|
||||||
* The example below we uses an {@link EventDispatcher} to link/register {@link Event} objects to
|
* respective handlers Once an {@link Event} is dispatched, it's respective handler is invoked and
|
||||||
* their respective handlers Once an {@link Event} is dispatched,
|
* the {@link Event} is handled accordingly
|
||||||
* it's respective handler is invoked and the {@link Event} is handled accordingly
|
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
|
@ -8,29 +8,33 @@ import java.util.HashMap;
|
|||||||
import java.util.Map;
|
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.
|
* A {@link HashMap} is used to store the association between events and their respective handlers.
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
public class EventDispatcher implements DynamicRouter<Event> {
|
public class EventDispatcher implements DynamicRouter<Event> {
|
||||||
|
|
||||||
private Map<Class<? extends Event>, Channel> handlers;
|
private Map<Class<? extends Event>, Channel> handlers;
|
||||||
|
|
||||||
public EventDispatcher() {
|
public EventDispatcher() {
|
||||||
handlers = new HashMap<Class<? extends Event>, Channel>();
|
handlers = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Links an {@link Event} to a specific {@link Channel}
|
* Links an {@link Event} to a specific {@link Channel}
|
||||||
|
*
|
||||||
* @param contentType The {@link Event} to be registered
|
* @param contentType The {@link Event} to be registered
|
||||||
* @param channel The {@link Channel} that will be handling the {@link Event}
|
* @param channel The {@link Channel} that will be handling the {@link Event}
|
||||||
*/
|
*/
|
||||||
public void registerChannel(Class<? extends Event> contentType,
|
public void registerChannel(Class<? extends Event> contentType,
|
||||||
Channel channel) {
|
Channel<?> channel) {
|
||||||
handlers.put(contentType, channel);
|
handlers.put(contentType, channel);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dispathes an {@link Event} depending on it's type.
|
* Dispatches an {@link Event} depending on it's type.
|
||||||
|
*
|
||||||
* @param content The {@link Event} to be dispatched
|
* @param content The {@link Event} to be dispatched
|
||||||
*/
|
*/
|
||||||
public void dispatch(Event content) {
|
public void dispatch(Event content) {
|
||||||
|
@ -2,7 +2,24 @@ package com.iluwatar.eda.event;
|
|||||||
|
|
||||||
import com.iluwatar.eda.framework.Message;
|
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 Event implements Message {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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() {
|
public Class<? extends Message> getType() {
|
||||||
return getClass();
|
return getClass();
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
package com.iluwatar.eda.event;
|
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 {
|
public class UserCreatedEvent extends Event {
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,8 @@
|
|||||||
package com.iluwatar.eda.event;
|
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 {
|
public class UserUpdatedEvent extends Event {
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
package com.iluwatar.eda.framework;
|
package com.iluwatar.eda.framework;
|
||||||
|
|
||||||
|
import com.iluwatar.eda.event.Event;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Channels are delivery points for messages.
|
* Channels are delivery points for messages. Every {@link Channel} is responsible for a single type
|
||||||
* Every {@link Channel} is responsible for a single type of message
|
* of message
|
||||||
*/
|
*/
|
||||||
public interface Channel<E extends Message> {
|
public interface Channel<E extends Message> {
|
||||||
void dispatch(E message);
|
void dispatch(E message);
|
||||||
|
@ -2,10 +2,12 @@ package com.iluwatar.eda.framework;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* A {@link DynamicRouter} is responsible for selecting the proper path of a {@link Message}
|
* 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
|
* Messages can be associated to Channels through the registerChannel method and dispatched by
|
||||||
* the dispatch method.
|
* calling the dispatch method.
|
||||||
*/
|
*/
|
||||||
public interface DynamicRouter<E extends Message> {
|
public interface DynamicRouter<E extends Message> {
|
||||||
void registerChannel(Class<? extends E> contentType, Channel channel);
|
|
||||||
|
void registerChannel(Class<? extends E> contentType, Channel<?> channel);
|
||||||
|
|
||||||
void dispatch(E content);
|
void dispatch(E content);
|
||||||
}
|
}
|
@ -1,8 +1,8 @@
|
|||||||
package com.iluwatar.eda.framework;
|
package com.iluwatar.eda.framework;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A {@link Message} is an object with a specific type that is associated to a
|
* A {@link Message} is an object with a specific type that is associated
|
||||||
* specific {@link Channel}
|
* to a specific {@link Channel}.
|
||||||
*/
|
*/
|
||||||
public interface Message {
|
public interface Message {
|
||||||
Class<? extends Message> getType();
|
Class<? extends Message> getType();
|
||||||
|
@ -4,10 +4,9 @@ import com.iluwatar.eda.event.UserCreatedEvent;
|
|||||||
import com.iluwatar.eda.framework.Channel;
|
import com.iluwatar.eda.framework.Channel;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles the {@link UserCreatedEvent} message
|
* Handles the {@link UserCreatedEvent} message.
|
||||||
*/
|
*/
|
||||||
public class UserCreatedEventHandler implements Channel<UserCreatedEvent> {
|
public class UserCreatedEventHandler implements Channel<UserCreatedEvent> {
|
||||||
|
|
||||||
public void dispatch(UserCreatedEvent message) {
|
public void dispatch(UserCreatedEvent message) {
|
||||||
System.out.println("User Created!");
|
System.out.println("User Created!");
|
||||||
}
|
}
|
||||||
|
@ -4,10 +4,9 @@ import com.iluwatar.eda.event.UserUpdatedEvent;
|
|||||||
import com.iluwatar.eda.framework.Channel;
|
import com.iluwatar.eda.framework.Channel;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles the {@link UserUpdatedEvent} message
|
* Handles the {@link UserUpdatedEvent} message.
|
||||||
*/
|
*/
|
||||||
public class UserUpdatedEventHandler implements Channel<UserUpdatedEvent> {
|
public class UserUpdatedEventHandler implements Channel<UserUpdatedEvent> {
|
||||||
|
|
||||||
public void dispatch(UserUpdatedEvent message) {
|
public void dispatch(UserUpdatedEvent message) {
|
||||||
System.out.println("User Updated!");
|
System.out.println("User Updated!");
|
||||||
}
|
}
|
||||||
|
4
pom.xml
4
pom.xml
@ -244,8 +244,8 @@
|
|||||||
<configuration>
|
<configuration>
|
||||||
<configLocation>checkstyle.xml</configLocation>
|
<configLocation>checkstyle.xml</configLocation>
|
||||||
<encoding>UTF-8</encoding>
|
<encoding>UTF-8</encoding>
|
||||||
<consoleOutput>false</consoleOutput>
|
<consoleOutput>true</consoleOutput>
|
||||||
<failsOnError>false</failsOnError>
|
<failsOnError>true</failsOnError>
|
||||||
</configuration>
|
</configuration>
|
||||||
</execution>
|
</execution>
|
||||||
</executions>
|
</executions>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user