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

de.otto.synapse.endpoint.receiver.aws.KinesisMessageLogResponse Maven / Gradle / Ivy

Go to download

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

There is a newer version: 0.10.1
Show newest version
package de.otto.synapse.endpoint.receiver.aws;

import com.google.common.collect.ImmutableList;
import de.otto.synapse.channel.ChannelDurationBehind;
import de.otto.synapse.channel.ChannelPosition;
import de.otto.synapse.consumer.MessageConsumer;
import de.otto.synapse.consumer.MessageDispatcher;
import de.otto.synapse.message.Message;
import de.otto.synapse.translator.MessageTranslator;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static de.otto.synapse.channel.ChannelPosition.channelPosition;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;

public class KinesisMessageLogResponse {

    private final String channelName;
    private final ImmutableList shardResponses;

    public KinesisMessageLogResponse(final String channelName,
                                     final ImmutableList shardResponses) {
        if (shardResponses.isEmpty()) {
            throw new IllegalArgumentException("Unable to create KinesisMessageLogResponse without KinesisShardResponses");
        }
        if (shardResponses.stream().anyMatch(response -> !response.getChannelName().equals(channelName))) {
            throw new IllegalArgumentException("Unable to create KinesisMessageLogResponse from KinesisShardResponses returned by different message channels");
        }        this.channelName = channelName;
        this.shardResponses = shardResponses;
    }

    public String getChannelName() {
        return channelName;
    }

    public ChannelDurationBehind getChannelDurationBehind() {
        final ChannelDurationBehind.Builder durationBehind = ChannelDurationBehind.channelDurationBehind();
        shardResponses.forEach((response) -> durationBehind.with(response.getShardName(), response.getDurationBehind()));
        return durationBehind.build();
    }

    public List> getMessages() {
        return shardResponses
                .stream()
                .flatMap(response -> response.getMessages().stream())
                .collect(toList());
    }

    /**
     * Translates the messages from the response into messages with {@link Message#getPayload() payload} of type
     * <P> and returns the translated messages as a list.
     *
     * @param messageTranslator the {@link MessageTranslator} used to translate the message payload
     * @param 

the type of the returned messages payload * @return list of messages with payload type <P> */ public

List> getMessages(final MessageTranslator

messageTranslator) { return shardResponses .stream() .flatMap(response -> response.getMessages().stream().map(messageTranslator::translate)) .collect(toList()); } /** * Dispatches all messages from the response to some {@link MessageConsumer}. *

* The {@link MessageDispatcher} can be used to consume and transform the messages into messages * with some custom payload and dispatch these messages to one or more {@code MessageConsumers}: *

*

     *     final KinesisMessageLogResponse response = kinesisMessageLogReader.read(iterator).get();
     *     final MessageConsumer<TestPayload> consumer = someTestMessageConsumer();
     *     response.dispatchMessages(new MessageDispatcher(new ObjectMapper(), singletonList(consumer)));
     * 
* @param messageConsumer the MessageConsumer */ public void dispatchMessages(final MessageConsumer messageConsumer) { shardResponses .stream() .flatMap(response -> response.getMessages().stream()) .forEach(messageConsumer); } public Set getShardNames() { return shardResponses .stream() .map(KinesisShardResponse::getShardName) .collect(toSet()); } public ImmutableList getShardResponses() { return shardResponses; } public ChannelPosition getChannelPosition() { return channelPosition(shardResponses .stream() .map(KinesisShardResponse::getShardPosition) .collect(Collectors.toList())); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy