Class InMemoryMessageStore
- java.lang.Object
-
- net.sf.eBus.feed.historic.store.InMemoryMessageStore
-
- All Implemented Interfaces:
IEMessageStore
public final class InMemoryMessageStore extends Object implements IEMessageStore
This class implementsIEMessageStoreproviding the ability to store events to and retrieve events from an in-memoryeBus 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 byIEMessageStore) 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 anEHistoricPublishFeedwhich guarantees that notification messages passed tostore(ENotificationMessage)match the store's this store's message key. Thereforestoredoes 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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classInMemoryMessageStore.BuilderInMemoryMessageStoreinstances are created using aBuilder.
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_STORE_CAPACITYMessage store default initial capacity is 1000.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static InMemoryMessageStore.Builderbuilder()Returns a new in-memory message store builder instance.intcapacity()Returns in-memory message store maximum allowed capacity.voidclear()Resets in-memory message store to its initial state with the notification message store array filled withnullvalues.InstantfirstTimestamp()Returns currently first notification message timestamp as anInstant.booleanisOpen()Returnstruesince this in-memory message store is always open.EMessageKeykey()Returns message key associated with message store.InstantlastTimestamp()Returns currently last notification message timestamp as anInstant.Collection<ENotificationMessage>retrieve(int numMessages)Returns up tonumMessageslast messages placed into message store.Collection<ENotificationMessage>retrieve(EInterval interval)Retrieves historic messages as defined by given date/time interval.intsize()Returns current in-memory message store size.voidstore(ENotificationMessage message)Inserts given message into in-memory message store.StringtoString()Returns text containing in-memory message store's current size, maximum allowed size, first and last message timestamps.
-
-
-
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()
Returnstruesince this in-memory message store is always open.- Specified by:
isOpenin interfaceIEMessageStore- Returns:
open.
-
key
public EMessageKey key()
Description copied from interface:IEMessageStoreReturns message key associated with message store.- Specified by:
keyin interfaceIEMessageStore- 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 withmessage.Note: this method does not validate if
messsage's key matches the store's configured message key. This is so thatPublishStatusEventmessages may be stored. This means this message store may be corrupted with invalid messages.EHistoricPublishFeedguarantees this will not happen if it has sole access to this message store.- Specified by:
storein interfaceIEMessageStore- 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:IEMessageStoreRetrieves historic messages as defined by given date/time interval. Returned collection may be empty but nevernull.Please note that implementation are not required to validate
intervalsince this is performed byEHistoricPublishFeed. That said, such validation is encourage if store is used outside of eBus historic feed framework.- Specified by:
retrievein interfaceIEMessageStore- Parameters:
interval- date/time interval.- Returns:
- iterator over historic messages. Does not
return
nullbut may return iterator whereIterator.hasNext()returnsfalse. - 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.
-
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 anInstant. If there are no notification messages stored, then returnsnull.- Returns:
- first notification message's timestamp.
-
lastTimestamp
@Nullable public Instant lastTimestamp()
Returns currently last notification message timestamp as anInstant. If there are no notification messages stored, then returnsnull.- Returns:
- last notification message's timestamp.
-
retrieve
public Collection<ENotificationMessage> retrieve(int numMessages)
Returns up tonumMessageslast messages placed into message store. If there are fewer thannumMessagesstored, then returns all returned messages. Returned list may be empty but will not benull.Unlike
retrieve(EInterval), this method validates thatnumMessagesis > zero.- Parameters:
numMessages- Return up to this many store notification messages.- Returns:
- eBus notification list.
- Throws:
IllegalArgumentException- ifnumMessages≤ 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 withnullvalues.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.
-
builder
public static InMemoryMessageStore.Builder builder()
Returns a new in-memory message store builder instance.- Returns:
- new in-memory message store
InMemoryMessageStore.Builderinstance.
-
-