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

io.atleon.core.AloQueueingTransformer Maven / Gradle / Ivy

package io.atleon.core;

import org.reactivestreams.Publisher;

import java.util.function.Function;
import java.util.function.Supplier;

/**
 * Transformer used on Publishers of items to create {@link Alo} items where acknowledgement is
 * "queued" such that acknowledgement order may be preserved independently of data processing
 * completion.
 *
 * @param  The type of items subscribed to
 * @param  The type of items extracted and contained in emitted Alo elements
 */
public final class AloQueueingTransformer implements Function, Publisher>> {

    private final Function groupExtractor;

    private final AcknowledgementQueueMode queueMode;

    private final AloQueueListener listener;

    private final AloComponentExtractor componentExtractor;

    private final AloFactory factory;

    private final long maxInFlight;

    private AloQueueingTransformer(
        Function groupExtractor,
        AcknowledgementQueueMode queueMode,
        AloQueueListener listener,
        AloComponentExtractor componentExtractor,
        AloFactory factory,
        long maxInFlight
    ) {
        this.groupExtractor = groupExtractor;
        this.queueMode = queueMode;
        this.listener = listener;
        this.componentExtractor = componentExtractor;
        this.factory = factory;
        this.maxInFlight = maxInFlight;
    }

    /**
     * Builds a new {@link AloQueueingTransformer} from the provided {@link AloComponentExtractor}.
     * The returned transformer will result in a single grouping of items, where acknowledgement is
     * executed in the same order as emission, and the number of in-flight messages is unbounded.
     *
     * @param componentExtractor Implements how to extract native {@link Alo} components
     * @return A new {@link AloQueueingTransformer}
     * @param  The type of items subscribed to
     * @param  The type of items extracted and contained in emitted Alo elements
     */
    public static  AloQueueingTransformer create(AloComponentExtractor componentExtractor) {
        return new AloQueueingTransformer<>(
            __ -> "singleton",
            AcknowledgementQueueMode.STRICT,
            AloQueueListener.noOp(),
            componentExtractor,
            ComposedAlo.factory(),
            Long.MAX_VALUE
        );
    }

    public AloQueueingTransformer withGroupExtractor(Function groupExtractor) {
        return new AloQueueingTransformer<>(groupExtractor, queueMode, listener, componentExtractor, factory, maxInFlight);
    }

    public AloQueueingTransformer withQueueMode(AcknowledgementQueueMode queueMode) {
        return new AloQueueingTransformer<>(groupExtractor, queueMode, listener, componentExtractor, factory, maxInFlight);
    }

    public AloQueueingTransformer withListener(AloQueueListener listener) {
        return new AloQueueingTransformer<>(groupExtractor, queueMode, listener, componentExtractor, factory, maxInFlight);
    }

    public AloQueueingTransformer withFactory(AloFactory factory) {
        return new AloQueueingTransformer<>(groupExtractor, queueMode, listener, componentExtractor, factory, maxInFlight);
    }

    public AloQueueingTransformer withMaxInFlight(long maxInFlight) {
        return new AloQueueingTransformer<>(groupExtractor, queueMode, listener, componentExtractor, factory, maxInFlight);
    }

    @Override
    public Publisher> apply(Publisher publisher) {
        return new AloQueueingOperator<>(
            publisher,
            groupExtractor,
            newQueueSupplier(queueMode),
            listener,
            componentExtractor,
            factory,
            maxInFlight
        );
    }

    private static Supplier newQueueSupplier(AcknowledgementQueueMode mode) {
        return () -> AcknowledgementQueue.create(mode);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy