Package pl.gsmservice.gateway.utils
Class EventStream<T>
- java.lang.Object
-
- pl.gsmservice.gateway.utils.EventStream<T>
-
- Type Parameters:
T- the type that SSEdatafields 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.AutoCloseableProvides a convenient way to consume Server-Sent Events (SSE) from a stream.Each SSE message's
datafield is deserialized into the typeT, 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
AutoCloseableand 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 voidclose()java.util.Iterator<T>iterator()Returns anIteratorofEventStreamevents, enabling iteration via for-each loops.java.util.Optional<T>next()Returns the next message.java.util.stream.Stream<T>stream()Returns aStreamof events.java.util.List<T>toList()Reads all events and returns them as aList.
-
-
-
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 returnsOptional.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 aList. This method callsclose().- Returns:
- list of events
-
iterator
public java.util.Iterator<T> iterator()
Returns anIteratorofEventStreamevents, enabling iteration via for-each loops.- Specified by:
iteratorin interfacejava.lang.Iterable<T>- Returns:
- events iterator.
-
stream
public java.util.stream.Stream<T> stream()
Returns aStreamof events. Must be closed after use!- Returns:
- streamed events
-
close
public void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Throws:
java.io.IOException
-
-