Package com.google.genai.gaos.utils
Class EventStream<T>
java.lang.Object
com.google.genai.gaos.utils.EventStream<T>
- Type Parameters:
T- the type that SSEdatafields will be deserialized into
- All Implemented Interfaces:
AutoCloseable,Iterable<T>
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
ConstructorsConstructorDescriptionEventStream(InputStream in, com.fasterxml.jackson.core.type.TypeReference<T> typeReference, com.fasterxml.jackson.databind.ObjectMapper mapper, Optional<String> terminalMessage) EventStream(InputStream in, com.fasterxml.jackson.core.type.TypeReference<T> typeReference, com.fasterxml.jackson.databind.ObjectMapper mapper, Optional<String> terminalMessage, boolean dataRequired) -
Method Summary
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
EventStream
public EventStream(InputStream in, com.fasterxml.jackson.core.type.TypeReference<T> typeReference, com.fasterxml.jackson.databind.ObjectMapper mapper, Optional<String> terminalMessage) -
EventStream
public EventStream(InputStream in, com.fasterxml.jackson.core.type.TypeReference<T> typeReference, com.fasterxml.jackson.databind.ObjectMapper mapper, Optional<String> terminalMessage, boolean dataRequired)
-
-
Method Details
-
next
Returns the next message. If another message does not exist returnsOptional.empty().- Returns:
- the next message or
Optional.empty()if no more messages - Throws:
IOException- when parsing the next message.
-
toList
Reads all events and returns them as aList. This method callsclose().- Returns:
- list of events
-
iterator
Returns anIteratorofEventStreamevents, enabling iteration via for-each loops. -
stream
Returns aStreamof events. Must be closed after use!- Returns:
- streamed events
-
close
- Specified by:
closein interfaceAutoCloseable- Throws:
IOException
-
isClosed
public boolean isClosed()
-