Search in sources :

Example 1 with BackPressureStrategy

use of io.smallrye.mutiny.subscription.BackPressureStrategy in project smallrye-reactive-messaging by smallrye.

the class ThrowingEmitter method create.

public static <T> Multi<T> create(Consumer<MultiEmitter<? super T>> deferred, long bufferSize) {
    // ThrowingEmitter works by wrapping around a delegate emitter and tracking the requests from downstream so that it can throw an exception from emit() if there aren't sufficient requests
    // If there's no buffer we can use IGNORE since we do our own counting of requests, otherwise we need the delegate to buffer requests for us
    BackPressureStrategy backPressureStrategy = bufferSize == 0 ? BackPressureStrategy.IGNORE : BackPressureStrategy.BUFFER;
    // Use deferred so that we can add a separate on request callback for each subscriber
    return Multi.createFrom().deferred(() -> {
        ThrowingEmitter<T> throwingEmitter = new ThrowingEmitter<>(bufferSize);
        // When someone subscribes, wrap the emitter with our throwing emitter
        Consumer<MultiEmitter<? super T>> consumer = emitter -> {
            throwingEmitter.delegate = emitter;
            deferred.accept(throwingEmitter);
        };
        // Create the Multi and attach the request callback
        return Multi.createFrom().emitter(consumer, backPressureStrategy).onRequest().invoke(throwingEmitter::request);
    });
}
Also used : Consumer(java.util.function.Consumer) Subscriptions(io.smallrye.mutiny.helpers.Subscriptions) AtomicLong(java.util.concurrent.atomic.AtomicLong) ProviderExceptions.ex(io.smallrye.reactive.messaging.providers.i18n.ProviderExceptions.ex) MultiEmitter(io.smallrye.mutiny.subscription.MultiEmitter) BackPressureStrategy(io.smallrye.mutiny.subscription.BackPressureStrategy) Multi(io.smallrye.mutiny.Multi) BackPressureStrategy(io.smallrye.mutiny.subscription.BackPressureStrategy) MultiEmitter(io.smallrye.mutiny.subscription.MultiEmitter)

Aggregations

Multi (io.smallrye.mutiny.Multi)1 Subscriptions (io.smallrye.mutiny.helpers.Subscriptions)1 BackPressureStrategy (io.smallrye.mutiny.subscription.BackPressureStrategy)1 MultiEmitter (io.smallrye.mutiny.subscription.MultiEmitter)1 ProviderExceptions.ex (io.smallrye.reactive.messaging.providers.i18n.ProviderExceptions.ex)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Consumer (java.util.function.Consumer)1