Class AbstractSSEEventListener
java.lang.Object
org.a2aproject.sdk.client.transport.spi.sse.AbstractSSEEventListener
Abstract base class for SSE event listeners that provides common functionality
for handling Server-Sent Events across different transport implementations.
This class implements the Template Method pattern, where subclasses provide the specific message parsing logic while the base class handles common concerns like error handling and connection lifecycle management.
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractSSEEventListener(Consumer<org.a2aproject.sdk.spec.StreamingEventKind> eventHandler, @Nullable Consumer<Throwable> errorHandler) Creates a new SSE event listener with the specified handlers. -
Method Summary
Modifier and TypeMethodDescriptionGets the error handler for processing errors.protected Consumer<org.a2aproject.sdk.spec.StreamingEventKind>Gets the event handler for processing streaming events.protected voidhandleEvent(org.a2aproject.sdk.spec.StreamingEventKind event, @Nullable Future<Void> future) Processes a parsed streaming event and handles auto-close logic for final events.voidHandles errors that occur during SSE streaming.abstract voidonMessage(org.a2aproject.sdk.client.http.ServerSentEvent event, @Nullable Future<Void> completableFuture) Handles incoming SSE messages.protected booleanshouldAutoClose(org.a2aproject.sdk.spec.StreamingEventKind event) Determines if the SSE connection should be automatically closed based on the event type.
-
Constructor Details
-
AbstractSSEEventListener
protected AbstractSSEEventListener(Consumer<org.a2aproject.sdk.spec.StreamingEventKind> eventHandler, @Nullable Consumer<Throwable> errorHandler) Creates a new SSE event listener with the specified handlers.- Parameters:
eventHandler- Handler for processing streaming eventserrorHandler- Optional handler for processing errors
-
-
Method Details
-
getEventHandler
Gets the event handler for processing streaming events.- Returns:
- The event handler
-
getErrorHandler
Gets the error handler for processing errors.- Returns:
- The error handler, or null if not set
-
onMessage
public abstract void onMessage(org.a2aproject.sdk.client.http.ServerSentEvent event, @Nullable Future<Void> completableFuture) Handles incoming SSE messages. Subclasses must implement the specific parsing logic for their transport protocol.- Parameters:
event- The parsed SSE event from the streamcompletableFuture- Optional future for controlling the SSE connection
-
onError
Handles errors that occur during SSE streaming. This method is identical across all implementations.- Parameters:
throwable- The error that occurredfuture- Optional future for closing the SSE connection
-
handleEvent
protected void handleEvent(org.a2aproject.sdk.spec.StreamingEventKind event, @Nullable Future<Void> future) Processes a parsed streaming event and handles auto-close logic for final events. This method encapsulates the common logic for handling events and determining when to close the SSE connection.- Parameters:
event- The parsed streaming eventfuture- Optional future for closing the SSE connection
-
shouldAutoClose
protected boolean shouldAutoClose(org.a2aproject.sdk.spec.StreamingEventKind event) Determines if the SSE connection should be automatically closed based on the event type. The connection is closed when receiving final task states to prevent connection leaks.- Parameters:
event- The streaming event to check- Returns:
- true if the connection should be closed, false otherwise
-