Class InMemoryScheduleStore

java.lang.Object
io.fluxzero.sdk.tracking.client.InMemoryMessageStore
io.fluxzero.sdk.scheduling.client.InMemoryScheduleStore
All Implemented Interfaces:
Monitored<List<SerializedMessage>>, HasMessageStore, MessageStore, SchedulingClient, AutoCloseable

public class InMemoryScheduleStore extends InMemoryMessageStore implements SchedulingClient
An in-memory implementation of a scheduling store that allows the scheduling, retrieval, and management of scheduled messages. It extends `InMemoryMessageStore` to reuse the functionalities for storing and managing messages and implements `SchedulingClient` to support scheduling-specific operations.

This implementation provides thread-safe mechanisms for scheduling, retrieving, and cancelling messages. Messages are scheduled to be processed at specific timestamps, with support for expiration and filtering of schedules.

  • Constructor Details

    • InMemoryScheduleStore

      public InMemoryScheduleStore()
    • InMemoryScheduleStore

      public InMemoryScheduleStore(Duration messageExpiration)
    • InMemoryScheduleStore

      public InMemoryScheduleStore(Duration messageExpiration, Clock clock)
  • Method Details

    • append

      public CompletableFuture<Void> append(SerializedMessage... messages)
      Description copied from interface: MessageStore
      Appends the given messages to the store.
      Specified by:
      append in interface MessageStore
      Parameters:
      messages - messages to append
      Returns:
      a CompletableFuture that completes when the messages have been successfully appended
    • schedule

      public CompletableFuture<Void> schedule(Guarantee guarantee, SerializedSchedule... schedules)
      Description copied from interface: SchedulingClient
      Schedule one or more serialized schedules with a specified Guarantee.
      Specified by:
      schedule in interface SchedulingClient
      Parameters:
      guarantee - Delivery guarantee to apply (e.g., none, sent, stored).
      schedules - One or more schedules to register.
      Returns:
      A future that completes when the scheduling is acknowledged.
    • cancelSchedule

      public CompletableFuture<Void> cancelSchedule(String scheduleId, Guarantee guarantee)
      Description copied from interface: SchedulingClient
      Cancel a scheduled message using the provided delivery guarantee.
      Specified by:
      cancelSchedule in interface SchedulingClient
      Parameters:
      scheduleId - The identifier of the schedule to cancel.
      guarantee - Delivery guarantee for the cancellation request.
      Returns:
      A future that completes when the cancellation is processed.
    • getSchedule

      public SerializedSchedule getSchedule(String scheduleId)
      Description copied from interface: SchedulingClient
      Retrieves the serialized schedule associated with the given ID.
      Specified by:
      getSchedule in interface SchedulingClient
      Parameters:
      scheduleId - The ID of the schedule to retrieve.
      Returns:
      The matching SerializedSchedule, or null if none is found.
    • append

      public CompletableFuture<Void> append(List<SerializedMessage> messages)
      Description copied from interface: MessageStore
      Appends a list of messages to the store.
      Specified by:
      append in interface MessageStore
      Overrides:
      append in class InMemoryMessageStore
      Parameters:
      messages - messages to append
      Returns:
      a CompletableFuture that completes when the messages have been successfully appended
    • getBatch

      public List<SerializedMessage> getBatch(Long minIndex, int maxSize, boolean inclusive)
      Returns a batch of schedules that are due for delivery.

      Unlike a regular message store, this schedule store only exposes entries whose deadline has passed and whose schedule id is still active.

      Specified by:
      getBatch in interface MessageStore
      Overrides:
      getBatch in class InMemoryMessageStore
      Parameters:
      minIndex - minimum message index to start from
      maxSize - maximum number of messages to retrieve
      inclusive - whether to include the message at minIndex
      Returns:
      a list of SerializedMessage instances
    • getBatch

      public List<SerializedMessage> getBatch(Long minIndex, int maxSize, boolean inclusive, boolean includeFuture)
      Returns a batch of schedules from the active schedule index.

      When includeFuture is false, only schedules whose deadline has passed are returned. When it is true, future schedules are also included. This is used by the test server to discover the next hidden deadline and wake waiting trackers as soon as that deadline expires.

    • scanBatch

      public MessageStoreBatch scanBatch(Long minIndex, int maxSize, boolean inclusive, long maxBytes, Predicate<? super SerializedMessage> filter)
      Description copied from interface: MessageStore
      Scans messages starting from the given minIndex, returning messages accepted by filter and metadata about the unfiltered source scan.

      maxSize limits the number of source messages scanned. maxBytes limits the cumulative serialized payload bytes of accepted messages. If the first accepted message is larger than maxBytes, it is still returned so consumers can make progress.

      Specified by:
      scanBatch in interface MessageStore
      Overrides:
      scanBatch in class InMemoryMessageStore
      Parameters:
      minIndex - minimum message index to start from
      maxSize - maximum number of source messages to scan
      inclusive - whether to include the message at minIndex
      maxBytes - maximum number of accepted serialized payload bytes, or 0 for no byte limit
      filter - predicate deciding which messages enter the returned batch
      Returns:
      accepted messages and source scan metadata
    • setClock

      public void setClock(@NonNull @NonNull Clock clock)
    • clockChanged

      protected void clockChanged()
    • truncate

      public void truncate()
      Description copied from interface: MessageStore
      Removes all messages from this store while keeping the logical log available for future appends.
      Specified by:
      truncate in interface MessageStore
      Overrides:
      truncate in class InMemoryMessageStore
    • getFutureSchedules

      public List<Schedule> getFutureSchedules(Serializer serializer)
    • removeExpiredSchedules

      public List<Schedule> removeExpiredSchedules(Serializer serializer)
    • asList

      protected List<Schedule> asList(Map<Long,String> scheduleIdsByIndex, Serializer serializer)
    • purgeExpiredMessages

      protected void purgeExpiredMessages(Duration messageExpiration)
      Overrides:
      purgeExpiredMessages in class InMemoryMessageStore
    • toString

      public String toString()
      Overrides:
      toString in class InMemoryMessageStore
    • close

      public void close()
      Description copied from interface: MessageStore
      Default no-op close method. Override if resources need cleanup.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface MessageStore
      Specified by:
      close in interface SchedulingClient
      Overrides:
      close in class InMemoryMessageStore