Class InMemoryMessageStore

  • All Implemented Interfaces:
    IEMessageStore

    public final class InMemoryMessageStore
    extends Object
    implements IEMessageStore
    This class implements IEMessageStore providing the ability to store events to and retrieve events from an in-memory eBus notification message array. This array is fixed length and implemented as a ring buffer. When the in-memory message store reaches its maximum allowed size, the oldest message is overwritten with the new message.

    In-memory message store supports message retrieval based on an EInterval (as required by IEMessageStore) and the last n messages stored. The later is not supported by eBus historic feed but provided for application use.

    Please not that this message store does not persist messages. When this store is closed, then stored messages are discarded.

    Please note that this message store is homogenous - meaning that all notification messages have the same key (excepting when PublishStatusEvents are stored). This store is designed to work within an EHistoricPublishFeed which guarantees that notification messages passed to store(ENotificationMessage) match the store's this store's message key. Therefore store does not validate the given notification message's key to see if it matches the configured key. In short, this in-memory message store is not designed for independent use but as part of eBus historic message feed subsystem.

    Author:
    Charles W. Rapp
    See Also:
    SqlMessageStore
    • Field Detail

      • DEFAULT_STORE_CAPACITY

        public static final int DEFAULT_STORE_CAPACITY
        Message store default initial capacity is 1000.
        See Also:
        Constant Field Values
    • Method Detail

      • isOpen

        public boolean isOpen()
        Returns true since this in-memory message store is always open.
        Specified by:
        isOpen in interface IEMessageStore
        Returns:
        open.
      • key

        public EMessageKey key()
        Description copied from interface: IEMessageStore
        Returns message key associated with message store.
        Specified by:
        key in interface IEMessageStore
        Returns:
        message store's associated key.
      • store

        public void store​(ENotificationMessage message)
        Inserts given message into in-memory message store. If message store has reached configured capacity, then oldest store message is replaced with message.

        Note: this method does not validate if messsage's key matches the store's configured message key. This is so that PublishStatusEvent messages may be stored. This means this message store may be corrupted with invalid messages. EHistoricPublishFeed guarantees this will not happen if it has sole access to this message store.

        Specified by:
        store in interface IEMessageStore
        Parameters:
        message - store this message into in-memory message store.
        See Also:
        retrieve(EInterval), retrieve(int)
      • retrieve

        public Collection<ENotificationMessage> retrieve​(EInterval interval)
        Description copied from interface: IEMessageStore
        Retrieves historic messages as defined by given date/time interval. Returned collection may be empty but never null.

        Please note that implementation are not required to validate interval since this is performed by EHistoricPublishFeed. That said, such validation is encourage if store is used outside of eBus historic feed framework.

        Specified by:
        retrieve in interface IEMessageStore
        Parameters:
        interval - date/time interval.
        Returns:
        iterator over historic messages. Does not return null but may return iterator where Iterator.hasNext() returns false.
        See Also:
        IEMessageStore.store(ENotificationMessage)
      • toString

        public String toString()
        Returns text containing in-memory message store's current size, maximum allowed size, first and last message timestamps.
        Overrides:
        toString in class Object
        Returns:
        in-memory message store stats as text.
      • size

        public int size()
        Returns current in-memory message store size. Returned value is ≥ zero.
        Returns:
        current in-memory message store size.
      • capacity

        public int capacity()
        Returns in-memory message store maximum allowed capacity.
        Returns:
        in-memory message store maximum allowed capacity.
      • firstTimestamp

        @Nullable
        public Instant firstTimestamp()
        Returns currently first notification message timestamp as an Instant. If there are no notification messages stored, then returns null.
        Returns:
        first notification message's timestamp.
      • lastTimestamp

        @Nullable
        public Instant lastTimestamp()
        Returns currently last notification message timestamp as an Instant. If there are no notification messages stored, then returns null.
        Returns:
        last notification message's timestamp.
      • retrieve

        public Collection<ENotificationMessage> retrieve​(int numMessages)
        Returns up to numMessages last messages placed into message store. If there are fewer than numMessages stored, then returns all returned messages. Returned list may be empty but will not be null.

        Unlike retrieve(EInterval), this method validates that numMessages is > zero.

        Parameters:
        numMessages - Return up to this many store notification messages.
        Returns:
        eBus notification list.
        Throws:
        IllegalArgumentException - if numMessages ≤ zero.
        See Also:
        retrieve(EInterval)
      • clear

        public void clear()
        Resets in-memory message store to its initial state with the notification message store array filled with null values.

        Note: this method should be called from within an eBus dispatcher thread to prevent this message store from being accessed at the same time as message store or retrieval.