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);
});
}
Aggregations