Class AbstractSSEEventListener

java.lang.Object
org.a2aproject.sdk.client.transport.spi.sse.AbstractSSEEventListener

public abstract class AbstractSSEEventListener extends Object
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

    Constructors
    Modifier
    Constructor
    Description
    protected
    AbstractSSEEventListener(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 Type
    Method
    Description
    protected @Nullable Consumer<Throwable>
    Gets the error handler for processing errors.
    protected Consumer<org.a2aproject.sdk.spec.StreamingEventKind>
    Gets the event handler for processing streaming events.
    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.
    void
    onError(Throwable throwable, @Nullable Future<Void> future)
    Handles errors that occur during SSE streaming.
    abstract void
    onMessage(org.a2aproject.sdk.client.http.ServerSentEvent event, @Nullable Future<Void> completableFuture)
    Handles incoming SSE messages.
    protected boolean
    shouldAutoClose(org.a2aproject.sdk.spec.StreamingEventKind event)
    Determines if the SSE connection should be automatically closed based on the event type.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 events
      errorHandler - Optional handler for processing errors
  • Method Details

    • getEventHandler

      protected Consumer<org.a2aproject.sdk.spec.StreamingEventKind> getEventHandler()
      Gets the event handler for processing streaming events.
      Returns:
      The event handler
    • getErrorHandler

      protected @Nullable Consumer<Throwable> 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 stream
      completableFuture - Optional future for controlling the SSE connection
    • onError

      public void onError(Throwable throwable, @Nullable Future<Void> future)
      Handles errors that occur during SSE streaming. This method is identical across all implementations.
      Parameters:
      throwable - The error that occurred
      future - 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 event
      future - 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