Appearance
Events
Squizy automatically publishes Spring application events whenever entities are created, updated, or deleted.
SquizyEntityAccessEvent
All entity operations publish a SquizyEntityAccessEvent<T> through Spring's ApplicationEventPublisher:
java
public class SquizyEntityAccessEvent<T> extends ApplicationEvent {
private final Class<T> entityClass;
private final ActionType actionType;
private final List<T> values;
private final String user;
}ActionType
| ActionType | Trigger |
|---|---|
CREATE | Entity created (single or bulk) |
UPDATE | Entity updated via patch (single or bulk) |
DELETE | Entity deleted (single or bulk) |
INFO
READ operations do not publish events.
Event Properties
| Property | Description |
|---|---|
entityClass | The Java class of the affected entity |
actionType | CREATE, UPDATE, or DELETE |
values | The list of affected entity instances |
user | The email of the user who performed the action |
Listening to Events
Synchronous Listeners
Synchronous listeners execute within the same transaction:
java
@Component
public class ProductEventListener {
@EventListener
public void onProductEvent(SquizyEntityAccessEvent<Product> event) {
if (event.getActionType() == ActionType.CREATE) {
event.getValues().forEach(product ->
log.info("Product created: {}", product.getName())
);
}
}
}WARNING
Synchronous listeners run within the same transaction. If the listener throws an exception, the entire operation is rolled back.
Asynchronous Listeners
For operations that should not block the main transaction:
java
@Component
public class ProductNotificationListener {
@Async
@EventListener
public void onProductCreated(SquizyEntityAccessEvent<Product> event) {
if (event.getActionType() == ActionType.CREATE) {
notificationService.notifyNewProduct(event.getValues());
}
}
}To use @Async, enable it with @EnableAsync on your configuration class.
Type-Safe Listeners
Spring resolves the correct listener based on the type parameter:
java
@EventListener
public void onOrderEvent(SquizyEntityAccessEvent<Order> event) {
// Only receives Order events
}
@EventListener
public void onCategoryEvent(SquizyEntityAccessEvent<Category> event) {
// Only receives Category events
}Implementation Details
Events are published after the entity has been persisted to the database but within the same transaction (for synchronous listeners). The framework uses ByteBuddy at runtime to create typed subclasses of SquizyEntityAccessEvent for each entity class, ensuring correct generic type resolution.