de.otto.synapse.messagestore.redis.RedisRingBufferMessageStore Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of synapse-redis Show documentation
Show all versions of synapse-redis Show documentation
Support for Redis as a MessageStore.
package de.otto.synapse.messagestore.redis;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.annotations.Beta;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import de.otto.synapse.channel.ChannelPosition;
import de.otto.synapse.channel.ShardPosition;
import de.otto.synapse.messagestore.Index;
import de.otto.synapse.messagestore.MessageStore;
import de.otto.synapse.messagestore.MessageStoreEntry;
import de.otto.synapse.translator.*;
import org.slf4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.*;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import static de.otto.synapse.channel.ChannelPosition.channelPosition;
import static de.otto.synapse.channel.ShardPosition.fromPosition;
import static de.otto.synapse.translator.ObjectMappers.currentObjectMapper;
import static java.util.Arrays.asList;
import static java.util.Spliterators.spliteratorUnknownSize;
import static org.slf4j.LoggerFactory.getLogger;
/**
* Redis-based implementation of a WritableMessageStore.
*
*
* The store can be configured like a ring-buffer to only store the latest N messages.
*
*/
@Beta
public class RedisRingBufferMessageStore implements MessageStore {
private static final Logger LOG = getLogger(RedisRingBufferMessageStore.class);
private static final int CHARACTERISTICS = Spliterator.ORDERED | Spliterator.NONNULL | Spliterator.IMMUTABLE;
private final String name;
private final RedisTemplate redisTemplate;
private final int batchSize;
private final int maxSize;
private final Encoder encoder;
private final Decoder decoder;
/**
* @param name the name of the message store
* @param batchSize the size of the batches used to fetch messages from Redis
* @param ringBufferSize the maximum number of messages stored in the ring-buffer
* @param stringRedisTemplate the RedisTemplate used to access Redis
*/
public RedisRingBufferMessageStore(final String name,
final int batchSize,
final int ringBufferSize,
final RedisTemplate stringRedisTemplate) {
this(name, batchSize, ringBufferSize, stringRedisTemplate, new TextEncoder(MessageFormat.V2), new TextDecoder());
}
/**
* @param name the name of the message store
* @param batchSize the size of the batches used to fetch messages from Redis
* @param ringBufferSize the maximum number of messages stored in the ring-buffer
* @param stringRedisTemplate the RedisTemplate used to access Redis
* @param messageEncoder the encoder used to encode messages into the string-representation stored in Redis
* @param messageDecoder the decoder used to decode messages from the string-representation stored in Redis
*/
public RedisRingBufferMessageStore(final String name,
final int batchSize,
final int ringBufferSize,
final RedisTemplate stringRedisTemplate,
final Encoder messageEncoder,
final Decoder messageDecoder) {
this.name = name;
this.redisTemplate = stringRedisTemplate;
this.batchSize = batchSize;
this.maxSize = ringBufferSize;
this.encoder = messageEncoder;
this.decoder = messageDecoder;
}
@Override
@SuppressWarnings("unchecked")
public void add(final MessageStoreEntry entry) {
// This will contain the results of all ops in the transaction
final List