Interface BlockingStream<M>

Type Parameters:
M - the type of Message contained in this stream
All Superinterfaces:
AutoCloseable
All Known Subinterfaces:
TrackingEventStream
All Known Implementing Classes:
EventBuffer

public interface BlockingStream<M> extends AutoCloseable
Interface for a stream that can be polled for information using (optionally blocking) pull operations.
Since:
3.0
Author:
Rene de Waele, Allard Buijze
  • Method Summary

    Modifier and Type
    Method
    Description
    default Stream<M>
    Returns this MessageStream as a Stream of Messages.
    void
     
    default boolean
    Checks whether or not the next message in the stream is available.
    boolean
    hasNextAvailable(int timeout, TimeUnit unit)
    Checks whether or not the next message in the stream is available.
    Returns the next available message in the stream.
    Checks whether or not the next message in the stream is immediately available.
    default boolean
    Set a callback to be invoked once new messages are available on this stream.
    default void
    Report the stream that a specific message was ignored by the consumer.
  • Method Details

    • hasNextAvailable

      default boolean hasNextAvailable()
      Checks whether or not the next message in the stream is available. If so this method returns true immediately. If not it returns false immediately.

      Note that if this BlockingStream has only recently been constructed, the chance is high there are no events present yet. Hence, it is recommended to use hasNextAvailable(int, TimeUnit) with a reasonable time window or setOnAvailableCallback(Runnable) instead.

      Returns:
      true if a message is available or becomes available before the given timeout, false otherwise
    • peek

      Optional<M> peek()
      Checks whether or not the next message in the stream is immediately available. If so, an Optional with the next message is returned (without moving the stream pointer), otherwise an empty Optional is returned.
      Returns:
      the next event if immediately available
    • hasNextAvailable

      boolean hasNextAvailable(int timeout, TimeUnit unit) throws InterruptedException
      Checks whether or not the next message in the stream is available. If a message is available when this method is invoked this method returns immediately. If not, this method will block until a message becomes available, returning true or until the given timeout expires, returning false.

      To check if the stream has messages available now, pass a zero timeout.

      Parameters:
      timeout - the maximum number of time units to wait for messages to become available
      unit - the time unit for the timeout
      Returns:
      true if a message is available or becomes available before the given timeout, false otherwise
      Throws:
      InterruptedException - when the thread is interrupted before the indicated time is up
    • nextAvailable

      M nextAvailable() throws InterruptedException
      Returns the next available message in the stream. Note that this method blocks for as long as there are no available messages in the stream. In case this blocking behavior is not desired use hasNextAvailable() with or without a timeout to check if the stream has available messages before calling this method.
      Returns:
      the next available message
      Throws:
      InterruptedException - when the thread is interrupted before the next message is returned
    • close

      void close()
      Specified by:
      close in interface AutoCloseable
    • asStream

      default Stream<M> asStream()
      Returns this MessageStream as a Stream of Messages. Note that the returned Stream will start at the current position of this instance.

      Note that iterating over the returned Stream may affect this MessageStream and vice versa. It is therefore not recommended to use this MessageStream after invoking this method.

      Returns:
      This MessageStream as a Stream of Messages
    • skipMessagesWithPayloadTypeOf

      default void skipMessagesWithPayloadTypeOf(M ignoredMessage)
      Report the stream that a specific message was ignored by the consumer. Stream implementation can use this information for instance to filter messages with the same type of payload.
      Parameters:
      ignoredMessage - the message containing the payload to exclude from the stream
    • setOnAvailableCallback

      default boolean setOnAvailableCallback(Runnable callback)
      Set a callback to be invoked once new messages are available on this stream. Returns true if this functionality is supported and false otherwise. When true is returned, the callee can expect the callback to be invoked immediately.

      Note that returning false does not define the given callback is never invoked. If the callee needs to be certain the callback is never invoked in case of false, a no-op Runnable should be provided.

      Parameters:
      callback - a Runnable
      Returns:
      true if on available callback is supported and can thus be waited on, false otherwise