Skip to content

Commit d671972

Browse files
committed
Revert "Revert "Merge remote-tracking branch 'origin/master'""
This reverts commit 03f0a21.
1 parent 03f0a21 commit d671972

File tree

16 files changed

+377
-3
lines changed

16 files changed

+377
-3
lines changed
37.3 KB
Loading

event-driven-architecture/index.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
layout: pattern
2+
title: Event Driven Architecture
3+
folder: event-driven-architecture
4+
permalink: /patterns/event-driven-architecture
5+
6+
7+
**Intent:** Send and notify state changes of your objects to other applications using an Event-driven Architecture.
8+
9+
![alt text](./etc/class_diagram.png "Event Driven Architecture")
10+
11+
**Applicability:** Use an Event-driven architecture when
12+
13+
* you want to create a loosely coupled system
14+
* you want to build a more responsive system
15+
* you want a system that is easier to extend
16+
17+
**Real world examples:**
18+
19+
* SendGrid, an email API, sends events whenever an email is processed, delivered, opened etc... (https://sendgrid.com/docs/API_Reference/Webhooks/event.html)
20+
* Chargify, a billing API, exposes payment activity through various events (https://docs.chargify.com/api-events)
21+
* Amazon's AWS Lambda, lets you execute code in response to events such as changes to Amazon S3 buckets, updates to an Amazon DynamoDB table, or custom events generated by your applications or devices. (https://aws.amazon.com/lambda)
22+
* MySQL runs triggers based on events such as inserts and update events happening on database tables.
23+
24+
**Credits:**
25+
26+
* [Event-driven architecture - Wikipedia](http://www.computerweekly.com/feature/Write-through-write-around-write-back-Cache-explained)
27+
* [Fundamental Components of an Event-Driven Architecture](http://giocc.com/fundamental-components-of-an-event-driven-architecture.html)
28+
* [Real World Applications/Event Driven Applications](https://wiki.haskell.org/Real_World_Applications/Event_Driven_Applications)
29+
* [Event-driven architecture definition](http://searchsoa.techtarget.com/definition/event-driven-architecture)

event-driven-architecture/pom.xml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
6+
<modelVersion>4.0.0</modelVersion>
7+
<parent>
8+
<groupId>com.iluwatar</groupId>
9+
<artifactId>java-design-patterns</artifactId>
10+
<version>1.10.0-SNAPSHOT</version>
11+
</parent>
12+
13+
<artifactId>event-driven-architecture</artifactId>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>junit</groupId>
18+
<artifactId>junit</artifactId>
19+
<scope>test</scope>
20+
</dependency>
21+
22+
<dependency>
23+
<groupId>org.mockito</groupId>
24+
<artifactId>mockito-core</artifactId>
25+
<scope>test</scope>
26+
</dependency>
27+
</dependencies>
28+
</project>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.iluwatar.eda;
2+
3+
import com.iluwatar.eda.event.Event;
4+
import com.iluwatar.eda.event.UserCreatedEvent;
5+
import com.iluwatar.eda.event.UserUpdatedEvent;
6+
import com.iluwatar.eda.framework.EventDispatcher;
7+
import com.iluwatar.eda.handler.UserCreatedEventHandler;
8+
import com.iluwatar.eda.handler.UserUpdatedEventHandler;
9+
import com.iluwatar.eda.model.User;
10+
11+
/**
12+
* An event-driven architecture (EDA) is a framework that orchestrates behavior around the
13+
* production, detection and consumption of events as well as the responses they evoke. An event is
14+
* any identifiable occurrence that has significance for system hardware or software. <p/> The
15+
* example below uses an {@link EventDispatcher} to link/register {@link Event} objects to their
16+
* respective handlers once an {@link Event} is dispatched, it's respective handler is invoked and
17+
* the {@link Event} is handled accordingly.
18+
*
19+
*/
20+
public class App {
21+
22+
/**
23+
* Once the {@link EventDispatcher} is initialised, handlers related to specific events have to be
24+
* made known to the dispatcher by registering them. In this case the {@link UserCreatedEvent} is
25+
* bound to the UserCreatedEventHandler, whilst the {@link UserUpdatedEvent} is bound to the
26+
* {@link UserUpdatedEventHandler}. The dispatcher can now be called to dispatch specific events.
27+
* When a user is saved, the {@link UserCreatedEvent} can be dispatched.
28+
* On the other hand, when a user is updated, {@link UserUpdatedEvent} can be dispatched.
29+
*
30+
*/
31+
public static void main(String[] args) {
32+
33+
EventDispatcher dispatcher = new EventDispatcher();
34+
dispatcher.registerChannel(UserCreatedEvent.class, new UserCreatedEventHandler());
35+
dispatcher.registerChannel(UserUpdatedEvent.class, new UserUpdatedEventHandler());
36+
37+
User user = new User("iluwatar");
38+
dispatcher.onEvent(new UserCreatedEvent(user));
39+
dispatcher.onEvent(new UserUpdatedEvent(user));
40+
}
41+
42+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.iluwatar.eda.event;
2+
3+
import com.iluwatar.eda.framework.EventDispatcher;
4+
import com.iluwatar.eda.framework.Message;
5+
6+
/**
7+
* The {@link Event} class serves as a base class for defining custom events happening with your
8+
* system. In this example we have two types of events defined.
9+
* <ul>
10+
* <li>{@link UserCreatedEvent} - used when a user is created</li>
11+
* <li>{@link UserUpdatedEvent} - used when a user is updated</li>
12+
* </ul>
13+
* Events can be distinguished using the {@link #getType() getType} method.
14+
*/
15+
public class Event implements Message {
16+
17+
/**
18+
* Returns the event type as a {@link Class} object
19+
* In this example, this method is used by the {@link EventDispatcher} to
20+
* dispatch events depending on their type.
21+
*
22+
* @return the Event type as a {@link Class}.
23+
*/
24+
public Class<? extends Message> getType() {
25+
return getClass();
26+
}
27+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.iluwatar.eda.event;
2+
3+
import com.iluwatar.eda.model.User;
4+
5+
/**
6+
* The {@link UserCreatedEvent} should should be dispatched whenever a user has been created.
7+
* This class can be extended to contain details about the user has been created. In this example,
8+
* the entire {@link User} object is passed on as data with the event.
9+
*/
10+
public class UserCreatedEvent extends Event {
11+
12+
private User user;
13+
14+
public UserCreatedEvent(User user) {
15+
this.user = user;
16+
}
17+
18+
public User getUser() {
19+
return user;
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.iluwatar.eda.event;
2+
3+
import com.iluwatar.eda.model.User;
4+
5+
/**
6+
* The {@link UserUpdatedEvent} should should be dispatched whenever a user has been updated.
7+
* This class can be extended to contain details about the user has been updated. In this example,
8+
* the entire {@link User} object is passed on as data with the event.
9+
*/
10+
public class UserUpdatedEvent extends Event {
11+
12+
private User user;
13+
14+
public UserUpdatedEvent(User user) {
15+
this.user = user;
16+
}
17+
18+
public User getUser() {
19+
return user;
20+
}
21+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.iluwatar.eda.framework;
2+
3+
import com.iluwatar.eda.event.Event;
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
/**
9+
* Handles the routing of {@link Event} messages to associated handlers.
10+
* A {@link HashMap} is used to store the association between events and their respective handlers.
11+
*
12+
*/
13+
public class EventDispatcher {
14+
15+
private Map<Class<? extends Event>, Handler<?>> handlers;
16+
17+
public EventDispatcher() {
18+
handlers = new HashMap<>();
19+
}
20+
21+
/**
22+
* Links an {@link Event} to a specific {@link Handler}.
23+
*
24+
* @param eventType The {@link Event} to be registered
25+
* @param handler The {@link Handler} that will be handling the {@link Event}
26+
*/
27+
public void registerChannel(Class<? extends Event> eventType,
28+
Handler<?> handler) {
29+
handlers.put(eventType, handler);
30+
}
31+
32+
/**
33+
* Dispatches an {@link Event} depending on it's type.
34+
*
35+
* @param event The {@link Event} to be dispatched
36+
*/
37+
public void onEvent(Event event) {
38+
handlers.get(event.getClass()).onEvent(event);
39+
}
40+
41+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.iluwatar.eda.framework;
2+
3+
import com.iluwatar.eda.event.Event;
4+
5+
/**
6+
* This interface can be implemented to handle different types of messages.
7+
* Every handler is responsible for a single of type message
8+
*/
9+
public interface Handler<E extends Message> {
10+
11+
/**
12+
* The onEvent method should implement and handle behavior related to the event.
13+
* This can be as simple as calling another service to handle the event on publishing the event on
14+
* a queue to be consumed by other sub systems.
15+
* @param event the {@link Event} object to be handled.
16+
*/
17+
void onEvent(Event event);
18+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.iluwatar.eda.framework;
2+
3+
/**
4+
* A {@link Message} is an object with a specific type that is associated
5+
* to a specific {@link Handler}.
6+
*/
7+
public interface Message {
8+
9+
/**
10+
* Returns the message type as a {@link Class} object. In this example the message type is
11+
* used to handle events by their type.
12+
* @return the message type as a {@link Class}.
13+
*/
14+
Class<? extends Message> getType();
15+
}

0 commit comments

Comments
 (0)