event-driven-architecture/README.md
Event-Driven Architecture (EDA) is designed to orchestrate behavior around the production, detection, consumption of, and reaction to events. This architecture enables highly decoupled, scalable, and dynamic interconnections between event producers and consumers.
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
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
Event-driven architecture (EDA) is a software architecture paradigm concerning the production and detection of events.
Architecture diagram
The Event-Driven Architecture (EDA) pattern in this module is implemented using several key classes and concepts:
First, we'll define the Event abstract class and the concrete event classes UserCreatedEvent and UserUpdatedEvent.
public abstract class Event {
// Event related properties and methods
}
public class UserCreatedEvent extends Event {
private User user;
public UserCreatedEvent(User user) {
this.user = user;
}
public User getUser() {
return user;
}
}
public class UserUpdatedEvent extends Event {
private User user;
public UserUpdatedEvent(User user) {
this.user = user;
}
public User getUser() {
return user;
}
}
Next, we'll define the event handlers UserCreatedEventHandler and UserUpdatedEventHandler.
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.
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.
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.
Use an Event-driven architecture when
Benefits:
Trade-offs: