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

de.otto.synapse.endpoint.sender.TeeMessageSender 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.endpoint.sender;

import com.google.common.collect.ImmutableList;
import de.otto.synapse.message.Message;
import jakarta.annotation.Nonnull;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Stream;

/**
 * A MessageSender that is sending all messages to N delegate MessageSenders.
 */
public class TeeMessageSender implements MessageSender {

    @Nonnull
    private final ImmutableList endpoints;

    public TeeMessageSender(final @Nonnull ImmutableList endpoints) {
        this.endpoints = endpoints;
    }

    public TeeMessageSender(final @Nonnull List endpoints) {
        this.endpoints = ImmutableList.copyOf(endpoints);
    }

    public TeeMessageSender(final @Nonnull MessageSender... endpoints) {
        this.endpoints = ImmutableList.copyOf(endpoints);
    }

    @Override
    public CompletableFuture send(@Nonnull Message message) {
        return CompletableFuture.allOf(endpoints
                .stream()
                .map(sender -> sender.send(message))
                .toArray(CompletableFuture[]::new));
    }

    @Override
    public CompletableFuture sendBatch(@Nonnull Stream> batch) {
        return CompletableFuture.allOf(endpoints
                .stream()
                .map(sender -> sender.sendBatch(batch))
                .toArray(CompletableFuture[]::new));
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy