|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object cz.cuni.amis.pogamut.base.agent.utils.runner.impl.AgentRunner<AGENT,PARAMS>
public abstract class AgentRunner<AGENT extends IAgent,PARAMS extends IAgentParameters>
BASIC USAGE
Class used for starting the agent with certain default parameters. This class can't be used alone
as it is abstract. That's because the GaviaLib does not provide any concrete agent implementation
you may instance. Instead, the GaviaLib must be used together with some environment-Pogamut bridge
that defines a concrete IAgent
with concrete IAgentParameters
.
The class provides start-synchronization behavior of respective agents via setPausing(boolean)
.
For more information see IAgentRunner.setPausing(boolean)
.
Note that the class also provides you with hook-methods that can be utilized to additionally configure
agent instances as they are created and started. These are preInitHook()
, preStartHook(IAgent)
,
AgentRunner#preResumeHook(IAgent[])
, postStartHook(IAgent)
and AgentRunner#postStartedHook(IAgent[])
.
This class is (almost complete) implementation that can instantiate and start one or multiple agents (of the same
class). The only thing that is left to be implemented is newDefaultAgentParameters()
that
are used to IAgentParameters.assignDefaults(IAgentParameters)
into user provided parameters (if any of
are provided).
Additional features:
setLogLevel(Level)
- allows you to set default logging level for the newly created agent (default Level.WARNING
)setConsoleLogging(boolean)
- allows you to attach default console logger (via IAgentLogger.addDefaultConsoleHandler()
)
This runner is based on the IAgentFactory
interface that is utilized to create new instances. It is advised
that concrete agent runners hides this fact from the user instantiating the factory themselves so the user
must not dive deep into GaviaLib architecture.
USING AgentRunner
FROM THE MAIN METHOD
There is one problem using Pogamut agents that comes from the decision to use JMX for the agents management.
If you start the agent in the main method and terminates it at some point in time - the JVM will not exit. That's
because of the rmi registry daemon thread that will hang up in the air preventing JVM from termination. This can
be handled by calling PogamutPlatform.close()
in the end, that shuts down the rmi registry.
If you happen to need to use AgentRunner
from the main method, do not forget to call setMain(boolean)
with param 'true'.
Field Summary | |
---|---|
protected java.util.List<AGENT> |
agents
List of started agents. |
protected boolean |
consoleLogging
Whether the console logging is enabled as default. |
protected java.util.logging.Level |
defaultLogLevel
Default log level that is set to the agent after its instantiation. |
protected IAgentFactory<AGENT,PARAMS> |
factory
Used to instantiate new agents. |
protected boolean |
killed
Whether we had to kill all agents (i.e., some exception/failure has happened). |
protected java.util.concurrent.CountDownLatch |
latch
Latch where we're awaiting till all agents finishes. |
protected FlagListener<IAgentState> |
listener
Listener that lowers the count on the MainAgentRunner#latch (if reaches zero, start method resumes and closes the Pogamut platform),
and watches for the agent's failure (MainAgentRunner#killAgents(IAgent[]) in this case). |
protected java.util.logging.Logger |
log
Use to log stuff. |
protected boolean |
main
Whether we should provide 'main' feature. |
protected java.lang.Object |
mutex
Mutex that synchronize killing of agents due to a failure. |
Constructor Summary | |
---|---|
AgentRunner(IAgentFactory<AGENT,PARAMS> factory)
The runner needs the 'factory' so it know how to construct (instantiate) new agents. |
Method Summary | |
---|---|
protected AGENT |
createAgentWithParams(PARAMS params)
Method that is used by AgentRunner#startAgentWithParams(IAgentParameters[]) to instantiate new
agents. |
protected void |
fillInDefaults(PARAMS params)
Fills defaults parameters into the 'params' by using newDefaultAgentParameters() . |
protected void |
fillInDefaults(PARAMS[] paramsArray)
Fills defaults parameters into every 'params' of the array by using newDefaultAgentParameters() ,
i.e., we're creating a new default parameters for every 'params' from the array. |
java.util.logging.Logger |
getLog()
|
boolean |
isMain()
Whether the runner is set to provide 'main' functionality (see the Javadoc for the whole class). |
boolean |
isPausing()
Tells, whether the pausing behavior is enabled. |
protected void |
killAgent(AGENT agent)
Kills a single agent instance, called during clean up when start/pause/resume of the agent fails. |
protected void |
killAgents(java.util.List<AGENT> agents)
This method is called whenever start/pause/resume of the single agent fails to clean up. |
protected IAgentId |
newAgentId(java.lang.String name)
Creates new AgentId from the 'name' and unique number that is automatically generated
from the ID . |
protected abstract IAgentParameters |
newDefaultAgentParameters()
Method that is called to provide another default parameters for newly created agents. |
protected void |
postStartedHook(java.util.List<AGENT> agents)
Custom hook called after all the agents have been instantiated by the factory
and started with IAgent.start() . |
protected void |
postStartHook(AGENT agent)
Custom hook called after the agent is instantiated by the factory and
started with IAgent.start() . |
protected void |
preInitHook()
Custom hook called before all the agents are going to be instantiated by the factory . |
protected void |
preResumeHook(java.util.List<AGENT> agents)
Custom hook called only iff isPausing() . |
protected void |
preStartHook(AGENT agent)
Custom hook called after the agent is instantiated by the factory and before
the IAgent.start() is called. |
AgentRunner<AGENT,PARAMS> |
setConsoleLogging(boolean enabled)
Allows you to disable/enable default console logging for the agent. |
AgentRunner<AGENT,PARAMS> |
setLog(java.util.logging.Logger log)
|
AgentRunner<AGENT,PARAMS> |
setLogLevel(java.util.logging.Level logLevel)
Sets default logging level for newly created agents (default is Level.WARNING ). |
AgentRunner<AGENT,PARAMS> |
setMain(boolean state)
Sets 'main' functionality (see the Javadoc for the whole class). |
AgentRunner<AGENT,PARAMS> |
setPausing(boolean state)
Sets the pausing behavior. |
AGENT |
startAgent()
Starts the agent by providing default parameters (defined during the construction of the implementor). |
protected void |
startAgent(AGENT agent)
Method that is used by AgentRunner#startAgentWithParams(IAgentParameters[]) to start newly
created agent. |
java.util.List<AGENT> |
startAgents(int count)
Starts agents by providing every one of them with default parameters (defined during the construction of the implementor). |
java.util.List<AGENT> |
startAgents(PARAMS... agentParameters)
Start an agent instance configured with 'agentsParameters'. |
protected java.util.List<AGENT> |
startAgentWithParams(boolean fillDefaults,
PARAMS... params)
This method should be internally used to create and start the batch of agent instances. |
protected java.util.List<AGENT> |
startAgentWithParamsMain(boolean fillDefaults,
PARAMS... params)
E.g. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected java.lang.Object mutex
protected IAgentFactory<AGENT extends IAgent,PARAMS extends IAgentParameters> factory
protected java.util.logging.Logger log
WARNING: may be null! Always check it before logging!
protected java.util.logging.Level defaultLogLevel
protected boolean consoleLogging
protected java.util.concurrent.CountDownLatch latch
protected java.util.List<AGENT extends IAgent> agents
protected boolean killed
protected boolean main
protected FlagListener<IAgentState> listener
MainAgentRunner#latch
(if reaches zero, start method resumes and closes the Pogamut platform),
and watches for the agent's failure (MainAgentRunner#killAgents(IAgent[])
in this case).
Constructor Detail |
---|
public AgentRunner(IAgentFactory<AGENT,PARAMS> factory)
factory
- preconfigured factory that is going to be used to produce new instances of agentsMethod Detail |
---|
public java.util.logging.Logger getLog()
public AgentRunner<AGENT,PARAMS> setLog(java.util.logging.Logger log)
public AGENT startAgent() throws PogamutException
IAgentRunner
startAgent
in interface IAgentRunner<AGENT extends IAgent,PARAMS extends IAgentParameters>
PogamutException
public java.util.List<AGENT> startAgents(int count) throws PogamutException
IAgentRunner
Note that if any instantiation/start of the agent fails, all agents are killed before the method throws the exception.
startAgents
in interface IAgentRunner<AGENT extends IAgent,PARAMS extends IAgentParameters>
count
- how many agents should be started
PogamutException
public java.util.List<AGENT> startAgents(PARAMS... agentParameters) throws PogamutException
IAgentRunner
Note that if any instantiation/start of the agent fails, all agents are killed before the method throws the exception.
startAgents
in interface IAgentRunner<AGENT extends IAgent,PARAMS extends IAgentParameters>
PogamutException
public boolean isPausing()
IAgentRunner
If enabled, the runner will pause all agents after their construction and resume them at once whenever all agents has been instantiated.
isPausing
in interface IAgentRunner<AGENT extends IAgent,PARAMS extends IAgentParameters>
public AgentRunner<AGENT,PARAMS> setPausing(boolean state)
IAgentRunner
If set true, the runner will pause all agents after their construction and resume them at once whenever all agents has been successfully started.
setPausing
in interface IAgentRunner<AGENT extends IAgent,PARAMS extends IAgentParameters>
public boolean isMain()
IAgentRunner
isMain
in interface IAgentRunner<AGENT extends IAgent,PARAMS extends IAgentParameters>
public AgentRunner<AGENT,PARAMS> setMain(boolean state)
IAgentRunner
setMain
in interface IAgentRunner<AGENT extends IAgent,PARAMS extends IAgentParameters>
public AgentRunner<AGENT,PARAMS> setLogLevel(java.util.logging.Level logLevel)
Level.WARNING
).
If set to null, no level is set as default to the agent logger.
This probably violates the way how logging should be set up, but is more transparent for beginners.
public AgentRunner<AGENT,PARAMS> setConsoleLogging(boolean enabled)
This probably violates the way how logging should be set up, but is more transparent for beginners.
enabled
-
protected java.util.List<AGENT> startAgentWithParams(boolean fillDefaults, PARAMS... params)
fillDefaults
- whether the method should fill default values into the 'params'params
-
protected java.util.List<AGENT> startAgentWithParamsMain(boolean fillDefaults, PARAMS... params)
startAgentWithParams(boolean, IAgentParameters[])
but
provides the blocking mechanism.
protected abstract IAgentParameters newDefaultAgentParameters()
Note that it might be the case, that some parameters can't be shared between agent instances, thus you might always need to provide a new parameters. This decision is up to you as an implementor (that means you must understand how these parameters are used by the particular agent, fail-safe behaviour is to always provide a new one).
Notice that the method does not require you to provide parameters of the type 'PARAMS' allowing you to provide any params as defaults.
protected IAgentId newAgentId(java.lang.String name)
AgentId
from the 'name' and unique number that is automatically generated
from the ID
.
name
- protected void fillInDefaults(PARAMS params)
newDefaultAgentParameters()
.
params
- protected void fillInDefaults(PARAMS[] paramsArray)
newDefaultAgentParameters()
,
i.e., we're creating a new default parameters for every 'params' from the array.
params
- protected AGENT createAgentWithParams(PARAMS params)
AgentRunner#startAgentWithParams(IAgentParameters[])
to instantiate new
agents. Uses factory
to do the job. It assumes the params were already configured
with defaults.
params
-
protected void startAgent(AGENT agent)
AgentRunner#startAgentWithParams(IAgentParameters[])
to start newly
created agent.
agent
- protected void killAgents(java.util.List<AGENT> agents)
Recalls killAgent(IAgent)
for every non-null agent instance.
agents
- some array elements may be null!protected void killAgent(AGENT agent)
agent
- protected void preInitHook() throws PogamutException
factory
.
May be utilized by the GaviaLib user to inject additional code into the runner.
PogamutException
protected void preStartHook(AGENT agent) throws PogamutException
factory
and before
the IAgent.start()
is called.
May be utilized by the GaviaLib user to inject additional code into the runner.
agent
-
PogamutException
protected void postStartHook(AGENT agent) throws PogamutException
factory
and
started with IAgent.start()
.
May be utilized by the GaviaLib user to inject additional code into the runner.
agent
-
PogamutException
protected void preResumeHook(java.util.List<AGENT> agents)
isPausing()
. This method is called after all the agents have been instantiated by the factory
and before they are resumed by IAgent.resume()
.
May be utilized by the GaviaLib user to inject additional code into the runner.
agents
- protected void postStartedHook(java.util.List<AGENT> agents)
factory
and started with IAgent.start()
.
May be utilized by the GaviaLib user to inject additional code into the runner.
agents
-
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |