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

de.otto.synapse.messagestore.ChannelPositions 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.messagestore;

import com.google.common.collect.ImmutableSet;
import de.otto.synapse.channel.ChannelPosition;
import de.otto.synapse.channel.ShardPosition;

import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import static de.otto.synapse.channel.ChannelPosition.*;


class ChannelPositions {

    private final ConcurrentMap channelPositions = new ConcurrentHashMap<>();

    void updateFrom(final MessageStoreEntry entry) {
        channelPositions.compute(entry.getChannelName(), (key, existing) -> {
            final Optional shardPosition = entry
                    .getTextMessage()
                    .getHeader()
                    .getShardPosition();
            if (existing != null) {
                return shardPosition.map(s -> merge(existing, channelPosition(s))).orElse(existing);
            } else {
                return shardPosition.map(ChannelPosition::channelPosition).orElseGet(ChannelPosition::fromHorizon);
            }
        });
    }

    public ImmutableSet getChannelNames() {
        return ImmutableSet.copyOf(channelPositions.keySet());
    }

    public ChannelPosition getLatestChannelPosition(final String channelName) {
        return channelPositions.getOrDefault(channelName, fromHorizon());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy