Package org.jgroups

Class BaseMessage

java.lang.Object
org.jgroups.BaseMessage
All Implemented Interfaces:
Constructable<Message>, Message, SizeStreamable, Streamable
Direct Known Subclasses:
BatchMessage, BytesMessage, CompositeMessage, EmptyMessage, LongMessage, NioMessage, ObjectMessage

public abstract class BaseMessage extends Object implements Message
A common superclass for all Message implementations. It contains functionality to manage headers, flags and destination and source addresses.
Since:
5.0
  • Field Details

    • dest

      protected Address dest
    • sender

      protected Address sender
    • headers

      protected volatile Header[] headers
    • flags

      protected volatile short flags
    • transient_flags

      protected volatile byte transient_flags
    • DEST_SET

      static final byte DEST_SET
      See Also:
    • SRC_SET

      static final byte SRC_SET
      See Also:
  • Constructor Details

    • BaseMessage

      public BaseMessage()
    • BaseMessage

      public BaseMessage(Address dest)
      Constructs a message given a destination address
      Parameters:
      dest - The Address of the receiver. If it is null, then the message is sent to the group. Otherwise, it is sent to a single member.
  • Method Details

    • getDest

      public Address getDest()
      Description copied from interface: Message
      Returns the destination address to send the message to. A null value sends the message to all cluster members
      Specified by:
      getDest in interface Message
    • setDest

      public Message setDest(Address new_dest)
      Description copied from interface: Message
      Sets the destination address to send the message to. A null value sends the message to all cluster members
      Specified by:
      setDest in interface Message
    • getSrc

      public Address getSrc()
      Description copied from interface: Message
      Returns the address of the sender
      Specified by:
      getSrc in interface Message
    • setSrc

      public Message setSrc(Address new_src)
      Description copied from interface: Message
      Sets the address of the sender of this message
      Specified by:
      setSrc in interface Message
    • getNumHeaders

      public int getNumHeaders()
      Description copied from interface: Message
      Returns the number of headers
      Specified by:
      getNumHeaders in interface Message
    • getHeaders

      public Map<Short,Header> getHeaders()
      Description copied from interface: Message
      Returns a hashmap of all header IDs and their associated headers
      Specified by:
      getHeaders in interface Message
    • headers

      public Header[] headers()
    • headers

      public Message headers(Header[] hdrs)
    • printHeaders

      public String printHeaders()
      Description copied from interface: Message
      Returns a pretty-printed string of the headers
      Specified by:
      printHeaders in interface Message
    • setFlag

      public Message setFlag(Message.Flag... flags)
      Sets a number of flags in a message
      Specified by:
      setFlag in interface Message
      Parameters:
      flags - The flag or flags
      Returns:
      A reference to the message
    • setFlag

      public Message setFlag(Message.TransientFlag... flags)
      Same as setFlag(Flag...) except that transient flags are not marshalled
      Specified by:
      setFlag in interface Message
      Parameters:
      flags - The flag
    • setFlag

      public Message setFlag(short flag, boolean transient_flags, boolean xor)
      Description copied from interface: Message
      Sets the flags as a short; this way, multiple flags can be set in one operation
      Specified by:
      setFlag in interface Message
      Parameters:
      flag - The flag to be set (as a short). Overrides existing flags (no xor-ing)
      transient_flags - True if the flag is transient, false otherwise
      xor - When true, existing flags will be xor-ed with flag, otherwise not
    • getFlags

      public short getFlags(boolean transient_flags)
      Returns the internal representation of flags. Don't use this, as the internal format might change at any time ! This is only used by unit test code
      Specified by:
      getFlags in interface Message
      Parameters:
      transient_flags - Returns transient flags if true, else regular flags
      Returns:
    • clearFlag

      public Message clearFlag(Message.Flag... flags)
      Clears a number of flags in a message
      Specified by:
      clearFlag in interface Message
      Parameters:
      flags - The flags
      Returns:
      A reference to the message
    • clearFlag

      public Message clearFlag(Message.TransientFlag... flags)
      Description copied from interface: Message
      Removes a number of transient flags from the message. No-op for a flag if it is not set
      Specified by:
      clearFlag in interface Message
    • isFlagSet

      public boolean isFlagSet(Message.Flag flag)
      Checks if a given flag is set
      Specified by:
      isFlagSet in interface Message
      Parameters:
      flag - The flag
      Returns:
      Whether the flag is currently set
    • isFlagSet

      public boolean isFlagSet(Message.TransientFlag flag)
      Description copied from interface: Message
      Returns true if a transient flag is set, false otherwise
      Specified by:
      isFlagSet in interface Message
    • setFlagIfAbsent

      public boolean setFlagIfAbsent(Message.TransientFlag flag)
      Atomically checks if a given flag is set and - if not - sets it. When multiple threads concurrently call this method with the same flag, only one of them will be able to set the flag
      Specified by:
      setFlagIfAbsent in interface Message
      Parameters:
      flag -
      Returns:
      True if the flag could be set, false if not (was already set)
    • copy

      public Message copy(boolean copy_payload, boolean copy_headers)
      Copies the source- and destination addresses, flags and headers (if copy_headers is true).

      If copy_payload is set, then method copyPayload(Message) of the subclass will be called, which is responsible for copying the payload specific to that message type.

      Note that for headers, only the arrays holding references to the headers are copied, not the headers themselves ! The consequence is that the headers array of the copy hold the *same* references as the original, so do *not* modify the headers ! If you want to change a header, copy it and call Message.putHeader(short,Header) again.

      Specified by:
      copy in interface Message
      Parameters:
      copy_payload - If true, the payload is copied, else it is null in the copied message
      copy_headers - If true, the headers are copied
    • putHeader

      public Message putHeader(short id, Header hdr)
      Puts a header given an ID into the hashmap. Overwrites potential existing entry.
      Specified by:
      putHeader in interface Message
    • putHeaderIfAbsent

      public Message putHeaderIfAbsent(short id, Header hdr)
      Description copied from interface: Message
      Adds a header to a message if not present
      Specified by:
      putHeaderIfAbsent in interface Message
    • getHeader

      public <T extends Header> T getHeader(short id)
      Description copied from interface: Message
      Gets a header from the message
      Specified by:
      getHeader in interface Message
    • clearHeaders

      public Message clearHeaders()
      Removes all headers: use carefully!
      Specified by:
      clearHeaders in interface Message
    • getPayload

      public <T> T getPayload()
      Description copied from interface: Message
      Returns the payload without any conversion (e.g. as in Message.getObject() or Message.getArray())
      Specified by:
      getPayload in interface Message
      Type Parameters:
      T - The type of the object
      Returns:
      The payload
    • setPayload

      public Message setPayload(Object pl)
      Description copied from interface: Message
      Sets the payload
      Specified by:
      setPayload in interface Message
      Parameters:
      pl - The paylolad
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • serializedSize

      public int serializedSize()
      Description copied from interface: SizeStreamable
      Returns the size (in bytes) of the marshalled object
      Specified by:
      serializedSize in interface SizeStreamable
    • size

      public int size()
      Description copied from interface: Message
      Returns the exact size of the marshalled message
      Specified by:
      size in interface Message
      Returns:
      The number of bytes for the marshalled message
    • writeTo

      public void writeTo(DataOutput out) throws IOException
      Description copied from interface: Streamable
      Write the entire state of the current object (including superclasses) to outstream. Note that the output stream must not be closed
      Specified by:
      writeTo in interface Streamable
      Throws:
      IOException
    • writeToNoAddrs

      public void writeToNoAddrs(Address src, DataOutput out) throws IOException
      Description copied from interface: Message
      Writes the message to an output stream excluding the destination (and possibly source) address
      Specified by:
      writeToNoAddrs in interface Message
      Throws:
      IOException
    • readFrom

      public void readFrom(DataInput in) throws IOException, ClassNotFoundException
      Description copied from interface: Streamable
      Read the state of the current object (including superclasses) from instream Note that the input stream must not be closed
      Specified by:
      readFrom in interface Streamable
      Throws:
      IOException
      ClassNotFoundException
    • putHeader

      protected Message putHeader(short id, Header hdr, boolean replace_if_present)
    • copyPayload

      protected Message copyPayload(Message copy)
      Copies the payload
    • createHeaders

      protected static Header[] createHeaders(int size)