Class JournalSubscription
java.lang.Object
com.loomcache.server.journal.JournalSubscription
Manages a single journal subscription with backpressure and lifecycle control.
Features:
- Bounded backpressure buffer for entries
- Subscribe/unsubscribe/pause/resume lifecycle
- Stats tracking (delivered, pending, lag)
- Thread-safe operations
- Since:
- 1.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final class -
Constructor Summary
ConstructorsConstructorDescriptionJournalSubscription(String subscriptionId, EventListener listener, int bufferCapacity) Create a new journal subscription. -
Method Summary
Modifier and TypeMethodDescriptionvoidbufferDuringReplay(JournalEntry entry) Buffer a live entry that arrived during replay.voidclose()Close this subscription (permanent; cannot be resumed).voidMark replay as complete and drain any entries that arrived during replay.longGet the sequence of the last delivered entry.intGet the current number of pending entries in the buffer.getStats(long journalLatestSequence) Get current subscription statistics.booleanisClosed()Check if the subscription is closed.booleanisPaused()Check if the subscription is paused.booleanCheck if this subscription is currently replaying historical entries.voidmarkDelivered(JournalEntry entry) Mark an entry as delivered and update stats.booleanoffer(JournalEntry entry) Try to offer an entry to this subscription's buffer.voidpause()Pause this subscription (no new entries will be delivered).@Nullable JournalEntrypoll()Poll the next entry from this subscription's buffer without blocking.voidresume()Resume this subscription.voidMark this subscription as replaying.
-
Constructor Details
-
JournalSubscription
Create a new journal subscription.- Parameters:
subscriptionId- The unique subscription identifierlistener- The event listener for this subscriptionbufferCapacity- Capacity of the backpressure buffer- Throws:
IllegalArgumentException- if parameters are invalid
-
-
Method Details
-
offer
Try to offer an entry to this subscription's buffer.Returns false if:
- The subscription is paused
- The subscription is closed
- The buffer is full
- Parameters:
entry- The entry to offer- Returns:
- true if successfully added to buffer, false otherwise
-
poll
Poll the next entry from this subscription's buffer without blocking.- Returns:
- The next entry, or null if buffer is empty
-
markDelivered
Mark an entry as delivered and update stats.- Parameters:
entry- The delivered entry
-
getLastDeliveredSequence
public long getLastDeliveredSequence()Get the sequence of the last delivered entry.- Returns:
- last delivered sequence, or -1 if no entries have been delivered
-
isPaused
public boolean isPaused()Check if the subscription is paused.- Returns:
- true if paused
-
pause
public void pause()Pause this subscription (no new entries will be delivered). -
resume
public void resume()Resume this subscription. -
isClosed
public boolean isClosed()Check if the subscription is closed.- Returns:
- true if closed
-
close
public void close()Close this subscription (permanent; cannot be resumed). -
getStats
Get current subscription statistics.- Parameters:
journalLatestSequence- The latest sequence in the journal- Returns:
- Subscription statistics
-
getPendingCount
public int getPendingCount()Get the current number of pending entries in the buffer.- Returns:
- Number of pending entries
-
startReplay
public void startReplay()Mark this subscription as replaying. While replaying, live entries are buffered instead of delivered immediately to prevent out-of-order delivery. -
isReplaying
public boolean isReplaying()Check if this subscription is currently replaying historical entries.- Returns:
- true if replay is in progress
-
bufferDuringReplay
Buffer a live entry that arrived during replay. These entries will be drained and delivered in order once replay completes.- Parameters:
entry- the live entry to buffer
-
completeReplay
public void completeReplay()Mark replay as complete and drain any entries that arrived during replay. Entries are delivered in sequence order to the listener.
-