java.lang.Object
io.smallrye.mutiny.subscription.DemandPauser
@Experimental("This API is still being designed and may change in the future")
public class DemandPauser
extends Object
A handle to control a pausable stream without holding a direct reference to the stream itself.
This handle allows pausing, resuming, and inspecting the state of a pausable stream from anywhere in the application, even after the stream has been transformed or subscribed to.
Example usage:
DemandPauser pauser = new DemandPauser();
Multi.createFrom().range(0, 100)
.pauseDemand().using(pauser)
.onItem().call(i -> Uni.createFrom().nullItem()
.onItem().delayIt().by(Duration.ofMillis(10)))
.onItem().transform(i -> i * 2)
.subscribe().with(System.out::println);
// Control from anywhere
pauser.pause();
pauser.resume();
System.out.println("Paused: " + pauser.isPaused());
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidbind(PausableMulti multi) Binds this handle to a pausable channel.intReturns the current buffer size (number of items in the buffer).booleanClears the buffer if the stream is currently paused.booleanisBound()Checks if this handle is bound to a channel.booleanisPaused()Checks if the stream is currently paused.voidpause()Pauses the stream.voidresume()Resumes the stream.
-
Constructor Details
-
DemandPauser
public DemandPauser()
-
-
Method Details
-
bind
Binds this handle to a pausable channel. This is typically called internally when creating a pausable stream.- Parameters:
multi- the pausable channel to bind to
-
pause
public void pause()Pauses the stream. Already requested items will be handled according to the configured buffer strategy.- Throws:
IllegalStateException- if the handle is not bound to a channel
-
resume
public void resume()Resumes the stream. Buffered items (if any) will be delivered before new items are requested.- Throws:
IllegalStateException- if the handle is not bound to a channel
-
isPaused
public boolean isPaused()Checks if the stream is currently paused.- Returns:
trueif paused,falseotherwise- Throws:
IllegalStateException- if the handle is not bound to a channel
-
bufferSize
public int bufferSize()Returns the current buffer size (number of items in the buffer). Only applicable when using BUFFER strategy.- Returns:
- the number of buffered items
- Throws:
IllegalStateException- if the handle is not bound to a channel
-
clearBuffer
public boolean clearBuffer()Clears the buffer if the stream is currently paused. Only applicable when using BUFFER strategy.- Returns:
trueif the buffer was cleared,falseif not paused or no buffer- Throws:
IllegalStateException- if the handle is not bound to a channel
-
isBound
public boolean isBound()Checks if this handle is bound to a channel.- Returns:
trueif bound,falseotherwise
-