#113 Event Driven Architecture

Adds more Javadoc and fixes checkstyle issues.
This commit is contained in:
cfarrugia 2015-11-28 15:03:22 +01:00
parent b8b94b697a
commit e1c0731f7e
9 changed files with 90 additions and 40 deletions

View File

@ -19,28 +19,5 @@
<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>

View File

@ -5,23 +5,37 @@ import com.iluwatar.eda.event.UserCreatedEvent;
import com.iluwatar.eda.event.UserUpdatedEvent; import com.iluwatar.eda.event.UserUpdatedEvent;
import com.iluwatar.eda.handler.UserCreatedEventHandler; import com.iluwatar.eda.handler.UserCreatedEventHandler;
import com.iluwatar.eda.handler.UserUpdatedEventHandler; import com.iluwatar.eda.handler.UserUpdatedEventHandler;
import com.iluwatar.eda.model.User;
/** /**
* An event-driven architecture (EDA) is a framework that orchestrates behavior around the * 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 * 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 * 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 * example below 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 * respective handlers once an {@link Event} is dispatched, it's respective handler is invoked and
* the {@link Event} is handled accordingly * the {@link Event} is handled accordingly.
*
*/ */
public class App { public class App {
/**
* Once the {@link EventDispatcher} is initialised, channels related to specific events have to be
* made known to the dispatcher by registering them. In this case the {@link UserCreatedEvent} is
* bound to the UserCreatedEventHandler, whilst the {@link UserUpdatedEvent} is bound to the
* {@link UserUpdatedEventHandler}. The dispatcher can now be called to dispatch specific events.
* When a user is saved, the {@link UserCreatedEvent} can be dispatched.
* On the other hand, when a user is updated, {@link UserUpdatedEvent} can be dispatched.
*
*/
public static void main(String[] args) { public static void main(String[] args) {
EventDispatcher dispatcher = new EventDispatcher(); EventDispatcher dispatcher = new EventDispatcher();
dispatcher.registerChannel(UserCreatedEvent.class, new UserCreatedEventHandler()); dispatcher.registerChannel(UserCreatedEvent.class, new UserCreatedEventHandler());
dispatcher.registerChannel(UserUpdatedEvent.class, new UserUpdatedEventHandler()); dispatcher.registerChannel(UserUpdatedEvent.class, new UserUpdatedEventHandler());
dispatcher.dispatch(new UserCreatedEvent());
dispatcher.dispatch(new UserUpdatedEvent()); User user = new User("iluwatar");
dispatcher.dispatch(new UserCreatedEvent(user));
dispatcher.dispatch(new UserUpdatedEvent(user));
} }
} }

View File

@ -8,21 +8,20 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
/** /**
* The {@link Event Dispatcher} handles routing of {@link Event} messages * Handles the routing of {@link Event} messages to associated channels.
* 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<>(); 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}

View File

@ -1,8 +1,22 @@
package com.iluwatar.eda.event; package com.iluwatar.eda.event;
import com.iluwatar.eda.model.User;
/** /**
* The {@link UserCreatedEvent} class should should be dispatched whenever a user has been created. * 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. * This class can be extended to contain details about the user has been created. In this example,
* the entire {@link User} object is passed on as data with the event.
*/ */
public class UserCreatedEvent extends Event { public class UserCreatedEvent extends Event {
private User user;
public UserCreatedEvent(User user) {
this.user = user;
}
public User getUser() {
return user;
}
} }

View File

@ -1,8 +1,21 @@
package com.iluwatar.eda.event; package com.iluwatar.eda.event;
import com.iluwatar.eda.model.User;
/** /**
* The {@link UserUpdatedEvent} class should should be dispatched whenever a user has been updated. * 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. * This class can be extended to contain details about the user has been updated. In this example,
* the entire {@link User} object is passed on as data with the event.
*/ */
public class UserUpdatedEvent extends Event { public class UserUpdatedEvent extends Event {
private User user;
public UserUpdatedEvent(User user) {
this.user = user;
}
public User getUser() {
return user;
}
} }

View File

@ -7,5 +7,5 @@ import com.iluwatar.eda.event.Event;
* of message * of message
*/ */
public interface Channel<E extends Message> { public interface Channel<E extends Message> {
void dispatch(E message); void dispatch(Event message);
} }

View File

@ -1,13 +1,19 @@
package com.iluwatar.eda.handler; package com.iluwatar.eda.handler;
import com.iluwatar.eda.event.Event;
import com.iluwatar.eda.event.UserCreatedEvent; import com.iluwatar.eda.event.UserCreatedEvent;
import com.iluwatar.eda.framework.Channel; import com.iluwatar.eda.framework.Channel;
import com.iluwatar.eda.model.User;
/** /**
* 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) {
System.out.println("User Created!"); @Override
public void dispatch(Event message) {
UserCreatedEvent userCreatedEvent = (UserCreatedEvent) message;
System.out.printf("User with %s has been Created!", userCreatedEvent.getUser().getUsername());
} }
} }

View File

@ -1,5 +1,7 @@
package com.iluwatar.eda.handler; package com.iluwatar.eda.handler;
import com.iluwatar.eda.event.Event;
import com.iluwatar.eda.event.UserCreatedEvent;
import com.iluwatar.eda.event.UserUpdatedEvent; import com.iluwatar.eda.event.UserUpdatedEvent;
import com.iluwatar.eda.framework.Channel; import com.iluwatar.eda.framework.Channel;
@ -7,7 +9,11 @@ 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) {
System.out.println("User Updated!"); @Override
public void dispatch(Event message) {
UserUpdatedEvent userUpdatedEvent = (UserUpdatedEvent) message;
System.out.printf("User with %s has been Updated!", userUpdatedEvent.getUser().getUsername());
} }
} }

View File

@ -0,0 +1,21 @@
package com.iluwatar.eda.model;
import com.iluwatar.eda.event.UserCreatedEvent;
import com.iluwatar.eda.event.UserUpdatedEvent;
/**
* This {@link User} class is a basic pojo used to demonstrate user data sent along with
* the {@link UserCreatedEvent} and {@link UserUpdatedEvent} events.
*/
public class User {
private String username;
public User(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
}