Interface Journal


  • public interface Journal
    A Journal contains all the messages that where leading to the current state of a single event-sourced entity stored in a StateRepository or in some other kind of storage.

    Messages can come from different channels. The Journal will keep track not only of the messages for a single entity, but also from the originating channel for every message.

    The messages of a Journal will be stored in a MessageStore. The store must have a journal-key index, so the Journal is able to return the Stream of messages (more precisely MessageStoreEntry) for every entity stored in the {code StateRepository} when calling getJournalFor(String).

    In order to identify the messages for an entity, journalKeyOf(String) must return the key of the associated messages for a given entityId, which is the key of the stored entities: the key that would be used, for example, to get the entity from the StateRepository.

    A number of default implementations of the Journal interface can be created using the Journal helper Journals.

    Implementation hints:

    If you intend to write your own Journal, you need to make sure, that messages will be added to the journal's MessageStore, for example by registering JournalingInterceptor for every message-log receiver that has to be journaled. The journal's MessageStore must have a Index.JOURNAL_KEY.

    In most cases, the journal-key index will be calculated by just using the message's Key.partitionKey() as journal key. This is the case, if the partitionKey is used as primary key of your journaled entities.

    See Also:
    EIP: Message Store
    • Method Summary

      Modifier and Type Method Description
      com.google.common.collect.ImmutableList<java.lang.String> getJournaledChannels()
      The List of channels that take influence on this Journal.
      default java.util.stream.Stream<MessageStoreEntry> getJournalFor​(java.lang.String entityId)
      Returns the MessageStoreEntries containing all messages that where modifying the state of the entity identified by entityId.
      MessageStore getMessageStore()
      Returns the MessageStore used to store the messages of the journaled entities.
      java.lang.String getName()
      The name of the journal.
      default java.lang.String journalKeyOf​(java.lang.String entityId)
      Calculates the journal key for a given entityId.
    • Method Detail

      • getName

        java.lang.String getName()
        The name of the journal.

        In most cases, this will be equal to the name of the journaled StateRepository

        Returns:
        journal name
      • getJournaledChannels

        com.google.common.collect.ImmutableList<java.lang.String> getJournaledChannels()
        The List of channels that take influence on this Journal.

        For every channel, a JournalingInterceptor is auto-configured, if the Journal is registered as a Spring Bean. The interceptors will take care of adding incoming messages to the journal's MessageStore.

        Returns:
        list of channel names
      • getMessageStore

        MessageStore getMessageStore()
        Returns the MessageStore used to store the messages of the journaled entities.

        The store must be indexed for Index.JOURNAL_KEY. For every given entity, the journal-key must match the key returned by journalKeyOf(String)

        Returns:
        MessageStore
      • journalKeyOf

        default java.lang.String journalKeyOf​(java.lang.String entityId)
        Calculates the journal key for a given entityId.

        The returned key will be used to fetch the messages for the entity.

        The default implementation will simply return the entityId, assuming that the message's Key.partitionKey() will be used as a primary key for stored entites.

        Parameters:
        entityId - the key used to get the entity from a StateRepository or other entity store.
        Returns:
        key used to identify the messages for the entity
      • getJournalFor

        default java.util.stream.Stream<MessageStoreEntry> getJournalFor​(java.lang.String entityId)
        Returns the MessageStoreEntries containing all messages that where modifying the state of the entity identified by entityId.

        The default implementation assumes, that all stored messages can be found by calling getMessageStore().stream(Index.JOURNAL_KEY, journalKeyOf(entityId));.

        Parameters:
        entityId - the key of the entity. In most cases, this will be the partitionKey of the message.
        Returns:
        stream of MessageStoreEntry that have influenced the entity, ordered from oldest to youngest messages.