Skip to content

Commit 36f4dc0

Browse files
committed
Add a developer guide which can be referenced.
1 parent f233dda commit 36f4dc0

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

src/main/resources/archetype-resources/DEVELOPER_GUIDE.md

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Add the following dependency to your `pom.xml`:
2626
<dependency>
2727
<groupId>org.tinystruct</groupId>
2828
<artifactId>tinystruct</artifactId>
29-
<version>${tinystructVersion}</version>
29+
<version>1.7.17</version>
3030
</dependency>
3131
```
3232

@@ -185,6 +185,46 @@ The dispatcher provides several utility commands:
185185
### Custom Exception Handling
186186
Extending `ApplicationException` or `ApplicationRuntimeException` allows for structured error reporting across both CLI and Web modes.
187187

188+
### Event Mechanism
189+
Tinystruct supports an event-driven architecture to decouple components. To improve performance in asynchronous scenarios, event handlers can offload heavy processing to separate threads.
190+
191+
1. **Define an Event**: Implement `org.tinystruct.system.Event<T>`.
192+
```java
193+
public class UserRegisterEvent implements Event<User> {
194+
private final User user;
195+
196+
public UserRegisterEvent(User user) {
197+
this.user = user;
198+
}
199+
200+
@Override
201+
public String getName() {
202+
return "user_register";
203+
}
204+
205+
@Override
206+
public User getPayload() {
207+
return user;
208+
}
209+
}
210+
```
211+
212+
2. **Dispatch an Event**:
213+
```java
214+
EventDispatcher.getInstance().dispatch(new UserRegisterEvent(newUser));
215+
```
216+
217+
3. **Asynchronous Handling**:
218+
To prevent blocking the main thread (e.g., during HTTP requests), handle events asynchronously using `CompletableFuture`.
219+
```java
220+
EventDispatcher.getInstance().registerHandler(UserRegisterEvent.class, event -> {
221+
CompletableFuture.runAsync(() -> {
222+
// Heavy tasks: send email, update analytics, etc.
223+
sendWelcomeEmail(event.getPayload());
224+
});
225+
});
226+
```
227+
188228
---
189229

190230
## 7. Networking & Integration

0 commit comments

Comments
 (0)