Class Agent
- java.lang.Object
-
- fr.dyade.aaa.agent.Agent
-
- All Implemented Interfaces:
AgentMBean,Encodable,Serializable
- Direct Known Subclasses:
AgentFactory,AMQPAgent,Container,Destination,SynchronousAgent,UserAgent
public abstract class Agent extends Object implements AgentMBean, Serializable, Encodable
TheAgentclass represents the basic component in our model. agents are "reactive" objects which behave according to "event/reaction"model: an event embodies a significant state change which one or many agents may react to.Class
Agentdefines the generic interface and the common behavior for all agents; every agent is an object of a class deriving from class Agent. Agents are the elementary programming and execution entities; they only communicate using notifications through the message bus, and are controlled by the execution engine.The reactive behavior is implemented by function member React, which defines the reaction of the agent when receiving a notification; this function member is called by the execution engine.
Agents are persistent objects, and the Agent class realizes a "swap-in/swap-out" mechanism which allows loading (or finding) in main memory the agents to activate, and unloading the agents idle since a while.
Agents must be created in two steps:- locally creating the object in memory (via constructor),
- configure it (for example via get/set methods),
- the deploy it .
The following code would then create a simple agent and deploy it:
Agent ag = new Agent(); ag.deploy();- See Also:
Notification,Engine,Channel, Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description booleanagentProfilingBoolean value indicating if the agent profiling is on.(package private) longcommitTimeprotected static booleanDEBUG(package private) booleandeployedDetermines if the currentAgenthas already been deployed.static StringemptyStringprotected booleanfixedSome agents must be loaded at any time, this can be enforced by this member variable.(package private) AgentIdidGlobal unique identifier of the agent.(package private) longlastthelastvariable contains the virtual time of the last access.protected Loggerlogmon(package private) StringnameSymbolic name of the agent(package private) intreactNb(package private) longreactTime(package private) static longserialVersionUIDDefine serialVersionUID for interoperability.private booleanupdatedtrueif the agent state has changed.-
Fields inherited from interface fr.dyade.aaa.common.encoding.Encodable
BOOLEAN_ENCODED_SIZE, BYTE_ENCODED_SIZE, DOUBLE_ENCODED_SIZE, FLOAT_ENCODED_SIZE, INT_ENCODED_SIZE, LONG_ENCODED_SIZE, SHORT_ENCODED_SIZE
-
-
Constructor Summary
Constructors Constructor Description Agent()Allocates a new Agent object.Agent(boolean fixed)Allocates a new Agent object.Agent(short to)Allocates a new Agent object.Agent(short to, boolean fixed)Allocates a new Agent object.Agent(short to, String name)Allocates a new Agent object.Agent(short to, String name, boolean fixed)Allocates a new Agent object.Agent(String name)Allocates a new Agent object.Agent(String name, boolean fixed)Allocates a new Agent object.Agent(String name, boolean fixed, int stamp)Constructor used to build Well Known Services agents.Agent(String name, boolean fixed, AgentId id)Constructor used to build "system" agents likeAgentFactory.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidagentFinalize(boolean lastTime)Called to inform this agent that it is garbaged and that it should free any active resources that it has allocated.protected voidagentInitialize(boolean firstTime)Gives this agent an opportunity to initialize after having been deployed, and each time it is loaded into memory.protected voidagentSave()Enables the sub-classes to save their state.voiddecode(Decoder decoder)Decodes the object.voiddelete()Permits this agent to destroy itself.voiddelete(AgentId agent)Permits this agent to destroy itself.voiddelete(AgentId agent, Object extraInformation)Permits this agent to destroy itself.voiddeploy()Deploys a new agent.voiddeploy(AgentId reply)Deploys a new agent.protected voiddoAdminSyncNotification(AgentId from, AdminSyncNotification not)Default handling of AdminSyncNotification.voidencode(Encoder encoder)Encodes the object.private StringgetA3MBeanName()StringgetAgentId()Returns String format of the global unique identifier of the agent.longgetCommitTime()Returns the total commit time calculated for this agent.intgetEncodableClassId()Enables the sub classes not to implement this method.intgetEncodedSize()Returns the size of the encoded object.AgentIdgetId()Returns the global unique identifier of the agent.protected StringgetLogTopic()Returns default log topic for agents.StringgetName()Returns thisAgent's name.intgetReactNb()longgetReactTime()Returns the total reaction time calculated for this agent.booleanhasName()voidincWorkInProgress()Increments the tick counter that reflects activity in server.private voidinitState(String name, boolean fixed, AgentId id)booleanisAgentProfiling()Returns true if the agent profiling is on.booleanisDeployed()Returns if the currentlyAgenthas already been deployed.booleanisFixed()Tests if the agent is pinned in memory.booleanisUpdated()Indicates whether the agent has been updated or not.(package private) static Agentload(AgentId id)Restores the object state from the persistent storage.static Agentload(Transaction transaction, AgentId id)protected booleanneedToBeCommited()Indicates to the Engine component that a commit is needed.voidreact(AgentId from, Notification not)Defines the reaction of the agent when receiving a notification.private voidreadObject(ObjectInputStream in)voidresetCommitTime()reset the commitTimevoidresetReactTime()reset the reactTimevoidresetTimer()Reset reactTime and commitTimeprotected voidsave()Saves the agent state unless not requested.protected voidsendTo(AgentId to, Notification not)This method sends a notification to the agent which id is given in parameter.protected voidsendTo(RoleMultiple role, Notification not)Sends a notification to all the agents registered in a role.protected voidsendTo(Role role, Notification not)This method sends a notification to the agent which id is wrapped in the specified role.voidsetAgentProfiling(boolean agentProfiling)Sets the agent profiling.voidsetName(String name)Sets thisAgent's name.protected voidsetNoSave()Sets theupdatedfield tofalseso that the agent state is not saved after the current reaction; the field is set back totruefor the next reaction.protected voidsetSave()Sets theupdatedfield totrueso that the agent state is saved after the current reaction.StringtoString()Returns a string representation of this agent, including the agent's class, name, global identification, and fixed property.private voidwriteObject(ObjectOutputStream out)
-
-
-
Field Detail
-
serialVersionUID
static final long serialVersionUID
Define serialVersionUID for interoperability.- See Also:
- Constant Field Values
-
logmon
protected transient Logger logmon
-
DEBUG
protected static final boolean DEBUG
-
updated
private transient boolean updated
trueif the agent state has changed.This field value is initialized as
true, so that by default the agent state is saved after a reaction.
-
reactNb
transient int reactNb
-
agentProfiling
public boolean agentProfiling
Boolean value indicating if the agent profiling is on. If true, the cumulative time of reaction and commit is kept for this agent.
-
reactTime
transient long reactTime
-
commitTime
transient long commitTime
-
id
transient AgentId id
Global unique identifier of the agent. Each agent is identified by a unique identifier allowing the agent to be found. The identifiers format is detailed in AgentId class. Be careful, id is not part of persistent data, it must be initialized after transaction load.
-
name
transient String name
Symbolic name of the agent
-
fixed
protected transient boolean fixed
Some agents must be loaded at any time, this can be enforced by this member variable. Iftrueagent is pinned in memory.
-
last
transient long last
thelastvariable contains the virtual time of the last access. It is used by swap-out policy.
-
emptyString
public static final String emptyString
- See Also:
- Constant Field Values
-
deployed
transient boolean deployed
Determines if the currentAgenthas already been deployed.
-
-
Constructor Detail
-
Agent
public Agent()
Allocates a new Agent object. The resulting object is not an agent; before it can react to a notification you must deploy it. This constructor has the same effect asAgent(AgentServer.getServerId(), null, false).- See Also:
Agent(short, java.lang.String, boolean),deploy()
-
Agent
public Agent(boolean fixed)
Allocates a new Agent object. This constructor has the same effect asAgent(AgentServer.getServerId(), null, fixed).- Parameters:
fixed- iftrueagent is pinned in memory- See Also:
Agent(short, String, boolean)
-
Agent
public Agent(String name)
Allocates a new Agent object. This constructor has the same effect asAgent(AgentServer.getServerId(), name, false).- Parameters:
name- symbolic name- See Also:
Agent(short, java.lang.String, boolean)
-
Agent
public Agent(String name, boolean fixed)
Allocates a new Agent object. This constructor has the same effect asAgent(AgentServer.getServerId(), name, fixed).- Parameters:
name- symbolic namefixed- iftrueagent is pinned in memory- See Also:
Agent(short, java.lang.String, boolean)
-
Agent
public Agent(short to)
Allocates a new Agent object. This constructor has the same effect asAgent(to, null, false).- Parameters:
to- Identification of target agent server- See Also:
Agent(short, java.lang.String, boolean)
-
Agent
public Agent(short to, String name)Allocates a new Agent object. This constructor has the same effect asAgent(to, name, false).- Parameters:
to- Identification of target agent servername- symbolic name- See Also:
Agent(short, java.lang.String, boolean)
-
Agent
public Agent(short to, boolean fixed)Allocates a new Agent object. This constructor has the same effect asAgent(to, null, fixed).- Parameters:
to- Identification of target agent serverfixed- iftrueagent is pinned in memory- See Also:
Agent(short, java.lang.String, boolean)
-
Agent
public Agent(short to, String name, boolean fixed)Allocates a new Agent object. The resulting object is not an agent; before it can react to a notification you must deploy it.- Parameters:
to- Identification of target agent servername- symbolic namefixed- iftrueagent is pinned in memory- See Also:
deploy()
-
Agent
Agent(String name, boolean fixed, AgentId id)
Constructor used to build "system" agents likeAgentFactory. System agents are created from theagentpackage. This constructor takes the agent id as a parameter instead of building it.- Parameters:
name- symbolic namefixed- iftrueagent is pinned in memoryid- unique identifier
-
Agent
public Agent(String name, boolean fixed, int stamp)
Constructor used to build Well Known Services agents.System agents are created from the
agentpackage. WKS agents are similar to system agents, except that they may be defined in separate packages, and they do not necessarily exist on all agent servers. Their creation is controlled from the configuration file of the agent server.This constructor takes the agent id as a parameter instead of building it. Since the constructor has been made public, the consistency of agent ids allocation must be enforced. This is done by the constructor checking that the id stamp is comprised in the
AgentId.MinWKSIdStamp-AgentId.MaxWKSIdStampinterval.- Parameters:
name- symbolic namefixed- iftrueagent is pinned in memorystamp- well known stamp
-
-
Method Detail
-
getReactNb
public int getReactNb()
- Specified by:
getReactNbin interfaceAgentMBean- Returns:
- the reactNb
-
incWorkInProgress
public final void incWorkInProgress()
Increments the tick counter that reflects activity in server.
-
isAgentProfiling
public boolean isAgentProfiling()
Returns true if the agent profiling is on.- Specified by:
isAgentProfilingin interfaceAgentMBean- Returns:
- true if the agent profiling is on.
- See Also:
EngineMBean.isAgentProfiling()
-
setAgentProfiling
public void setAgentProfiling(boolean agentProfiling)
Sets the agent profiling.- Specified by:
setAgentProfilingin interfaceAgentMBean- Parameters:
agentProfiling- if true sets the agent profiling.- See Also:
EngineMBean.setAgentProfiling(boolean)
-
getReactTime
public long getReactTime()
Description copied from interface:AgentMBeanReturns the total reaction time calculated for this agent.- Specified by:
getReactTimein interfaceAgentMBean- Returns:
- the reactTime
-
resetReactTime
public void resetReactTime()
reset the reactTime- Specified by:
resetReactTimein interfaceAgentMBean
-
getCommitTime
public long getCommitTime()
Description copied from interface:AgentMBeanReturns the total commit time calculated for this agent.- Specified by:
getCommitTimein interfaceAgentMBean- Returns:
- the commitTime
-
resetCommitTime
public void resetCommitTime()
reset the commitTime- Specified by:
resetCommitTimein interfaceAgentMBean
-
resetTimer
public void resetTimer()
Reset reactTime and commitTime- Specified by:
resetTimerin interfaceAgentMBean
-
setNoSave
protected void setNoSave()
Sets theupdatedfield tofalseso that the agent state is not saved after the current reaction; the field is set back totruefor the next reaction.
-
setSave
protected void setSave()
Sets theupdatedfield totrueso that the agent state is saved after the current reaction.
-
isUpdated
public boolean isUpdated()
Indicates whether the agent has been updated or not.- Returns:
- true if the agent has been updated.
-
needToBeCommited
protected final boolean needToBeCommited()
Indicates to the Engine component that a commit is needed.- Returns:
- true if there is no error.
-
save
protected final void save() throws IOExceptionSaves the agent state unless not requested.- Throws:
IOException- if any error occurs.
-
agentSave
protected void agentSave() throws IOExceptionEnables the sub-classes to save their state.- Throws:
IOException- if any error occurs.
-
load
static final Agent load(AgentId id) throws IOException, ClassNotFoundException
Restores the object state from the persistent storage.- Parameters:
id- The agent unique identifier.- Returns:
- The agent object.
- Throws:
IOException- when accessing the stored image.ClassNotFoundException- if the stored image class may not be found.
-
load
public static final Agent load(Transaction transaction, AgentId id) throws IOException, ClassNotFoundException
- Throws:
IOExceptionClassNotFoundException
-
hasName
public boolean hasName()
-
getName
public String getName()
Returns thisAgent's name. If the name is not set returns the string representation of its unique id.- Specified by:
getNamein interfaceAgentMBean- Returns:
- this
Agent's name.
-
setName
public void setName(String name)
Sets thisAgent's name.- Parameters:
name- theAgent's name.
-
getLogTopic
protected String getLogTopic()
Returns default log topic for agents. Its method should be overridden in subclass in order to permit fine configuration of logging system. By default it returnsDebug.A3Agent.- Returns:
- the default log topic for agents.
-
writeObject
private void writeObject(ObjectOutputStream out) throws IOException
- Throws:
IOException
-
readObject
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
- Throws:
IOExceptionClassNotFoundException
-
isDeployed
public boolean isDeployed()
Returns if the currentlyAgenthas already been deployed.- Returns:
- true if the current agent has already been deployed.
-
deploy
public final void deploy() throws IOExceptionDeploys a new agent. It works by sending a notification to a special agent, of class Factory, running on the target agent server. The notification asks for a remote creation of the agent. This solution presents the advantage of reusing the standard communication mechanisms of the agent machine.The whole process involves then the following steps:
- serializing the object state,
- building an
AgentCreateRequestnotification with the resulting bytes stream, - sending it to the target Factory agent.
- Throws:
IOException- unspecialized exception
-
deploy
public final void deploy(AgentId reply) throws IOException
Deploys a new agent. It works as deploy() method above; after the agent creation, the Factory agent sends anAgentCreateReplynotification.- Parameters:
reply- agent to reply to- Throws:
IOException- unspecialized exception
-
toString
public String toString()
Returns a string representation of this agent, including the agent's class, name, global identification, and fixed property.- Specified by:
toStringin interfaceAgentMBean- Overrides:
toStringin classObject- Returns:
- A string representation of this agent.
-
getAgentId
public final String getAgentId()
Returns String format of the global unique identifier of the agent.- Specified by:
getAgentIdin interfaceAgentMBean- Returns:
- the global unique identifier of the agent.
-
getId
public final AgentId getId()
Returns the global unique identifier of the agent. Each agent is identified by a unique identifier allowing the agent to be found. The identifiers format is detailed in AgentId class.- Returns:
- the global unique identifier of the agent.
-
isFixed
public final boolean isFixed()
Tests if the agent is pinned in memory.- Specified by:
isFixedin interfaceAgentMBean- Returns:
- true if this agent is a pinned in memory; false otherwise.
-
agentInitialize
protected void agentInitialize(boolean firstTime) throws ExceptionGives this agent an opportunity to initialize after having been deployed, and each time it is loaded into memory.This function is first called by the factory agent, just after it deploys the agent.
This function is used by agents with a
fixedfield set totrueto initialize their transient variables, as it is called each time the agent server is restarted.This function is not declared
finalso that derived classes may change their reload policy. The implementation of this method provided by theAgentclass just registers the JMS MBean.- Parameters:
firstTime- true when first called by the factory- Throws:
Exception- unspecialized exception
-
getA3MBeanName
private final String getA3MBeanName()
-
sendTo
protected final void sendTo(AgentId to, Notification not)
This method sends a notification to the agent which id is given in parameter. During an agent reaction alls notifications sent are buffered until reaction commit.Be careful if you use this method outside of an agent reaction, its behavior is slightly different: each notification is immediately sent using a local transaction.
- Parameters:
to- the unique id. of destinationAgent.not- the notification to send.- See Also:
Channel.sendTo(fr.dyade.aaa.agent.AgentId, fr.dyade.aaa.agent.Notification)
-
sendTo
protected final void sendTo(Role role, Notification not)
This method sends a notification to the agent which id is wrapped in the specified role.- Parameters:
role- the destinationRole.not- the notification to send.
-
sendTo
protected final void sendTo(RoleMultiple role, Notification not)
Sends a notification to all the agents registered in a role.- Parameters:
role- the destinationMultiplRole.not- the notification to send.
-
delete
public void delete()
Permits this agent to destroy itself. If necessary, this method should be overloaded to work properly.- Specified by:
deletein interfaceAgentMBean
-
delete
public void delete(AgentId agent)
Permits this agent to destroy itself. If necessary, this method should be overloaded to work properly.- Parameters:
agent- Id of agent to notify.
-
delete
public void delete(AgentId agent, Object extraInformation)
Permits this agent to destroy itself. If necessary, this method should be overloaded to work properly.- Parameters:
agent- Id of agent to notify.extraInformation- extra information added when notifying the agent.
-
react
public void react(AgentId from, Notification not) throws Exception
Defines the reaction of the agent when receiving a notification. This member function implements the common reactive behavior of an agent, it is called by the execution engine (see Engine class).If there is no corresponding reaction, the agent send an
UnknownNotificationnotification to the sender.- Parameters:
from- agent sending notificationnot- notification to react to- Throws:
Exception- unspecialized exception
-
doAdminSyncNotification
protected void doAdminSyncNotification(AgentId from, AdminSyncNotification not)
Default handling of AdminSyncNotification. Currently there is no command defined at the Agent level, this method just logs a warning, and unlocks the caller.- Parameters:
from-not-
-
agentFinalize
public void agentFinalize(boolean lastTime)
Called to inform this agent that it is garbaged and that it should free any active resources that it has allocated. A subclass ofAgentshould override this method if it has any operation that it wants to perform before it is garbaged. For example, an agent with threads (a ProxyAgent for example) would use the initialize method to create the threads and theagentFinalizemethod to stop them.Be careful, the notification sending is not allowed in this method.
The implementation of this method provided by the
Agentclass just unregister the JMX MBean if needed.- Parameters:
lastTime- true when last called by the factory on agent deletion.
-
getEncodableClassId
public int getEncodableClassId()
Enables the sub classes not to implement this method.- Specified by:
getEncodableClassIdin interfaceEncodable- Returns:
- -1
-
getEncodedSize
public int getEncodedSize() throws ExceptionReturns the size of the encoded object.- Specified by:
getEncodedSizein interfaceEncodable- Returns:
- the size of the encoded object
- Throws:
Exception- if an error occurs
-
-