Package org.agrona.concurrent
Class CompositeAgent
java.lang.Object
org.agrona.concurrent.CompositeAgent
- All Implemented Interfaces:
Agent
Group several
Agent
s into one composite, so they can be scheduled as a unit.-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCompositeAgent
(List<? extends Agent> agents) CompositeAgent
(Agent... agents) -
Method Summary
Modifier and TypeMethodDescriptionint
doWork()
An agent should implement this method to do its work.void
onClose()
To be overridden by Agents that need to do resource cleanup on close.void
onStart()
To be overridden by Agents that need to do resource init on start.roleName()
Get the name of this agent's role.
-
Field Details
-
agents
-
roleName
-
agentIndex
private int agentIndex
-
-
Constructor Details
-
CompositeAgent
- 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 providedNullPointerException
- if the array or any element is null
-
CompositeAgent
- 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 providedNullPointerException
- if the array or any element is null
-
-
Method Details
-
roleName
Get 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 interfaceAgent
- Throws:
RuntimeException
- if any sub-agent throws an exception onStart. The agents exceptions are collected as suppressed exceptions in the thrown exception.
-
doWork
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. -
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 interfaceAgent
- Throws:
RuntimeException
- if any sub-agent throws an exception onClose. The agents exceptions are collected as suppressed exceptions in the thrown exception.
-