Class EventJournal
java.lang.Object
com.loomcache.server.journal.EventJournal
Distributed event journal for durable, ordered event sourcing.
Features:
- Monotonically increasing sequence numbers for ordering
- Thread-safe append and read operations
- Query by key, event type, or both
- Real-time subscription with backpressure
- Configurable overflow policies (DROP_OLDEST, BLOCK, REJECT)
- Comprehensive statistics tracking
Thread safety: All operations are protected by ReentrantReadWriteLock for consistent views and atomic transactions.
- Since:
- 1.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumOverflow policy when the journal reaches capacity. -
Constructor Summary
ConstructorsConstructorDescriptionEventJournal(String journalName, int capacity, EventJournal.OverflowPolicy overflowPolicy, int subscriptionBufferCapacity) Create an event journal with custom configuration. -
Method Summary
Modifier and TypeMethodDescriptionlongAppend a new event to the journal.voidclear()Clear all entries from the journal.intcompact(RetentionPolicy policy) Compact the journal based on a retention policy.longGet the latest sequence number in the journal.longGet the oldest sequence number in the journal.getStats()Get overall journal statistics.intGet active subscription count.@Nullable SubscriptionStatsgetSubscriptionStats(String subscriptionId) Get statistics for a specific subscription.voidpauseSubscription(String subscriptionId) Pause a subscription.read(long fromSequence, int maxCount) Read entries from a starting sequence.readByEventType(String eventType, long fromSequence, int maxCount) Read entries filtered by event type.Read entries filtered by key.voidresumeSubscription(String subscriptionId) Resume a paused subscription.intsize()Get the current number of entries.subscribe(long fromSequence, EventListener listener) Subscribe to journal events starting from a sequence.voidunsubscribe(String subscriptionId) Unsubscribe from the journal.
-
Constructor Details
-
EventJournal
public EventJournal(String journalName, int capacity, EventJournal.OverflowPolicy overflowPolicy, int subscriptionBufferCapacity) Create an event journal with custom configuration.- Parameters:
journalName- The name of this journalcapacity- Maximum number of entriesoverflowPolicy- Overflow behavior at capacitysubscriptionBufferCapacity- Buffer size for each subscription- Throws:
IllegalArgumentException- if parameters are invalid
-
-
Method Details
-
append
Append a new event to the journal.- Parameters:
eventType- The event type classifierkey- Associated key (use empty string if no key)payload- Event payload as bytesmetadata- Event metadata- Returns:
- The sequence number assigned to this entry
- Throws:
IllegalStateException- if overflow policy is REJECT and journal is full
-
read
Read entries from a starting sequence.- Parameters:
fromSequence- Starting sequence (inclusive)maxCount- Maximum entries to return- Returns:
- List of entries (may be smaller than maxCount)
-
readByKey
Read entries filtered by key.- Parameters:
key- The key to filter byfromSequence- Starting sequence (inclusive)maxCount- Maximum entries to return- Returns:
- List of entries matching the key
-
readByEventType
Read entries filtered by event type.- Parameters:
eventType- The event type to filter byfromSequence- Starting sequence (inclusive)maxCount- Maximum entries to return- Returns:
- List of entries matching the event type
-
getLatestSequence
public long getLatestSequence()Get the latest sequence number in the journal.- Returns:
- The latest sequence, or -1 if empty
-
getOldestSequence
public long getOldestSequence()Get the oldest sequence number in the journal.- Returns:
- The oldest sequence, or -1 if empty
-
subscribe
Subscribe to journal events starting from a sequence.Returns immediately. The subscription will receive entries asynchronously via the listener callback.
- Parameters:
fromSequence- The starting sequencelistener- The event listener- Returns:
- The subscription ID (can be used to manage the subscription)
-
unsubscribe
Unsubscribe from the journal.- Parameters:
subscriptionId- The subscription ID
-
pauseSubscription
Pause a subscription.- Parameters:
subscriptionId- The subscription ID
-
resumeSubscription
Resume a paused subscription.- Parameters:
subscriptionId- The subscription ID
-
getSubscriptionStats
Get statistics for a specific subscription.- Parameters:
subscriptionId- The subscription ID- Returns:
- Subscription statistics, or null if not found
-
getStats
-
compact
Compact the journal based on a retention policy.Removes entries that do not pass the retention policy check. This is typically called periodically or in response to storage constraints.
- Parameters:
policy- The retention policy to apply- Returns:
- Number of entries removed
-
clear
public void clear()Clear all entries from the journal.This is a destructive operation and resets statistics.
-
size
public int size()Get the current number of entries.- Returns:
- Current size
-
getSubscriptionCount
public int getSubscriptionCount()Get active subscription count.- Returns:
- Number of active subscriptions
-