Class ParallelBehavior
- All Implemented Interfaces:
Behavior
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.
-
Field Summary
Fields inherited from class dev.agenor.core.composite.CompositeBehavior
active, agent, behaviorId, childBehaviors, interval -
Constructor Summary
ConstructorsConstructorDescriptionParallelBehavior(String behaviorId) ParallelBehavior(String behaviorId, CompletionStrategy strategy) ParallelBehavior(String behaviorId, CompletionStrategy strategy, int requiredCompletions) ParallelBehavior(String behaviorId, CompletionStrategy strategy, int requiredCompletions, Duration childTimeout) -
Method Summary
Modifier and TypeMethodDescriptionexecute()Executes this behavior once, asynchronously.intintParallelBehavioris a one-shot fan-out: fire all children in parallel, wait for the completion strategy, then done.voidsetChildTimeout(Duration childTimeout) Methods inherited from class dev.agenor.core.composite.CompositeBehavior
addChildBehavior, getAgent, getBehaviorId, getChildBehaviors, getInterval, getType, isActive, setAgent, stopMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface dev.agenor.core.Behavior
getInitialDelay
-
Constructor Details
-
ParallelBehavior
-
ParallelBehavior
-
ParallelBehavior
-
ParallelBehavior
public ParallelBehavior(String behaviorId, CompletionStrategy strategy, int requiredCompletions, Duration childTimeout)
-
-
Method Details
-
getSchedulingHint
ParallelBehavioris a one-shot fan-out: fire all children in parallel, wait for the completion strategy, then done. The scheduler drives this automatically.- Overrides:
getSchedulingHintin classCompositeBehavior- Returns:
- the scheduling hint, never null
-
execute
Description copied from interface:BehaviorExecutes 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
BehaviorTypeand managed by theBehaviorScheduler.Asynchronous Execution: This method is non-blocking and returns immediately with a
CompletableFuturethat 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
CompletableFuturethat completes when execution is finished, or completes exceptionally if execution fails - See Also:
-
getCompletedCount
public int getCompletedCount() -
getFinishedCount
public int getFinishedCount() -
getStrategy
-
getChildTimeout
-
setChildTimeout
-