Package org.jgroups

Class CompositeMessage

java.lang.Object
org.jgroups.BaseMessage
org.jgroups.CompositeMessage
All Implemented Interfaces:
Iterable<Message>, Constructable<Message>, Message, SizeStreamable, Streamable

public class CompositeMessage extends BaseMessage implements Iterable<Message>
A message composed of multiple messages. This is useful when multiple messages are to be passed in a single message. Example: a byte buffer (1000 bytes) with a request type (req/rsp/ack).

In versions prior to 5.0, the byte arrays had to be copied into a single, larger (1001 bytes), byte array in order to be passed to the message.

This class is unsynchronized; the envisaged use case is that a CompositeMessage is created with a number of messages, or messages are added, but then the instance is not modified anymore and sent.

Since:
5.0
  • Field Details

    • msgs

      protected Message[] msgs
    • index

      protected int index
    • collapse

      protected boolean collapse
    • mf

      protected static final MessageFactory mf
  • Constructor Details

    • CompositeMessage

      public CompositeMessage()
    • CompositeMessage

      public CompositeMessage(Address dest)
    • CompositeMessage

      public CompositeMessage(Address dest, Message... messages)
  • Method Details

    • create

      public Supplier<Message> create()
      Description copied from interface: Constructable
      Creates an instance of the class implementing this interface
      Specified by:
      create in interface Constructable<Message>
    • getType

      public short getType()
      Description copied from interface: Message
      Returns the type of the message, e.g. BYTES_MSG, OBJ_MSG etc
      Specified by:
      getType in interface Message
    • hasPayload

      public boolean hasPayload()
      Description copied from interface: Message
      Returns true if the message has a payload, e.g. a byte[] array in a BytesMessage or an object in an ObjectMessage. This is more generic than Message.hasArray(), as it is not just applicable to a byte array.
      Specified by:
      hasPayload in interface Message
      Returns:
      True if the message has a payload
    • hasArray

      public boolean hasArray()
      Description copied from interface: Message
      Returns true if this message has a byte[] array as payload (even if it's null!), false otherwise
      Specified by:
      hasArray in interface Message
    • getNumberOfMessages

      public int getNumberOfMessages()
    • getOffset

      public int getOffset()
      Description copied from interface: Message
      Returns the offset of the byte[] array at which user data starts. Throws an exception if the message does not have a byte[] array payload (if Message.hasArray() is false).

      Note that this is a convenience method, as most messages are of type BytesMessage.

      Specified by:
      getOffset in interface Message
    • getArray

      public byte[] getArray()
      Description copied from interface: Message
      Returns a reference to the payload (byte array). Note that this array should not be modified as we do not copy the array on copy() or clone(): the array of the copied message is simply a reference to the old array.

      Even if offset and length are used: we return the entire array, not a subset.

      Throws an exception if the message does not have a byte[] array payload (Message.hasArray() is false).

      Note that this is a convenience method, as most messages are of type BytesMessage. It is recommended to downcast a Message to the correct subtype and use the methods available there to get/set the payload.

      Specified by:
      getArray in interface Message
    • setArray

      public CompositeMessage setArray(byte[] b, int o, int l)
      Description copied from interface: Message
      Sets the byte array in a message.

      Throws an exception if the message does not have a byte[] array payload (Message.hasArray() is false).

      Note that this is a convenience method, as most messages are of type BytesMessage. It is recommended to downcast a Message to the correct subtype and use the methods available there to get/set the payload.

      Specified by:
      setArray in interface Message
    • setArray

      public CompositeMessage setArray(ByteArray buf)
      Description copied from interface: Message
      Sets the byte array in a message.

      Throws an exception if the message does not have a byte[] array payload (Message.hasArray() is false).

      Note that this is a convenience method, as most messages are of type BytesMessage. It is recommended to downcast a Message to the correct subtype and use the methods available there to get/set the payload.

      Specified by:
      setArray in interface Message
    • setObject

      public CompositeMessage setObject(Object obj)
      Description copied from interface: Message
      Sets an object in a message. In a ObjectMessage, the object is set directly. In a BytesMessage, the object is serialized into a byte[] array and then the array is set as the payload of the message
      Specified by:
      setObject in interface Message
    • getObject

      public <T> T getObject()
      Description copied from interface: Message
      Gets an object from the payload. If the payload is a byte[] array (e.g. as in BytesMessage), an attempt to de-serialize the array into an object is made, and the object returned.

      If the payload is an object (e.g. as is the case in ObjectMessage), the object will be returned directly.

      Specified by:
      getObject in interface Message
    • collapse

      public boolean collapse()
    • collapse

      public CompositeMessage collapse(boolean b)
    • getLength

      public int getLength()
      Description copied from interface: Message
      Returns the length of the byte[] array payload. If the message does not have a byte[] array payload (Message.hasArray() is false), then the serialized size may be returned, or an implementation may choose to throw an exception
      Specified by:
      getLength in interface Message
    • add

      public CompositeMessage add(Message msg)
      Adds the message at the end of the array. Increases the array if needed
    • add

      public CompositeMessage add(Message... messages)
    • get

      public <T extends Message> T get(int index)
    • copy

      public CompositeMessage copy(boolean copy_payload, boolean copy_headers)
      Create a copy of this CompositeMessage.
      Specified by:
      copy in interface Message
      Overrides:
      copy in class BaseMessage
      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
    • toString

      public String toString()
      Overrides:
      toString in class BaseMessage
    • size

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

      public Iterator<Message> iterator()
      Specified by:
      iterator in interface Iterable<Message>
    • writePayload

      public void writePayload(DataOutput out) throws IOException
      Specified by:
      writePayload in interface Message
      Throws:
      IOException
    • readPayload

      public void readPayload(DataInput in) throws IOException, ClassNotFoundException
      Specified by:
      readPayload in interface Message
      Throws:
      IOException
      ClassNotFoundException
    • writePayloadAsBytes

      protected void writePayloadAsBytes(DataOutput out) throws IOException
      Throws:
      IOException
    • ensureCapacity

      protected void ensureCapacity(int size)
    • ensureSameDest

      protected Message ensureSameDest(Message msg)