fr.dyade.aaa.agent
Class MessageVector

java.lang.Object
  extended by fr.dyade.aaa.agent.MessageVector
All Implemented Interfaces:
MessageQueue

final class MessageVector
extends java.lang.Object
implements MessageQueue

Class MessageVector represents 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 save MessageVector object state.


Field Summary
private  int count
          The number of messages in this MessageVector object.
private  long cpt1
           
private  long cpt2
           
private  java.lang.Object[] data
          The array buffer into which the Message objects are stored in memory.
private  int first
          The index of the first message in the circular buffer.
private  org.objectweb.util.monolog.api.Logger logmon
           
private  java.lang.String logmsg
           
private  boolean persistent
           
private  int validated
          The number of validated message in this MessageQueue.
 
Constructor Summary
MessageVector(java.lang.String name, boolean persistent)
           
 
Method Summary
 Message get()
          Looks at the message at the top of this queue without removing it from the queue.
 Message get(long timeout)
          Looks at the message at the top of this queue without removing it from the queue.
private  Message getMessageAt(int index)
          Returns the message at the specified index.
 Message getMessageTo(short to)
          Looks at the first message of this queue where the destination server is the specified one.
 void insert(Message item)
          Insert a message in the queue, it should only be used during initialization for restoring the queue state.
private  void insertMessageAt(Message item, int index)
          Inserts the specified message to this MessageVector at the specified index.
 Message pop()
          Removes the message at the top of this queue.
 void push(Message item)
          Pushes a message onto the bottom of this queue.
(package private)  int remove(int stamp)
          Removes all messages with a stamp less than the specified one.
(package private)  Message removeExpired(long currentTimeMillis)
          Removes the first messages with a timestamp less than the specified one.
(package private)  void removeMessage(Message msg)
          Removes the specified message from the queue if exists.
private  void removeMessageAt(int index)
          Deletes the message at the specified index.
 int size()
          Returns the number of messages in this vector.
 java.lang.String toString()
          Returns a string representation of this MessageVector object.
 void validate()
          Atomically validates all messages pushed in queue during a reaction.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

logmon

private org.objectweb.util.monolog.api.Logger logmon

logmsg

private java.lang.String logmsg

cpt1

private long cpt1

cpt2

private long cpt2

data

private java.lang.Object[] data
The array buffer into which the Message objects are stored in memory. The capacity of this array buffer is at least large enough to contain all the messages of the MessageVector.

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
Constructor Detail

MessageVector

MessageVector(java.lang.String name,
              boolean persistent)
Method Detail

insert

public void insert(Message item)
Insert a message in the queue, it should only be used during initialization for restoring the queue state.

Specified by:
insert in interface MessageQueue
Parameters:
item - the message to be pushed onto this queue.

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:
push in interface MessageQueue
Parameters:
item - the message to be pushed onto this queue.

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:
pop in interface MessageQueue
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:
validate in interface MessageQueue

get

public Message get()
            throws java.lang.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:
get in interface MessageQueue
Returns:
the message at the top of this queue.
Throws:
java.lang.InterruptedException - if another thread has interrupted the current thread.

get

public Message get(long timeout)
            throws java.lang.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:
get in interface MessageQueue
Parameters:
timeout - the maximum time to wait in milliseconds.
Returns:
the message at the top of this queue.
Throws:
java.lang.InterruptedException - if another thread has interrupted the current thread.
java.lang.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.


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.


insertMessageAt

private void insertMessageAt(Message item,
                             int index)
Inserts the specified message to this MessageVector at 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:
size in interface MessageQueue
Returns:
the number of messages in this vector.

toString

public java.lang.String toString()
Returns a string representation of this MessageVector object. Be careful we scan the vector without synchronization, so the result can be incoherent.

Overrides:
toString in class java.lang.Object
Returns:
A string representation of this object.


Copyright © 2010 ScalAgent D.T.. All Rights Reserved.