de.otto.synapse.subscription.StateRepositorySnapshotProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of synapse-core Show documentation
Show all versions of synapse-core Show documentation
A library used at otto.de to implement Spring Boot based event-sourcing microservices.
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 extends Message>> snapshot(final String entityId) {
return stateRepository.get(entityId)
.map((E t) -> entityToMessagesTransformer.apply(entityId, t).stream())
.orElse(Stream.empty());
}
}