Class EventStream<T>

  • Type Parameters:
    T - the type that SSE data fields will be deserialized into
    All Implemented Interfaces:
    java.lang.AutoCloseable, java.lang.Iterable<T>

    public final class EventStream<T>
    extends java.lang.Object
    implements java.lang.Iterable<T>, java.lang.AutoCloseable
    Provides a convenient way to consume Server-Sent Events (SSE) from a stream.

    Each SSE message's data field is deserialized into the type T, allowing for easy processing of events as domain objects.

    Event Consumption

    Events can be consumed in multiple ways:

    • Iteration: Use a for-each loop to process each event:
    
     try (EventStream<MyEvent> eventStream = new EventStream<>(...)) {
         for (MyEvent event : eventStream) {
             handleEvent(event);
         }
     }
     
    • Stream API: Consume events as a Java Stream (must be closed after use):
    
     try (EventStream<MyEvent> eventStream = new EventStream<>(...);
          Stream<MyEvent> stream = eventStream.stream()) {
         stream.forEach(this::handleEvent);
     }
     
    • Collect to List: Read all remaining events into a list:
    
     try (EventStream<MyEvent> eventStream = new EventStream<>(...)) {
         List<MyEvent> events = eventStream.toList();
     }
     

    Events are lazily loaded from the underlying SSE stream. Consumption stops either when the stream ends or when an optional terminal message is encountered.

    Important: This class implements AutoCloseable and must be used within a try-with-resources block to ensure that underlying streams are properly closed after consumption, preventing resource leaks.

    • Constructor Summary

      Constructors 
      Constructor Description
      EventStream​(java.io.InputStream in, com.fasterxml.jackson.core.type.TypeReference<T> typeReference, com.fasterxml.jackson.databind.ObjectMapper mapper, java.util.Optional<java.lang.String> terminalMessage)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()  
      java.util.Iterator<T> iterator()
      Returns an Iterator of EventStream events, enabling iteration via for-each loops.
      java.util.Optional<T> next()
      Returns the next message.
      java.util.stream.Stream<T> stream()
      Returns a Stream of events.
      java.util.List<T> toList()
      Reads all events and returns them as a List.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
    • Constructor Detail

      • EventStream

        public EventStream​(java.io.InputStream in,
                           com.fasterxml.jackson.core.type.TypeReference<T> typeReference,
                           com.fasterxml.jackson.databind.ObjectMapper mapper,
                           java.util.Optional<java.lang.String> terminalMessage)
    • Method Detail

      • next

        public java.util.Optional<T> next()
                                   throws java.io.IOException
        Returns the next message. If another message does not exist returns Optional.empty().
        Returns:
        the next message or Optional.empty() if no more messages
        Throws:
        java.io.IOException - when parsing the next message.
      • toList

        public java.util.List<T> toList()
        Reads all events and returns them as a List. This method calls close().
        Returns:
        list of events
      • iterator

        public java.util.Iterator<T> iterator()
        Returns an Iterator of EventStream events, enabling iteration via for-each loops.
        Specified by:
        iterator in interface java.lang.Iterable<T>
        Returns:
        events iterator.
      • stream

        public java.util.stream.Stream<T> stream()
        Returns a Stream of events. Must be closed after use!
        Returns:
        streamed events
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Throws:
        java.io.IOException