11package io .a2a .examples .helloworld .server ;
22
3- import java .util .Map ;
4- import java .util .concurrent .CompletableFuture ;
5- import java .util .concurrent .ConcurrentHashMap ;
6- import java .util .concurrent .ExecutorService ;
7- import java .util .concurrent .Executors ;
8- import java .util .concurrent .TimeUnit ;
9-
3+ import jakarta .annotation .PreDestroy ;
104import jakarta .enterprise .context .ApplicationScoped ;
115import jakarta .enterprise .inject .Produces ;
12- import jakarta .annotation .PreDestroy ;
136
147import io .a2a .server .agentexecution .AgentExecutor ;
158import io .a2a .server .agentexecution .RequestContext ;
@@ -31,16 +24,6 @@ public AgentExecutor agentExecutor() {
3124 }
3225
3326 private static class FireAndForgetAgentExecutor implements AgentExecutor {
34- // Dedicated thread pool for background task execution
35- private final ExecutorService taskExecutor = Executors .newCachedThreadPool (r -> {
36- Thread t = new Thread (r , "AgentTask-" + System .currentTimeMillis ());
37- t .setDaemon (true ); // Don't prevent JVM shutdown
38- return t ;
39- });
40-
41- // Track running tasks for cancellation - store the future reference
42- private final Map <String , CompletableFuture <Void >> runningTasks = new ConcurrentHashMap <>();
43-
4427 @ Override
4528 public void execute (RequestContext context , EventQueue eventQueue ) throws JSONRPCError {
4629 Task task = context .getTask ();
@@ -68,12 +51,12 @@ public void execute(RequestContext context, EventQueue eventQueue) throws JSONRP
6851
6952 System .out .println ("====> task set to WORKING, starting background execution" );
7053
71- // Fire and forget - start the task but don't wait for it
72- CompletableFuture <Void > taskFuture = CompletableFuture
73- .runAsync (() -> executeTaskInBackground (context , eventQueue ), taskExecutor );
54+ // // Fire and forget - start the task but don't wait for it
55+ // CompletableFuture<Void> taskFuture = CompletableFuture
56+ // .runAsync(() -> executeTaskInBackground(context, eventQueue), taskExecutor);
7457
75- // Store the future for potential cancellation
76- runningTasks .put (context .getTaskId (), taskFuture );
58+ // // Store the future for potential cancellation
59+ // runningTasks.put(context.getTaskId(), taskFuture);
7760
7861 // Method returns immediately - task continues in background
7962 System .out .println ("====> execute() method returning immediately, task running in background" );
@@ -99,16 +82,6 @@ public void cancel(RequestContext context, EventQueue eventQueue) throws JSONRPC
9982 throw new TaskNotCancelableError ();
10083 }
10184
102- // Cancel the CompletableFuture
103- CompletableFuture <Void > taskFuture = runningTasks .get (task .getId ());
104- if (taskFuture != null ) {
105- boolean cancelled = taskFuture .cancel (true ); // mayInterruptIfRunning = true
106- System .out .println ("====> cancellation attempted, success: " + cancelled );
107- }
108-
109- // Remove from running tasks and update status
110- runningTasks .remove (task .getId ());
111-
11285 eventQueue .enqueueEvent (new TaskStatusUpdateEvent .Builder ()
11386 .taskId (task .getId ())
11487 .contextId (task .getContextId ())
@@ -129,21 +102,9 @@ private void executeTaskInBackground(RequestContext context, EventQueue eventQue
129102 try {
130103 System .out .println ("====> background execution started for task: " + taskId );
131104
132- // Check if task was cancelled before we even started
133- if (!runningTasks .containsKey (taskId )) {
134- System .out .println ("====> task was cancelled before background execution started" );
135- return ;
136- }
137-
138105 // Perform the actual work
139106 Object result = performActualWork (context );
140107
141- // Check again if task was cancelled during execution
142- if (!runningTasks .containsKey (taskId )) {
143- System .out .println ("====> task was cancelled during execution" );
144- return ;
145- }
146-
147108 // Task completed successfully
148109 eventQueue .enqueueEvent (new TaskStatusUpdateEvent .Builder ()
149110 .taskId (taskId )
@@ -159,33 +120,13 @@ private void executeTaskInBackground(RequestContext context, EventQueue eventQue
159120 System .out .println ("====> background task was interrupted: " + taskId );
160121 Thread .currentThread ().interrupt ();
161122
162- // Only send CANCELED event if task is still tracked (not already cancelled)
163- if (runningTasks .containsKey (taskId )) {
164- eventQueue .enqueueEvent (new TaskStatusUpdateEvent .Builder ()
165- .taskId (taskId )
166- .contextId (context .getContextId ())
167- .status (new TaskStatus (TaskState .CANCELED ))
168- .isFinal (true )
169- .build ());
170- }
171-
172123 } catch (Exception e ) {
173124 // Task failed
174125 System .err .println ("====> background task failed: " + taskId );
175126 e .printStackTrace ();
176127
177- if (runningTasks .containsKey (taskId )) {
178- eventQueue .enqueueEvent (new TaskStatusUpdateEvent .Builder ()
179- .taskId (taskId )
180- .contextId (context .getContextId ())
181- .status (new TaskStatus (TaskState .FAILED ))
182- .isFinal (true )
183- .build ());
184- }
185-
186128 } finally {
187- // Always clean up - remove from running tasks
188- runningTasks .remove (taskId );
129+ // Always clean up
189130 System .out .println ("====> background task cleanup completed: " + taskId );
190131 }
191132 }
@@ -220,15 +161,6 @@ private Object performActualWork(RequestContext context) throws InterruptedExcep
220161 @ PreDestroy
221162 public void cleanup () {
222163 System .out .println ("====> shutting down task executor" );
223- taskExecutor .shutdown ();
224- try {
225- if (!taskExecutor .awaitTermination (5 , TimeUnit .SECONDS )) {
226- taskExecutor .shutdownNow ();
227- }
228- } catch (InterruptedException e ) {
229- taskExecutor .shutdownNow ();
230- Thread .currentThread ().interrupt ();
231- }
232- }
164+ }
233165 }
234166}
0 commit comments