Class DynamicCompositeAgent
- All Implemented Interfaces:
Agent
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumEnumto indicate the current status of aDynamicCompositeAgent. -
Constructor Summary
ConstructorsConstructorDescriptionDynamicCompositeAgent(String roleName) Construct a new composite that has noAgents to begin with.DynamicCompositeAgent(String roleName, List<? extends Agent> agents) Construct a new composite with a given listAgents to begin with.DynamicCompositeAgent(String roleName, Agent... agents) Construct a new composite with a given listAgents to begin with. -
Method Summary
Modifier and TypeMethodDescriptionintdoWork()An agent should implement this method to do its work.booleanHas the last successfultryAdd(Agent)operation been processed in thedoWork()cycle?booleanHas the lasttryRemove(Agent)operation been processed in thedoWork()cycle?voidonClose()To be overridden by Agents that need to do resource cleanup on close.voidonStart()To be overridden by Agents that need to do resource init on start.roleName()Get the name of this agent's role.status()Get theDynamicCompositeAgent.Statusfor the Agent.booleanTry and add a newAgentto the composite.booleanTry and remove anAgentfrom the composite.
-
Constructor Details
-
DynamicCompositeAgent
Construct a new composite that has noAgents to begin with.- Parameters:
roleName- to be given forAgent.roleName().
-
DynamicCompositeAgent
Construct a new composite with a given listAgents to begin with.- Parameters:
roleName- to be given forAgent.roleName().agents- the parts of this composite, at least one agent and no null agents allowed.- Throws:
NullPointerException- if the array or any element is null.
-
DynamicCompositeAgent
Construct a new composite with a given listAgents to begin with.- Parameters:
roleName- to be given forAgent.roleName().agents- the parts of this composite, at least one agent and no null agents allowed.- Throws:
NullPointerException- if the array or any element is null.
-
-
Method Details
-
status
Get theDynamicCompositeAgent.Statusfor the Agent.- Returns:
- the
DynamicCompositeAgent.Statusfor 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
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
AgentTerminationExceptioncan 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:
onClosein interfaceAgent- Throws:
RuntimeException- if any sub-agent throws an exception onClose. The agents exceptions are collected as suppressed exceptions in the thrown exception.
-
roleName
Get the name of this agent's role. -
tryAdd
Try and add a newAgentto 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
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:
-
tryRemove
Try and remove anAgentfrom 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
Agentis 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
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:
-