Class CompositeAgent

java.lang.Object
org.agrona.concurrent.CompositeAgent
All Implemented Interfaces:
Agent

public class CompositeAgent extends Object implements Agent
Group several Agents into one composite, so they can be scheduled as a unit.
  • Field Details

    • agents

      private final Agent[] agents
    • roleName

      private final String roleName
    • agentIndex

      private int agentIndex
  • Constructor Details

    • CompositeAgent

      public CompositeAgent(List<? extends Agent> agents)
      Construct a new composite with a given list Agents to begin with.
      Parameters:
      agents - the parts of this composite, at least one agent and no null agents allowed
      Throws:
      IllegalArgumentException - if an empty array of agents is provided
      NullPointerException - if the array or any element is null
    • CompositeAgent

      public CompositeAgent(Agent... agents)
      Construct a new composite with a given list Agents to begin with.
      Parameters:
      agents - the parts of this composite, at least one agent and no null agents allowed
      Throws:
      IllegalArgumentException - if an empty array of agents is provided, or single agent provided
      NullPointerException - if the array or any element is null
  • Method Details

    • roleName

      public String roleName()
      Get the name of this agent's role.
      Specified by:
      roleName in interface Agent
      Returns:
      the name of this agent's role.
    • onStart

      public void onStart()
      To be overridden by Agents that need to do resource init on start.

      This method will be called by the agent thread once on start.

      Note that one agent throwing an exception on start will not prevent other agents from being started.

      Specified by:
      onStart in interface Agent
      Throws:
      RuntimeException - if any sub-agent throws an exception onStart. The agents exceptions are collected as suppressed exceptions in the thrown exception.
    • doWork

      public int doWork() throws Exception
      An agent should implement this method to do its work.

      The return value is used for implementing a backoff strategy that can be employed when no work is currently available for the agent to process.

      If the Agent wished to terminate and close then a AgentTerminationException can be thrown.

      Specified by:
      doWork in interface Agent
      Returns:
      0 to indicate no work was currently available, a positive value otherwise.
      Throws:
      Exception - if an error has occurred
    • onClose

      public void onClose()
      To be overridden by Agents that need to do resource cleanup on close.

      This method will be called after the agent thread has terminated or if the agent is closed before it runs.

      Note that one agent throwing an exception on close will not prevent other agents from being closed.

      Specified by:
      onClose in interface Agent
      Throws:
      RuntimeException - if any sub-agent throws an exception onClose. The agents exceptions are collected as suppressed exceptions in the thrown exception.