Implementation:Apache Shardingsphere DataChangedEvent Constructor
| Knowledge Sources | |
|---|---|
| Domains | Configuration_Management, Distributed_Systems |
| Last Updated | 2026-02-10 00:00 GMT |
Overview
Concrete tool for constructing structured data change event objects that represent modifications to distributed registry nodes, provided by the ShardingSphere mode core module.
Description
The DataChangedEvent class is an immutable value object that encapsulates a change notification from the distributed registry. It is constructed via a Lombok-generated @RequiredArgsConstructor that accepts three final fields: the registry node key (path), the node value (content), and the change type.
The class defines an inner enum Type with four values:
- ADDED: The registry node was newly created. In the context of configuration changes, this typically indicates that a new rule item or configuration node was added for the first time.
- UPDATED: An existing registry node's value was modified. This is the most common type for configuration changes, occurring when the active version pointer is overwritten during a version switch.
- DELETED: The registry node was removed. This occurs when a rule item or entire rule configuration is deleted.
- IGNORED: The event should be silently discarded. This type is used for registry infrastructure events (session events, connection events) that do not represent meaningful configuration changes.
The class uses Lombok annotations for conciseness:
- @RequiredArgsConstructor generates the all-args constructor from the three final fields.
- @Getter generates accessor methods for all three fields: getKey(), getValue(), getType().
This event object is the universal data carrier in ShardingSphere's event-driven configuration pipeline. It is produced by registry watcher implementations and consumed by all DataChangedEventListener implementations throughout the cluster dispatch system.
Usage
DataChangedEvent objects are constructed by the distributed registry watcher infrastructure when nodes are modified. They are consumed by:
- DatabaseMetaDataChangedListener.onChange() for database-scoped events.
- GlobalMetaDataChangedListener for global configuration events.
- ActiveVersionChecker.checkSame() for version validation using the event's key and value.
Code Reference
Source Location
- Repository: Apache ShardingSphere
- File: mode/core/src/main/java/org/apache/shardingsphere/mode/event/DataChangedEvent.java
- Lines: 28-43
Signature
@RequiredArgsConstructor
@Getter
public final class DataChangedEvent {
private final String key;
private final String value;
private final Type type;
public enum Type {
ADDED, UPDATED, DELETED, IGNORED
}
}
Import
import org.apache.shardingsphere.mode.event.DataChangedEvent;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| key | String | Yes | The full registry path of the node that changed. For configuration changes this encodes the database name, rule type, and item name in a hierarchical path structure. |
| value | String | Yes | The new content of the registry node. For ADDED and UPDATED events on active version paths, this is the version number as a string. For DELETED events, this is the last known value. |
| type | DataChangedEvent.Type | Yes | The kind of change that occurred. One of ADDED, UPDATED, DELETED, or IGNORED. |
Outputs
| Name | Type | Description |
|---|---|---|
| instance | DataChangedEvent | An immutable event object. Fields are accessed via getKey(), getValue(), and getType() accessor methods. |
Usage Examples
// Constructing a DataChangedEvent when an active version pointer is updated
// (This is typically done by the registry watcher, not by application code)
DataChangedEvent event = new DataChangedEvent(
"/metadata/my_db/rules/sharding/tables/t_order/active_version",
"3",
DataChangedEvent.Type.UPDATED
);
// Consuming the event in a listener
String registryPath = event.getKey(); // "/metadata/my_db/rules/sharding/tables/t_order/active_version"
String newVersion = event.getValue(); // "3"
DataChangedEvent.Type changeType = event.getType(); // UPDATED
// Constructing a DataChangedEvent for a deleted rule item
DataChangedEvent deleteEvent = new DataChangedEvent(
"/metadata/my_db/rules/sharding/tables/t_order",
"",
DataChangedEvent.Type.DELETED
);
// The IGNORED type is used for non-actionable registry events
DataChangedEvent ignoredEvent = new DataChangedEvent(
"/some/infrastructure/path",
"",
DataChangedEvent.Type.IGNORED
);