All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.otto.synapse.subscription.StateRepositorySnapshotProvider Maven / Gradle / Ivy

Go to download

A library used at otto.de to implement Spring Boot based event-sourcing microservices.

There is a newer version: 0.33.1
Show newest version
package de.otto.synapse.subscription;

import de.otto.synapse.message.Message;
import de.otto.synapse.state.StateRepository;

import java.util.stream.Stream;

import static java.util.Collections.singletonList;

/**
 * A SnapshotProvider that is relying on a {@link StateRepository} to generate the snapshot messages
 * for a given entity-id.
 * 

* Using an {@link EntityToMessageListTransformer}, the entities stored in the StateRepository can be * transformed into a sequence of messages. This is especially required, if the representation of the * entity does not match the payload of the snapshot messages. *

* @param */ public class StateRepositorySnapshotProvider implements SnapshotProvider { private final String channelName; private final StateRepository stateRepository; private final EntityToMessageListTransformer entityToMessagesTransformer; public StateRepositorySnapshotProvider(final String channelName, final StateRepository stateRepository) { this(channelName, stateRepository, (String key, E payload) -> singletonList(Message.message(key, payload))); } public StateRepositorySnapshotProvider(final String channelName, final StateRepository stateRepository, final EntityToMessageListTransformer entityToMessagesTransformer) { this.channelName = channelName; this.stateRepository = stateRepository; this.entityToMessagesTransformer = entityToMessagesTransformer; } @Override public String channelName() { return channelName; } @Override public Stream> snapshot(final String entityId) { return stateRepository.get(entityId) .map((E t) -> entityToMessagesTransformer.apply(entityId, t).stream()) .orElse(Stream.empty()); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy