Class CachingTrackingClient
- All Implemented Interfaces:
TrackingClient
,AutoCloseable
TrackingClient
implementation that wraps another client (typically a WebsocketTrackingClient
)
and caches recent messages in memory to reduce redundant round trips to the Flux platform.
This client is particularly useful in environments where multiple consumers or trackers are processing the same stream of messages. Rather than each tracker reading from the backend individually, a shared in-memory cache serves recent messages directly when possible.
Behavior
- Internally starts a special tracker that continuously appends new messages to a bounded in-memory cache.
- Trackers that read from this client are first served from the local cache when possible.
- Falls back to the delegate
TrackingClient
for uncached or missed messages. - Trackers waiting for new messages are notified via scheduled polling or real-time cache updates.
- Cache size is limited via
maxCacheSize
; old messages are evicted in insertion order.
Use Cases
- Optimizing performance when many trackers are polling the same stream concurrently
- Reducing network latency and load on the Flux platform for high-volume message types
- Minimizing end-to-end processing delay in horizontally scaled applications
Tracking Mechanics
- Uses a background tracker configured with
ignoreSegment = true
andclientControlledIndex = true
to stream all new messages into the cache. - Trackers calling
read(java.lang.String, java.lang.Long, io.fluxcapacitor.javaclient.tracking.ConsumerConfiguration)
are served from the cache if theirlastIndex
is already present. - If not, the delegate is queried directly to maintain completeness.
Thread Safety
- The cache is backed by a
ConcurrentSkipListMap
for safe concurrent access. - Eviction and tracker notifications are synchronized to prevent race conditions.
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected void
cacheNewMessages
(List<SerializedMessage> messages) claimSegment
(String trackerId, Long lastIndex, ConsumerConfiguration config) Claims a processing segment for the given tracker.void
close()
Closes any open resources associated with this client.disconnectTracker
(String consumer, String trackerId, boolean sendFinalEmptyBatch, Guarantee guarantee) Disconnects the specified tracker from its segment with the specified delivery guarantee.protected List
<SerializedMessage> filterMessages
(List<SerializedMessage> messages, int[] segmentRange, Position position, ConsumerConfiguration config) protected MessageBatch
getMessageBatch
(ConsumerConfiguration config, long minIndex, ClaimSegmentResult claim) Returns theMessageType
(e.g., COMMAND, EVENT, QUERY) associated with this tracking client.getPosition
(String consumer) Returns the current committed tracking position for the given consumer.getTopic()
Returns the topic associated with this tracking client.read
(String trackerId, Long lastIndex, ConsumerConfiguration config) Asynchronously reads the next availableMessageBatch
for a given tracker.readFromIndex
(long minIndex, int maxSize) Fetches messages starting from the given index up to the provided max size.protected void
resetPosition
(String consumer, long lastIndex, Guarantee guarantee) Resets the consumer's tracking position to a given index with a specific delivery guarantee.storePosition
(String consumer, int[] segment, long lastIndex, Guarantee guarantee) Stores the last successfully processed position for a consumer with a specific delivery guarantee.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.fluxcapacitor.javaclient.tracking.client.TrackingClient
disconnectTracker, readAndWait, resetPosition, storePosition
-
Constructor Details
-
CachingTrackingClient
-
-
Method Details
-
read
public CompletableFuture<MessageBatch> read(String trackerId, Long lastIndex, ConsumerConfiguration config) Description copied from interface:TrackingClient
Asynchronously reads the next availableMessageBatch
for a given tracker.- Specified by:
read
in interfaceTrackingClient
- Parameters:
trackerId
- the unique ID for the tracker thread requesting messageslastIndex
- the last index successfully handled by this trackerconfig
- the full configuration for the consumer- Returns:
- a
CompletableFuture
that completes with the next batch of messages
-
getMessageBatch
protected MessageBatch getMessageBatch(ConsumerConfiguration config, long minIndex, ClaimSegmentResult claim) -
filterMessages
protected List<SerializedMessage> filterMessages(List<SerializedMessage> messages, int[] segmentRange, Position position, ConsumerConfiguration config) -
cacheNewMessages
-
removeOldMessages
protected void removeOldMessages() -
readFromIndex
Description copied from interface:TrackingClient
Fetches messages starting from the given index up to the provided max size.This method bypasses consumer configurations and is primarily used for diagnostics or reprocessing.
- Specified by:
readFromIndex
in interfaceTrackingClient
- Parameters:
minIndex
- the starting index (inclusive)maxSize
- the maximum number of messages to retrieve- Returns:
- a list of serialized messages starting at the given index
-
claimSegment
public CompletableFuture<ClaimSegmentResult> claimSegment(String trackerId, Long lastIndex, ConsumerConfiguration config) Description copied from interface:TrackingClient
Claims a processing segment for the given tracker.Segments are used to partition the message log among multiple tracker threads for parallel processing.
- Specified by:
claimSegment
in interfaceTrackingClient
- Parameters:
trackerId
- the unique identifier of the tracker attempting to claim a segmentlastIndex
- the tracker's last successfully processed indexconfig
- the full consumer configuration- Returns:
- a
CompletableFuture
resolving to the result of the claim
-
storePosition
public CompletableFuture<Void> storePosition(String consumer, int[] segment, long lastIndex, Guarantee guarantee) Description copied from interface:TrackingClient
Stores the last successfully processed position for a consumer with a specific delivery guarantee.- Specified by:
storePosition
in interfaceTrackingClient
- Parameters:
consumer
- the name of the consumersegment
- the segment the tracker is processinglastIndex
- the last message index processedguarantee
- delivery guarantee (e.g., STORED, SENT)- Returns:
- a future indicating completion
-
resetPosition
Description copied from interface:TrackingClient
Resets the consumer's tracking position to a given index with a specific delivery guarantee.- Specified by:
resetPosition
in interfaceTrackingClient
- Parameters:
consumer
- the name of the consumerlastIndex
- the new index to start fromguarantee
- the delivery guarantee- Returns:
- a future indicating completion
-
getPosition
Description copied from interface:TrackingClient
Returns the current committed tracking position for the given consumer.- Specified by:
getPosition
in interfaceTrackingClient
- Parameters:
consumer
- the name of the consumer- Returns:
- the last known committed position
-
disconnectTracker
public CompletableFuture<Void> disconnectTracker(String consumer, String trackerId, boolean sendFinalEmptyBatch, Guarantee guarantee) Description copied from interface:TrackingClient
Disconnects the specified tracker from its segment with the specified delivery guarantee.- Specified by:
disconnectTracker
in interfaceTrackingClient
- Parameters:
consumer
- the name of the consumer grouptrackerId
- the ID of the tracker thread being disconnectedsendFinalEmptyBatch
- whether to send a final empty batch to commit stateguarantee
- the delivery guarantee to use- Returns:
- a future indicating disconnection
-
getMessageType
Description copied from interface:TrackingClient
Returns theMessageType
(e.g., COMMAND, EVENT, QUERY) associated with this tracking client.- Specified by:
getMessageType
in interfaceTrackingClient
- Returns:
- the message type
-
getTopic
Description copied from interface:TrackingClient
Returns the topic associated with this tracking client.This is applicable only when
TrackingClient.getMessageType()
isMessageType.DOCUMENT
orMessageType.CUSTOM
, where messages are organized into named topics beyond the standard type-based categorization.For other
MessageType
s (e.g.,COMMAND
,EVENT
), the concept of a topic is implicit and not required for tracking.- Specified by:
getTopic
in interfaceTrackingClient
- Returns:
- the topic name, or
null
if not applicable for the message type
-
close
public void close()Description copied from interface:TrackingClient
Closes any open resources associated with this client.Once closed, the client should no longer be used to fetch or commit tracking state.
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceTrackingClient
-