Class CloseCallbackMessageStream<M extends Message>

java.lang.Object
org.axonframework.messaging.core.DelegatingMessageStream<M,M>
org.axonframework.messaging.core.CloseCallbackMessageStream<M>
Type Parameters:
M - The type of Message handled by this MessageStream.
All Implemented Interfaces:
MessageStream<M>

public class CloseCallbackMessageStream<M extends Message> extends DelegatingMessageStream<M,M>
MessageStream implementation that invokes the given closeHandler once the stream is closed. A stream is considered closed when a consumer explicitly calls close() or when the stream is completed.

Note that when close is called on the delegate, or when the client does not attempt to consume this stream, the close handler may never be invoked, even though the stream is completed.

  • Constructor Details

    • CloseCallbackMessageStream

      public CloseCallbackMessageStream(@Nonnull MessageStream<M> delegate, @Nonnull Runnable closeHandler)
      Creates an instance of the CloseCallbackMessageStream, calling the given closeHandler once this stream is closed, or the given delegate completes.
      Parameters:
      delegate - The MessageStream to wrap with the close handler invocation logic
      closeHandler - The handler to invoke when the stream is closed or completed
  • Method Details

    • next

      public Optional<MessageStream.Entry<M>> next()
      Description copied from interface: MessageStream
      Returns an Optional carrying the next entry from the stream, if such entry was available. If no entry was available for reading, this method returns an empty Optional.

      This method will never block for elements becoming available.

      Returns:
      An optional carrying the next entry, if available.
    • peek

      public Optional<MessageStream.Entry<M>> peek()
      Description copied from interface: MessageStream
      Returns an Optional carrying the next entry from the stream (without moving the stream pointer), if such entry was available. If no entry was available for reading, this method returns an empty Optional.

      This method will never block for elements becoming available.

      Returns:
      An optional carrying the next entry, if available.
    • setCallback

      public void setCallback(@Nonnull Runnable callback)
      Description copied from interface: MessageStream
      Registers the callback to invoke when entries are available for reading or when the stream completes (either normally or with an error). An invocation of the callback does not in any way guarantee that entries are indeed available, or that the stream has indeed been completed. Implementations may choose to suppress repeated invocations of the callback if no entries have been read in the meantime.

      Any previously registered callback is replaced with the given callback.

      The callback is called on an arbitrary thread, and it should keep work performed on this thread to a minimum as this may interfere with other callbacks handled by the same thread. Any exception thrown by the callback will result in the stream completing with this exception as the error.

      Specified by:
      setCallback in interface MessageStream<M extends Message>
      Overrides:
      setCallback in class DelegatingMessageStream<M extends Message,M extends Message>
      Parameters:
      callback - The callback to invoke when entries are available for reading, or the stream completes.
    • close

      public void close()
      Description copied from interface: MessageStream
      Closes this stream, freeing any possible resources occupied by the underlying stream. After invocation, some entries may still be available for reading.

      Implementations must always release resources when a stream is completed, either with an error or normally. Therefore, it is only required to close() a stream if the consumer decides to not read until the end.

      Specified by:
      close in interface MessageStream<M extends Message>
      Overrides:
      close in class DelegatingMessageStream<M extends Message,M extends Message>
    • hasNextAvailable

      public boolean hasNextAvailable()
      Description copied from interface: MessageStream
      Indicates whether an entry is available for immediate reading. When entries are reported available, there is no guarantee that MessageStream.next() will indeed return an entry. However, besides any concurrent activity on this stream, it is guaranteed that no entries are available for reading when this method returns false.
      Specified by:
      hasNextAvailable in interface MessageStream<M extends Message>
      Overrides:
      hasNextAvailable in class DelegatingMessageStream<M extends Message,M extends Message>
      Returns:
      true when there are entries available for reading, false otherwise.