Class ParallelBehavior

java.lang.Object
dev.agenor.core.composite.CompositeBehavior
dev.agenor.runtime.behavior.composite.ParallelBehavior
All Implemented Interfaces:
Behavior

public class ParallelBehavior extends CompositeBehavior
Executes multiple child behaviors in parallel using virtual threads. Supports different completion strategies (ALL, ANY, FIRST, N_OF_M).

ParallelBehavior reports SchedulingHint.ONCE: the scheduler fires all children in parallel immediately after registration via agent.addBehavior(), waits for the configured completion strategy, then marks the behavior inactive.

The @Behavior(type = PARALLEL) route to this class was removed in 0.30.0 with the constant that named it: an annotation cannot express a composite's children. Build it and add it with agent.addBehavior(), the way OrderOrchestratorAgent builds its sibling SequentialBehavior.

  • Constructor Details

  • Method Details

    • getSchedulingHint

      public SchedulingHint getSchedulingHint()
      ParallelBehavior is a one-shot fan-out: fire all children in parallel, wait for the completion strategy, then done. The scheduler drives this automatically.
      Overrides:
      getSchedulingHint in class CompositeBehavior
      Returns:
      the scheduling hint, never null
    • execute

      public CompletableFuture<Void> execute()
      Description copied from interface: Behavior
      Executes this behavior once, asynchronously.

      This method performs a single execution cycle of the behavior's logic. The actual execution pattern (one-shot, cyclic, etc.) is determined by the BehaviorType and managed by the BehaviorScheduler.

      Asynchronous Execution: This method is non-blocking and returns immediately with a CompletableFuture that completes when the behavior's work is done. This enables:

      • Parallel execution of multiple behaviors
      • Non-blocking agent operations
      • Compositional behavior chains
      • Centralized error handling

      Active State Check: Implementations should check Behavior.isActive() before performing work and return early if the behavior has been stopped:

      
       public CompletableFuture<Void> execute() {
           if (!isActive()) {
               return CompletableFuture.completedFuture(null);
           }
           // Perform behavior logic...
       }
       

      Error Handling: Exceptions thrown during execution should be handled within the behavior or propagated via the returned future. Uncaught exceptions may:

      • Stop cyclic behavior execution
      • Mark the behavior as failed
      • Trigger agent error handlers

      Thread Safety: This method may be called concurrently from different threads, especially for event-driven behaviors. Implementations must be thread-safe.

      Performance: Keep execution time reasonable, especially for cyclic behaviors. Long-running operations should be delegated to separate threads or use async APIs.

      Example:

      
       @Override
       public CompletableFuture<Void> execute() {
           return CompletableFuture.supplyAsync(() -> {
               if (!isActive()) return null;
      
               try {
                   // Perform behavior work
                   Data data = collectData();
                   processData(data);
      
                   // Send results
                   Message result = Message.builder()
                           .topic("data.processed")
                           .content(data)
                           .build();
                   agent.getMessageDispatcher().publish("data.processed", result);
      
                   return null;
      
               } catch (Exception e) {
                   log.error("Behavior execution failed", e);
                   throw new CompletionException(e);
               }
           });
       }
       
      Returns:
      a CompletableFuture that completes when execution is finished, or completes exceptionally if execution fails
      See Also:
    • getCompletedCount

      public int getCompletedCount()
    • getFinishedCount

      public int getFinishedCount()
    • getStrategy

      public CompletionStrategy getStrategy()
    • getChildTimeout

      public Duration getChildTimeout()
    • setChildTimeout

      public void setChildTimeout(Duration childTimeout)