docs: update eda

This commit is contained in:
Ilkka Seppälä
2024-05-25 22:16:54 +03:00
parent ff7594465c
commit 9c30eaa5ed
+96 -21
View File
@@ -24,19 +24,19 @@ Event-Driven Architecture (EDA) is designed to orchestrate behavior around the p
## Explanation
### Real-world example
Real-world example
> A real-world example of the Event-Driven Architecture (EDA) pattern is the operation of an air traffic control system. In this system, events such as aircraft entering airspace, changes in weather conditions, and ground vehicle movements trigger specific responses like altering flight paths, scheduling gate assignments, and updating runway usage. This setup allows for highly efficient, responsive, and safe management of airport operations, reflecting EDA's core principles of asynchronous communication and dynamic event handling.
### In plain words
In plain words
> Event-Driven Architecture is a design pattern where system behavior is dictated by the occurrence of specific events, allowing for dynamic, efficient, and decoupled responses.
### Wikipedia says
Wikipedia says
> Event-driven architecture (EDA) is a software architecture paradigm concerning the production and detection of events.
### Programmatic Example
**Programmatic Example**
The Event-Driven Architecture (EDA) pattern in this module is implemented using several key classes and concepts:
@@ -45,27 +45,105 @@ The Event-Driven Architecture (EDA) pattern in this module is implemented using
* EventDispatcher: This class is responsible for dispatching events to their respective handlers. It maintains a mapping of event types to handlers.
* UserCreatedEventHandler and UserUpdatedEventHandler: These are the handler classes for the UserCreatedEvent and UserUpdatedEvent respectively. They contain the logic to execute when these events occur.
Here's a simplified code example of how these classes interact:
First, we'll define the `Event` abstract class and the concrete event classes `UserCreatedEvent` and `UserUpdatedEvent`.
```java
// Create an EventDispatcher
EventDispatcher dispatcher = new EventDispatcher();
public abstract class Event {
// Event related properties and methods
}
// Register handlers for UserCreatedEvent and UserUpdatedEvent
dispatcher.registerHandler(UserCreatedEvent.class, new UserCreatedEventHandler());
dispatcher.registerHandler(UserUpdatedEvent.class, new UserUpdatedEventHandler());
public class UserCreatedEvent extends Event {
private User user;
// Create a User
User user = new User("iluwatar");
public UserCreatedEvent(User user) {
this.user = user;
}
// Dispatch UserCreatedEvent
dispatcher.dispatch(new UserCreatedEvent(user));
public User getUser() {
return user;
}
}
// Dispatch UserUpdatedEvent
dispatcher.dispatch(new UserUpdatedEvent(user));
public class UserUpdatedEvent extends Event {
private User user;
public UserUpdatedEvent(User user) {
this.user = user;
}
public User getUser() {
return user;
}
}
```
In this example, the EventDispatcher is created and handlers for UserCreatedEvent and UserUpdatedEvent are registered. Then a User is created and UserCreatedEvent and UserUpdatedEvent are dispatched. When these events are dispatched, the EventDispatcher calls the appropriate handler to handle the event. This is a basic example of an Event-Driven Architecture, where the occurrence of events drives the flow of the program. The system is designed to respond to events as they occur, which allows for a high degree of flexibility and decoupling between components.
Next, we'll define the event handlers `UserCreatedEventHandler` and `UserUpdatedEventHandler`.
```java
public class UserCreatedEventHandler {
public void onUserCreated(UserCreatedEvent event) {
// Logic to execute when a UserCreatedEvent occurs
}
}
public class UserUpdatedEventHandler {
public void onUserUpdated(UserUpdatedEvent event) {
// Logic to execute when a UserUpdatedEvent occurs
}
}
```
Then, we'll define the `EventDispatcher` class that is responsible for dispatching events to their respective handlers.
```java
public class EventDispatcher {
private Map<Class<? extends Event>, List<Consumer<Event>>> handlers = new HashMap<>();
public <E extends Event> void registerHandler(Class<E> eventType, Consumer<E> handler) {
handlers.computeIfAbsent(eventType, k -> new ArrayList<>()).add(handler::accept);
}
public void dispatch(Event event) {
List<Consumer<Event>> eventHandlers = handlers.get(event.getClass());
if (eventHandlers != null) {
eventHandlers.forEach(handler -> handler.accept(event));
}
}
}
```
Finally, we'll demonstrate how to use these classes in the main application.
```java
public class App {
public static void main(String[] args) {
// Create an EventDispatcher
EventDispatcher dispatcher = new EventDispatcher();
// Register handlers for UserCreatedEvent and UserUpdatedEvent
dispatcher.registerHandler(UserCreatedEvent.class, new UserCreatedEventHandler()::onUserCreated);
dispatcher.registerHandler(UserUpdatedEvent.class, new UserUpdatedEventHandler()::onUserUpdated);
// Create a User
User user = new User("iluwatar");
// Dispatch UserCreatedEvent
dispatcher.dispatch(new UserCreatedEvent(user));
// Dispatch UserUpdatedEvent
dispatcher.dispatch(new UserUpdatedEvent(user));
}
}
```
Running the example produces the following console output:
```
22:15:19.997 [main] INFO com.iluwatar.eda.handler.UserCreatedEventHandler -- User 'iluwatar' has been Created!
22:15:20.000 [main] INFO com.iluwatar.eda.handler.UserUpdatedEventHandler -- User 'iluwatar' has been Updated!
```
This example demonstrates the Event-Driven Architecture pattern, where the occurrence of events drives the flow of the program. The system is designed to respond to events as they occur, which allows for a high degree of flexibility and decoupling between components.
## Class diagram
@@ -110,10 +188,7 @@ Trade-offs:
## Credits
* [Event-driven architecture - Wikipedia](https://en.wikipedia.org/wiki/Event-driven_architecture)
* [What is an Event-Driven Architecture](https://aws.amazon.com/event-driven-architecture/)
* [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)
* [Patterns of Enterprise Application Architecture](https://amzn.to/3Q3vBki)
* [Enterprise Integration Patterns: Designing, Building, and Deploying Messaging Solutions](https://amzn.to/49Aljz0)
* [Reactive Messaging Patterns With the Actor Model: Applications and Integration in Scala and Akka](https://amzn.to/3UeoBUa)
* [What is an Event-Driven Architecture (Amazon)](https://aws.amazon.com/event-driven-architecture/)