44import java .util .function .Consumer ;
55import java .util .logging .Logger ;
66
7+ import io .a2a .client .transport .spi .sse .AbstractSSEEventListener ;
78import io .a2a .grpc .StreamResponse ;
89import io .a2a .grpc .utils .JSONRPCUtils ;
910import io .a2a .grpc .utils .ProtoUtils ;
1011import io .a2a .jsonrpc .common .json .JsonProcessingException ;
1112import io .a2a .spec .A2AError ;
1213import io .a2a .spec .StreamingEventKind ;
13- import io .a2a .spec .Task ;
14- import io .a2a .spec .TaskState ;
15- import io .a2a .spec .TaskStatusUpdateEvent ;
1614import org .jspecify .annotations .Nullable ;
1715
18- public class SSEEventListener {
16+ /**
17+ * JSON-RPC transport implementation of SSE event listener.
18+ * Handles parsing of JSON-RPC formatted messages from SSE streams.
19+ */
20+ public class SSEEventListener extends AbstractSSEEventListener {
1921
2022 private static final Logger log = Logger .getLogger (SSEEventListener .class .getName ());
21- private final Consumer <StreamingEventKind > eventHandler ;
22- private final @ Nullable
23- Consumer <Throwable > errorHandler ;
2423 private volatile boolean completed = false ;
2524
2625 public SSEEventListener (Consumer <StreamingEventKind > eventHandler ,
2726 @ Nullable Consumer <Throwable > errorHandler ) {
28- this .eventHandler = eventHandler ;
29- this .errorHandler = errorHandler ;
27+ super (eventHandler , errorHandler );
3028 }
3129
30+ @ Override
3231 public void onMessage (String message , @ Nullable Future <Void > completableFuture ) {
33- handleMessage (message , completableFuture );
34- }
35-
36- public void onError (Throwable throwable , @ Nullable Future <Void > future ) {
37- if (errorHandler != null ) {
38- errorHandler .accept (throwable );
39- }
40- if (future != null ) {
41- future .cancel (true ); // close SSE channel
42- }
32+ parseAndHandleMessage (message , completableFuture );
4333 }
4434
4535 public void onComplete () {
@@ -52,40 +42,30 @@ public void onComplete() {
5242
5343 // Signal normal stream completion (null error means successful completion)
5444 log .fine ("SSEEventListener.onComplete() called - signaling successful stream completion" );
55- if (errorHandler != null ) {
45+ if (getErrorHandler () != null ) {
5646 log .fine ("Calling errorHandler.accept(null) to signal successful completion" );
57- errorHandler .accept (null );
47+ getErrorHandler () .accept (null );
5848 } else {
5949 log .warning ("errorHandler is null, cannot signal completion" );
6050 }
6151 }
6252
63- private void handleMessage (String message , @ Nullable Future <Void > future ) {
53+ /**
54+ * Parses a JSON-RPC message and delegates to the base class for event handling.
55+ *
56+ * @param message The raw JSON-RPC message string
57+ * @param future Optional future for controlling the SSE connection
58+ */
59+ private void parseAndHandleMessage (String message , @ Nullable Future <Void > future ) {
6460 try {
6561 StreamResponse response = JSONRPCUtils .parseResponseEvent (message );
66-
6762 StreamingEventKind event = ProtoUtils .FromProto .streamingEventKind (response );
68- eventHandler .accept (event );
69-
70- // Client-side auto-close on final events to prevent connection leaks
71- // Handles both TaskStatusUpdateEvent and Task objects with final states
72- // This covers late subscriptions to completed tasks and ensures no connection leaks
73- boolean shouldClose = false ;
74- if (event instanceof TaskStatusUpdateEvent tue && tue .isFinal ()) {
75- shouldClose = true ;
76- } else if (event instanceof Task task ) {
77- TaskState state = task .status ().state ();
78- if (state .isFinal ()) {
79- shouldClose = true ;
80- }
81- }
82-
83- if (shouldClose && future != null ) {
84- future .cancel (true ); // close SSE channel
85- }
63+
64+ // Delegate to base class for common event handling and auto-close logic
65+ handleEvent (event , future );
8666 } catch (A2AError error ) {
87- if (errorHandler != null ) {
88- errorHandler .accept (error );
67+ if (getErrorHandler () != null ) {
68+ getErrorHandler () .accept (error );
8969 }
9070 } catch (JsonProcessingException e ) {
9171 throw new RuntimeException (e );
0 commit comments