Class MessageVector

  • All Implemented Interfaces:
    MessageQueue

    final class MessageVector
    extends 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.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      (package private) static class  MessageVector.Counter  
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private int count
      The number of messages in this MessageVector object.
      (package private) Hashtable<Class,​MessageVector.Counter> counters  
      private long cpt1  
      private long cpt2  
      private Object[] data
      The array buffer into which the Message objects are stored in memory.
      private static boolean DEBUG  
      private int first
      The index of the first message in the circular buffer.
      private Logger logmon  
      private String logmsg  
      static String MSG_TYPES_TRACKING
      Name of property allowing to track the distribution of the types of messages, by default false.
      private static boolean msgTypesTracking
      True if the tracking of the distribution of messages type is allowed.
      private boolean persistent  
      private int validated
      The 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 void dec​(Notification not)  
      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.
      private void inc​(Notification not)  
      void insert​(Message item, MessageComparator comparator)
      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.
      void pushAndValidate​(Message item)
      Pushes and validates a message.
      (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.
      String report()
      Returns a report about the distribution of messages type in queue.
      int size()
      Returns the number of messages in this vector.
      String toString()
      Returns a string representation of this MessageVector object.
      void validate()
      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 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
      • 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 java launching 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.
    • 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:
        insert in interface MessageQueue
        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:
        push in interface MessageQueue
        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:
        pushAndValidate in interface MessageQueue
        Parameters:
        item - the message to be pushed and validated
      • 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 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:
        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:
        get in interface MessageQueue
        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 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 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 Object
        Returns:
        A string representation of this object.
      • report

        public String report()
        Returns a report about the distribution of messages type in queue.
        Specified by:
        report in interface MessageQueue
        Returns:
        a report about the distribution of messages type in queue.