Class SseSubscription

java.lang.Object
org.apache.juneau.rest.server.sse.SseSubscription
All Implemented Interfaces:
AutoCloseable, Iterable<SseEvent>

public class SseSubscription extends Object implements AutoCloseable, Iterable<SseEvent>
Subscriber queue for an SseBroadcaster.
  • Method Details

    • getId

      public String getId()
      The subscriber identifier.
      Returns:
      The subscriber identifier.
    • isClosed

      public boolean isClosed()
      Returns whether this subscription has been closed.
      Returns:
      true if this subscription has been closed.
    • offer

      public boolean offer(SseEvent event)
      Queues an event, dropping the oldest queued event when the queue is full.

      Does nothing once the subscription is closed. SseBroadcaster.publish(SseEvent) calls this for every subscriber; call it directly to address a single subscriber.

      Parameters:
      event - The event. Must not be null.
      Returns:
      true if the oldest queued event was dropped to make room.
    • take

      Blocks until the next event is available.
      Returns:
      The next event.
      Throws:
      InterruptedException - If the wait was interrupted.
    • poll

      public SseEvent poll(Duration timeout) throws InterruptedException
      Waits up to a timeout for the next event.

      Unlike take(), returns once the subscription is closed (within about 50 ms of the close), so a caller whose subscription was replaced or closed from another thread never hangs. The 50 ms bound applies to a waiting poll; take() is not woken by close().

      Parameters:
      timeout - How long to wait. Must not be null.
      Returns:
      The next event, or null on timeout or once the subscription is closed.
      Throws:
      InterruptedException - If the wait was interrupted.
    • iterator

      Specified by:
      iterator in interface Iterable<SseEvent>
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable