public abstract class Agent extends Object implements AgentMBean, Serializable, Encodable
Agent
class 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 Agent
defines 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.
The following code would then create a simple agent and deploy it:
Agent ag = new Agent(); ag.deploy();
Notification
,
Engine
,
Channel
,
Serialized FormModifier and Type | Field and Description |
---|---|
boolean |
agentProfiling
Boolean value indicating if the agent profiling is on.
|
(package private) long |
commitTime |
(package private) boolean |
deployed
Determines if the current
Agent has already been deployed. |
static String |
emptyString |
protected boolean |
fixed
Some agents must be loaded at any time, this can be enforced by this
member variable.
|
(package private) AgentId |
id
Global unique identifier of the agent.
|
(package private) long |
last
the
last variable contains the virtual time of the
last access. |
protected org.objectweb.util.monolog.api.Logger |
logmon |
(package private) String |
name
Symbolic name of the agent
|
(package private) int |
reactNb |
(package private) long |
reactTime |
(package private) static long |
serialVersionUID
Define serialVersionUID for interoperability.
|
private boolean |
updated
true if the agent state has changed. |
BOOLEAN_ENCODED_SIZE, BYTE_ENCODED_SIZE, DOUBLE_ENCODED_SIZE, FLOAT_ENCODED_SIZE, INT_ENCODED_SIZE, LONG_ENCODED_SIZE, SHORT_ENCODED_SIZE
Constructor and 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,
AgentId id)
Constructor used to build "system" agents like
AgentFactory . |
Agent(String name,
boolean fixed,
int stamp)
Constructor used to build Well Known Services agents.
|
Modifier and Type | Method and Description |
---|---|
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.
|
protected void |
agentInitialize(boolean firstTime)
Gives this agent an opportunity to initialize after having been deployed,
and each time it is loaded into memory.
|
protected void |
agentSave()
Enables the sub-classes to save their state.
|
void |
decode(Decoder decoder)
Decodes the object.
|
void |
delete()
Permits this agent to destroy itself.
|
void |
delete(AgentId agent)
Permits this agent to destroy itself.
|
void |
delete(AgentId agent,
Object extraInformation)
Permits this agent to destroy itself.
|
void |
deploy()
Deploys a new agent.
|
void |
deploy(AgentId reply)
Deploys a new agent.
|
void |
encode(Encoder encoder)
Encodes the object.
|
String |
getAgentId()
Returns String format of the global unique identifier of the agent.
|
long |
getCommitTime()
Returns the total commit time calculated for this agent.
|
int |
getEncodableClassId()
Enables the sub classes not to implement this method.
|
int |
getEncodedSize()
Returns the size of the encoded object.
|
AgentId |
getId()
Returns the global unique identifier of the agent.
|
protected String |
getLogTopic()
Returns default log topic for agents.
|
private String |
getMBeanName() |
String |
getName()
Returns this
Agent 's name. |
int |
getReactNb() |
long |
getReactTime()
Returns the total reaction time calculated for this agent.
|
boolean |
hasName() |
void |
incWorkInProgress()
Increments the tick counter that reflects activity in server.
|
private void |
initState(String name,
boolean fixed,
AgentId id) |
boolean |
isAgentProfiling()
Returns true if the agent profiling is on.
|
boolean |
isDeployed()
Returns if the currently
Agent has already been deployed. |
boolean |
isFixed()
Tests if the agent is pinned in memory.
|
boolean |
isUpdated()
Indicates whether the agent has been updated or not.
|
(package private) static Agent |
load(AgentId id)
Restores the object state from the persistent storage.
|
protected boolean |
needToBeCommited()
Indicates to the Engine component that a commit is needed.
|
void |
react(AgentId from,
Notification not)
Defines the reaction of the agent when receiving a notification.
|
private void |
readObject(ObjectInputStream in) |
void |
resetCommitTime()
reset the commitTime
|
void |
resetReactTime()
reset the reactTime
|
void |
resetTimer()
Reset reactTime and commitTime
|
protected void |
save()
Saves the agent state unless not requested.
|
protected void |
sendTo(AgentId to,
Notification not)
This method sends a notification to the agent which id is given in
parameter.
|
protected void |
sendTo(RoleMultiple role,
Notification not)
Sends a notification to all the agents registered in a role.
|
protected void |
sendTo(Role role,
Notification not)
This method sends a notification to the agent which id is wrapped
in the specified role.
|
void |
setAgentProfiling(boolean agentProfiling)
Sets the agent profiling.
|
void |
setName(String name)
Sets this
Agent 's name. |
protected void |
setNoSave()
Sets the
updated field to false so that the
agent state is not saved after the current reaction; the field is set
back to true for the next reaction. |
protected void |
setSave()
Sets the
updated field to true so that the
agent state is saved after the current reaction. |
String |
toString()
Returns a string representation of this agent, including the agent's
class, name, global identification, and fixed property.
|
private void |
writeObject(ObjectOutputStream out) |
static final long serialVersionUID
private transient boolean updated
true
if the agent state has changed.
This field value is initialized as true
, so that by default
the agent state is saved after a reaction.
transient int reactNb
public boolean agentProfiling
transient long reactTime
transient long commitTime
transient AgentId id
transient String name
protected transient boolean fixed
true
agent is pinned in memory.protected transient org.objectweb.util.monolog.api.Logger logmon
transient long last
last
variable contains the virtual time of the
last access. It is used by swap-out policy.public static final String emptyString
transient boolean deployed
Agent
has already been deployed.public Agent()
Agent(AgentServer.getServerId(), null, false)
.Agent(short, java.lang.String, boolean)
,
deploy()
public Agent(boolean fixed)
Agent(AgentServer.getServerId(), null, fixed)
.fixed
- if true
agent is pinned in memoryAgent(short, String, boolean)
public Agent(String name)
Agent(AgentServer.getServerId(), name, false)
.name
- symbolic nameAgent(short, java.lang.String, boolean)
public Agent(String name, boolean fixed)
Agent(AgentServer.getServerId(), name, fixed)
.name
- symbolic namefixed
- if true
agent is pinned in memoryAgent(short, java.lang.String, boolean)
public Agent(short to)
Agent(to, null, false)
.to
- Identification of target agent serverAgent(short, java.lang.String, boolean)
public Agent(short to, String name)
Agent(to, name, false)
.to
- Identification of target agent servername
- symbolic nameAgent(short, java.lang.String, boolean)
public Agent(short to, boolean fixed)
Agent(to, null, fixed)
.to
- Identification of target agent serverfixed
- if true
agent is pinned in memoryAgent(short, java.lang.String, boolean)
public Agent(short to, String name, boolean fixed)
to
- Identification of target agent servername
- symbolic namefixed
- if true
agent is pinned in memorydeploy()
Agent(String name, boolean fixed, AgentId id)
AgentFactory
.
System agents are created from the agent
package. This
constructor takes the agent id as a parameter instead of building it.name
- symbolic namefixed
- if true
agent is pinned in memoryid
- unique identifierpublic Agent(String name, boolean fixed, int stamp)
System agents are created from the agent
package.
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.MaxWKSIdStamp
interval.
name
- symbolic namefixed
- if true
agent is pinned in memorystamp
- well known stamppublic int getReactNb()
getReactNb
in interface AgentMBean
public final void incWorkInProgress()
public boolean isAgentProfiling()
isAgentProfiling
in interface AgentMBean
EngineMBean.isAgentProfiling()
public void setAgentProfiling(boolean agentProfiling)
setAgentProfiling
in interface AgentMBean
agentProfiling
- if true sets the agent profiling.EngineMBean.setAgentProfiling(boolean)
public long getReactTime()
AgentMBean
getReactTime
in interface AgentMBean
public void resetReactTime()
resetReactTime
in interface AgentMBean
public long getCommitTime()
AgentMBean
getCommitTime
in interface AgentMBean
public void resetCommitTime()
resetCommitTime
in interface AgentMBean
public void resetTimer()
resetTimer
in interface AgentMBean
protected void setNoSave()
updated
field to false
so that the
agent state is not saved after the current reaction; the field is set
back to true
for the next reaction.protected void setSave()
updated
field to true
so that the
agent state is saved after the current reaction.public boolean isUpdated()
protected final boolean needToBeCommited()
protected final void save() throws IOException
IOException
- if any error occurs.protected void agentSave() throws IOException
IOException
- if any error occurs.static final Agent load(AgentId id) throws IOException, ClassNotFoundException
id
- The agent unique identifier.IOException
- when accessing the stored image.ClassNotFoundException
- if the stored image class may not be found.public boolean hasName()
public String getName()
Agent
's name.
If the name is not set returns the string representation of its unique id.getName
in interface AgentMBean
Agent
's name.public void setName(String name)
Agent
's name.name
- the Agent
's name.protected String getLogTopic()
Debug.A3Agent
.private void writeObject(ObjectOutputStream out) throws IOException
IOException
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
IOException
ClassNotFoundException
public boolean isDeployed()
Agent
has already been deployed.public final void deploy() throws IOException
The whole process involves then the following steps:
AgentCreateRequest
notification with the
resulting bytes stream,
IOException
- unspecialized exceptionpublic final void deploy(AgentId reply) throws IOException
AgentCreateReply
notification.reply
- agent to reply toIOException
- unspecialized exceptionpublic String toString()
toString
in interface AgentMBean
toString
in class Object
public final String getAgentId()
getAgentId
in interface AgentMBean
public final AgentId getId()
public final boolean isFixed()
isFixed
in interface AgentMBean
protected void agentInitialize(boolean firstTime) throws Exception
This function is first called by the factory agent, just after it deploys the agent.
This function is used by agents with a fixed
field set to
true
to initialize their transient variables, as it is called
each time the agent server is restarted.
This function is not declared final
so that derived classes
may change their reload policy. The implementation of this method provided
by the Agent
class just registers the JMS MBean.
firstTime
- true when first called by the factoryException
- unspecialized exceptionprivate String getMBeanName()
protected final void sendTo(AgentId to, Notification not)
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.
to
- the unique id. of destination Agent
.not
- the notification to send.Channel.sendTo(fr.dyade.aaa.agent.AgentId, fr.dyade.aaa.agent.Notification)
protected final void sendTo(Role role, Notification not)
role
- the destination Role
.not
- the notification to send.protected final void sendTo(RoleMultiple role, Notification not)
role
- the destination MultiplRole
.not
- the notification to send.public void delete()
delete
in interface AgentMBean
public void delete(AgentId agent)
agent
- Id of agent to notify.public void delete(AgentId agent, Object extraInformation)
agent
- Id of agent to notify.extraInformation
- extra information added when notifying the agent.public void react(AgentId from, Notification not) throws Exception
If there is no corresponding reaction, the agent send an
UnknownNotification
notification to the sender.
from
- agent sending notificationnot
- notification to react toException
- unspecialized exceptionpublic void agentFinalize(boolean lastTime)
Agent
should 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 the agentFinalize
method to
stop them.
Be careful, the notification sending is not allowed in this method.
The implementation of this method provided by the Agent
class
just unregister the JMX MBean if needed.
lastTime
- true when last called by the factory on agent deletion.public int getEncodableClassId()
getEncodableClassId
in interface Encodable
public int getEncodedSize() throws Exception
getEncodedSize
in interface Encodable
Exception
- if an error occursCopyright © 2021 ScalAgent D.T.. All rights reserved.