| @@ -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,22 +7,21 @@ 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 { | ||||||
|  |  | ||||||
|     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 UserCreatedEvent()); | ||||||
|         dispatcher.dispatch(new UserUpdatedEvent()); |     dispatcher.dispatch(new UserUpdatedEvent()); | ||||||
|     } |   } | ||||||
|  |  | ||||||
| } | } | ||||||
|   | |||||||
| @@ -8,32 +8,36 @@ 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 channel The {@link Channel} that will be handling the {@link Event} |    * @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) { |   public void registerChannel(Class<? extends Event> contentType, | ||||||
|         handlers.put(contentType, channel); |                               Channel<?> 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) { |    */ | ||||||
|         handlers.get(content.getClass()).dispatch(content); |   public void dispatch(Event content) { | ||||||
|     } |     handlers.get(content.getClass()).dispatch(content); | ||||||
|  |   } | ||||||
| } | } | ||||||
| @@ -2,8 +2,25 @@ 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 { | ||||||
|     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(); | ||||||
|  |   } | ||||||
| } | } | ||||||
| @@ -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,9 +1,11 @@ | |||||||
| 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 dispatch(E content); |   void registerChannel(Class<? extends E> contentType, Channel<?> channel); | ||||||
|  |  | ||||||
|  |   void dispatch(E content); | ||||||
| } | } | ||||||
| @@ -1,9 +1,9 @@ | |||||||
| 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,11 +4,10 @@ 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,11 +4,10 @@ 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> | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user