Class DemandPauser

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 Details

    • DemandPauser

      public DemandPauser()
  • Method Details

    • bind

      public void bind(PausableMulti multi)
      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:
      true if paused, false otherwise
      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:
      true if the buffer was cleared, false if 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:
      true if bound, false otherwise