Class DynamicCompositeAgent
- java.lang.Object
-
- org.agrona.concurrent.DynamicCompositeAgent
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
DynamicCompositeAgent.Status
Enum
to indicate the current status of aDynamicCompositeAgent
.
-
Field Summary
Fields Modifier and Type Field Description private java.util.concurrent.atomic.AtomicReference<Agent>
addAgent
private int
agentIndex
private Agent[]
agents
private static Agent[]
EMPTY_AGENTS
private java.util.concurrent.atomic.AtomicReference<Agent>
removeAgent
private java.lang.String
roleName
private DynamicCompositeAgent.Status
status
-
Constructor Summary
Constructors Constructor Description DynamicCompositeAgent(java.lang.String roleName)
Construct a new composite that has noAgent
s to begin with.DynamicCompositeAgent(java.lang.String roleName, java.util.List<? extends Agent> agents)
DynamicCompositeAgent(java.lang.String roleName, Agent... agents)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
add(Agent agent)
int
doWork()
An agent should implement this method to do its work.boolean
hasAddAgentCompleted()
Has the last successfultryAdd(Agent)
operation been processed in thedoWork()
cycle?boolean
hasRemoveAgentCompleted()
Has the lasttryRemove(Agent)
operation been processed in thedoWork()
cycle?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.private void
remove(Agent agent)
java.lang.String
roleName()
Get the name of this agent's role.DynamicCompositeAgent.Status
status()
Get theDynamicCompositeAgent.Status
for the Agent.boolean
tryAdd(Agent agent)
Try and add a newAgent
to the composite.boolean
tryRemove(Agent agent)
Try and remove anAgent
from the composite.
-
-
-
Field Detail
-
EMPTY_AGENTS
private static final Agent[] EMPTY_AGENTS
-
agentIndex
private int agentIndex
-
status
private volatile DynamicCompositeAgent.Status status
-
agents
private Agent[] agents
-
roleName
private final java.lang.String roleName
-
addAgent
private final java.util.concurrent.atomic.AtomicReference<Agent> addAgent
-
removeAgent
private final java.util.concurrent.atomic.AtomicReference<Agent> removeAgent
-
-
Constructor Detail
-
DynamicCompositeAgent
public DynamicCompositeAgent(java.lang.String roleName)
Construct a new composite that has noAgent
s to begin with.- Parameters:
roleName
- to be given forAgent.roleName()
.
-
DynamicCompositeAgent
public DynamicCompositeAgent(java.lang.String roleName, java.util.List<? extends Agent> agents)
- Parameters:
roleName
- to be given forAgent.roleName()
.agents
- the parts of this composite, at least one agent and no null agents allowed- Throws:
java.lang.NullPointerException
- if the array or any element is null
-
DynamicCompositeAgent
public DynamicCompositeAgent(java.lang.String roleName, Agent... agents)
- Parameters:
roleName
- to be given forAgent.roleName()
.agents
- the parts of this composite, at least one agent and no null agents allowed- Throws:
java.lang.NullPointerException
- if the array or any element is null
-
-
Method Detail
-
status
public DynamicCompositeAgent.Status status()
Get theDynamicCompositeAgent.Status
for the Agent.- Returns:
- the
DynamicCompositeAgent.Status
for the Agent.
-
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 may result in other agents not being started.
-
doWork
public int doWork() throws java.lang.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.
-
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.
-
roleName
public java.lang.String roleName()
Get the name of this agent's role.
-
tryAdd
public boolean tryAdd(Agent agent)
Try and add a newAgent
to the composite. This method does not block and will return false if another concurrent attempt to add is in progress.The agent will be added during the next invocation of
doWork()
if this operation is successful. If theAgent.onStart()
method throws an exception then it will not be added andAgent.onClose()
will be called.- Parameters:
agent
- to be added to the composite.- Returns:
- true is a successful add request is pending otherwise false if another concurrent add request is in progress.
- See Also:
hasAddAgentCompleted()
-
hasAddAgentCompleted
public boolean hasAddAgentCompleted()
Has the last successfultryAdd(Agent)
operation been processed in thedoWork()
cycle?- Returns:
- the last successful
tryAdd(Agent)
operation been processed in thedoWork()
cycle? - See Also:
tryAdd(Agent)
-
tryRemove
public boolean tryRemove(Agent agent)
Try and remove anAgent
from the composite. The agent is removed during the nextdoWork()
duty cycle if this operation is successful. This method does not block and will return false if another concurrent attempt to remove is in progress.The
Agent
is removed by identity. Only the first found is removed.- Parameters:
agent
- to be removed.- Returns:
- true is a successful remove request is pending otherwise false if another concurrent remove request is in progress.
- See Also:
hasRemoveAgentCompleted()
-
hasRemoveAgentCompleted
public boolean hasRemoveAgentCompleted()
Has the lasttryRemove(Agent)
operation been processed in thedoWork()
cycle?- Returns:
- the last
tryRemove(Agent)
operation been processed in thedoWork()
cycle? - See Also:
tryRemove(Agent)
-
add
private void add(Agent agent)
-
remove
private void remove(Agent agent)
-
-