Package fr.dyade.aaa.agent
Class MessageVector
- java.lang.Object
-
- fr.dyade.aaa.agent.MessageVector
-
- All Implemented Interfaces:
MessageQueue
final class MessageVector extends Object implements MessageQueue
ClassMessageVectorrepresents a persistent vector of Message (source and target agent identifier, notification). As messages have a relatively short life span, then the messages are kept in main memory. If possible, the list is backed by a persistent image on the disk for reliability needs. In this case, we can use SoftReference to avoid memory overflow.
The stamp information in Message is used to restore the queue from persistent storage at initialization time, so there is no longer need to saveMessageVectorobject state.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classMessageVector.Counter
-
Field Summary
Fields Modifier and Type Field Description private intcountThe number of messages in this MessageVector object.(package private) Hashtable<Class,MessageVector.Counter>countersprivate longcpt1private longcpt2private Object[]dataThe array buffer into which theMessageobjects are stored in memory.private static booleanDEBUGprivate intfirstThe index of the first message in the circular buffer.private Loggerlogmonprivate Stringlogmsgstatic StringMSG_TYPES_TRACKINGName of property allowing to track the distribution of the types of messages, by default false.private static booleanmsgTypesTrackingTrue if the tracking of the distribution of messages type is allowed.private booleanpersistentprivate intvalidatedThe number of validated message in this MessageQueue.
-
Constructor Summary
Constructors Constructor Description MessageVector(String name, boolean persistent)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private voiddec(Notification not)Messageget()Looks at the message at the top of this queue without removing it from the queue.Messageget(long timeout)Looks at the message at the top of this queue without removing it from the queue.private MessagegetMessageAt(int index)Returns the message at the specified index.MessagegetMessageTo(short to)Looks at the first message of this queue where the destination server is the specified one.private voidinc(Notification not)voidinsert(Message item, MessageComparator comparator)Insert a message in the queue, it should only be used during initialization for restoring the queue state.private voidinsertMessageAt(Message item, int index)Inserts the specified message to thisMessageVectorat the specified index.Messagepop()Removes the message at the top of this queue.voidpush(Message item)Pushes a message onto the bottom of this queue.voidpushAndValidate(Message item)Pushes and validates a message.(package private) intremove(int stamp)Removes all messages with a stamp less than the specified one.(package private) MessageremoveExpired(long currentTimeMillis)Removes the first messages with a timestamp less than the specified one.(package private) voidremoveMessage(Message msg)Removes the specified message from the queue if exists.private voidremoveMessageAt(int index)Deletes the message at the specified index.Stringreport()Returns a report about the distribution of messages type in queue.intsize()Returns the number of messages in this vector.StringtoString()Returns a string representation of thisMessageVectorobject.voidvalidate()Atomically validates all messages pushed in queue during a reaction.
-
-
-
Field Detail
-
DEBUG
private static final boolean DEBUG
-
logmon
private Logger logmon
-
logmsg
private String logmsg
-
cpt1
private long cpt1
-
cpt2
private long cpt2
-
data
private Object[] data
The array buffer into which theMessageobjects are stored in memory. The capacity of this array buffer is at least large enough to contain all the messages of theMessageVector.Messages are stored in a circular way, first one in data[first] through data[(first+count-1)%length]. Any other array elements are null.
-
first
private int first
The index of the first message in the circular buffer.
-
count
private int count
The number of messages in this MessageVector object. Components data[first] through data[(first+count-1)%length] are the actual items.
-
validated
private int validated
The number of validated message in this MessageQueue.
-
persistent
private boolean persistent
-
MSG_TYPES_TRACKING
public static final String MSG_TYPES_TRACKING
Name of property allowing to track the distribution of the types of messages, by default false. If true, for each type of messages the total number of messages sent since the beginning and the number of those waiting is counted.This property can be fixed either from
javalaunching command or a3servers.xml configuration file.- See Also:
- Constant Field Values
-
msgTypesTracking
private static final boolean msgTypesTracking
True if the tracking of the distribution of messages type is allowed.
-
counters
Hashtable<Class,MessageVector.Counter> counters
-
-
Constructor Detail
-
MessageVector
MessageVector(String name, boolean persistent)
-
-
Method Detail
-
insert
public void insert(Message item, MessageComparator comparator)
Insert a message in the queue, it should only be used during initialization for restoring the queue state. This algorithm takes into account the reinitialization of the stamp and makes it possible to suitably sort messages having been created before this reset.- Specified by:
insertin interfaceMessageQueue- Parameters:
item- the message to be pushed onto this queue.comparator- the MessageComparator interface of MessageConsumer.
-
push
public void push(Message item)
Pushes a message onto the bottom of this queue. It should only be used during a transaction. The item will be really available after the transaction commit and the queue validate.- Specified by:
pushin interfaceMessageQueue- Parameters:
item- the message to be pushed onto this queue.
-
pushAndValidate
public void pushAndValidate(Message item)
Pushes and validates a message. This method should not be used during a transaction.- Specified by:
pushAndValidatein interfaceMessageQueue- Parameters:
item- the message to be pushed and validated
-
pop
public Message pop() throws EmptyQueueException
Removes the message at the top of this queue. It must only be used during a transaction.- Specified by:
popin interfaceMessageQueue- Returns:
- The message at the top of this queue.
- Throws:
EmptyQueueException- if this queue is empty.
-
validate
public void validate()
Atomically validates all messages pushed in queue during a reaction. It must only be used during a transaction.- Specified by:
validatein interfaceMessageQueue
-
get
public Message get() throws InterruptedException
Looks at the message at the top of this queue without removing it from the queue. It should never be used during a transaction to avoid dead-lock problems.- Specified by:
getin interfaceMessageQueue- Returns:
- the message at the top of this queue.
- Throws:
InterruptedException- if another thread has interrupted the current thread.
-
get
public Message get(long timeout) throws InterruptedException
Looks at the message at the top of this queue without removing it from the queue. It should never be used during a transaction to avoid dead-lock problems. It waits until a message is available or the specified amount of time has elapsed.- Specified by:
getin interfaceMessageQueue- Parameters:
timeout- the maximum time to wait in milliseconds.- Returns:
- the message at the top of this queue.
- Throws:
InterruptedException- if another thread has interrupted the current thread.IllegalArgumentException- if the value of timeout is negative.
-
getMessageTo
public Message getMessageTo(short to)
Looks at the first message of this queue where the destination server is the specified one. The message is not removed from the queue. It should never be used during a transaction to avoid dead-lock problems.- Parameters:
to- the unique server id.- Returns:
- the corresponding message or null if none .
-
removeMessage
void removeMessage(Message msg)
Removes the specified message from the queue if exists.- Parameters:
msg- the message to remove.
-
remove
int remove(int stamp)
Removes all messages with a stamp less than the specified one. Be careful with the use of this method, in particular it does not take in account the multiples incoming nodes.- Parameters:
stamp- the barrier stamp.- Returns:
- the number of removed messages.
-
removeExpired
Message removeExpired(long currentTimeMillis)
Removes the first messages with a timestamp less than the specified one. Be careful with the use of this method, in particular it does not take in account the multiples incoming nodes.- Parameters:
currentTimeMillis- the timestamp- Returns:
- the first messages with a timestamp less than the specified one.
-
insertMessageAt
private void insertMessageAt(Message item, int index)
Inserts the specified message to thisMessageVectorat the specified index. Each component in this vector with an index greater or equal to the specified index is shifted upward.- Parameters:
item- the message to be pushed onto this queue.index- where to insert the new message.
-
getMessageAt
private Message getMessageAt(int index)
Returns the message at the specified index.- Parameters:
index- the index of the message.- Returns:
- The message at the top of this queue.
-
removeMessageAt
private void removeMessageAt(int index)
Deletes the message at the specified index.- Parameters:
index- the index of the message to remove.
-
size
public int size()
Returns the number of messages in this vector.- Specified by:
sizein interfaceMessageQueue- Returns:
- the number of messages in this vector.
-
toString
public String toString()
Returns a string representation of thisMessageVectorobject. Be careful we scan the vector without synchronization, so the result can be incoherent.
-
inc
private void inc(Notification not)
-
dec
private void dec(Notification not)
-
report
public String report()
Returns a report about the distribution of messages type in queue.- Specified by:
reportin interfaceMessageQueue- Returns:
- a report about the distribution of messages type in queue.
-
-