Package org.jgroups
Interface Message
-
- All Superinterfaces:
Constructable<Message>,SizeStreamable,Streamable
- All Known Implementing Classes:
BaseMessage,BatchMessage,BytesMessage,CompositeMessage,EmptyMessage,FragmentedMessage,LongMessage,NioMessage,ObjectMessage
public interface Message extends SizeStreamable, Constructable<Message>
A Message is used to send data to members of a group. It contains the address of the sender, the destination address, (typically) a payload, flags and a list of headers.
Subclasses define different types of payloads, e.g. byte arrays, ByteBuffers, Objects etc.- Since:
- 5.0
- Author:
- Bela Ban
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static classMessage.Flagstatic classMessage.TransientFlag
-
Field Summary
Fields Modifier and Type Field Description static shortBYTES_MSGstatic shortCOMPOSITE_MSGstatic shortEARLYBATCH_MSGstatic shortEMPTY_MSGstatic shortFRAG_MSGstatic shortLONG_MSGstatic shortNIO_MSGstatic shortOBJ_MSG
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description MessageclearFlag(Message.Flag... flags)Removes a number of flags from the message.MessageclearFlag(Message.TransientFlag... flags)Removes a number of transient flags from the message.MessageclearHeaders()Removes all headers: use carefully!Messagecopy(boolean copy_payload, boolean copy_headers)Copies a messagedefault Addressdest()default Messagedest(Address new_dest)byte[]getArray()Returns a reference to the payload (byte array).AddressgetDest()Returns the destination address to send the message to.default shortgetFlags()shortgetFlags(boolean transient_flags)Returns the flags as an or-ed short<T extends Header>
TgetHeader(short id)Gets a header from the messagejava.util.Map<java.lang.Short,Header>getHeaders()Returns a hashmap of all header IDs and their associated headersintgetLength()Returns the length of the byte[] array payload.intgetNumHeaders()Returns the number of headers<T> TgetObject()Gets an object from the payload.intgetOffset()Returns the offset of the byte[] array at which user data starts.<T> TgetPayload()Returns the payload without any conversion (e.g.AddressgetSrc()Returns the address of the sendershortgetType()Returns the type of the message, e.g.booleanhasArray()Returns true if this message has a byte[] array as payload (even if it's null!), false otherwisebooleanhasPayload()Returns true if the message has a payload, e.g.booleanisFlagSet(Message.Flag flag)Returns true if a flag is set, false otherwisebooleanisFlagSet(Message.TransientFlag flag)Returns true if a transient flag is set, false otherwisedefault intlength()java.lang.StringprintHeaders()Returns a pretty-printed string of the headersMessageputHeader(short id, Header hdr)Adds a header to the messageMessageputHeaderIfAbsent(short id, Header hdr)Adds a header to a message if not presentvoidreadPayload(java.io.DataInput in)default MessagesetArray(byte[] b)MessagesetArray(byte[] b, int offset, int length)Sets the byte array in a message.
Throws an exception if the message does not have a byte[] array payload (hasArray()is false).
Note that this is a convenience method, as most messages are of typeBytesMessage.MessagesetArray(ByteArray buf)Sets the byte array in a message.
Throws an exception if the message does not have a byte[] array payload (hasArray()is false).
Note that this is a convenience method, as most messages are of typeBytesMessage.MessagesetDest(Address new_dest)Sets the destination address to send the message to.default MessagesetFlag(short flag, boolean transient_flags)Sets the flags as a short; this way, multiple flags can be set in one operationMessagesetFlag(short flag, boolean transient_flags, boolean xor)Sets the flags as a short; this way, multiple flags can be set in one operationMessagesetFlag(Message.Flag... flags)Sets one or more flags (xor-ing existing flags)MessagesetFlag(Message.TransientFlag... flags)Sets one or more transient flags (xor-ing).booleansetFlagIfAbsent(Message.TransientFlag flag)Atomically sets a transient flag if not set.MessagesetObject(java.lang.Object obj)Sets an object in a message.MessagesetPayload(java.lang.Object pl)Sets the payloadMessagesetSrc(Address new_src)Sets the address of the sender of this messageintsize()Returns the exact size of the marshalled messageintsizeNoAddrs(Address src)Returns the exact size of the marshalled message without destination (and possibly source) addressdefault Addresssrc()default Messagesrc(Address new_src)voidwritePayload(java.io.DataOutput out)voidwriteToNoAddrs(Address src, java.io.DataOutput out)Writes the message to an output stream excluding the destination (and possibly source) address-
Methods inherited from interface org.jgroups.Constructable
create
-
Methods inherited from interface org.jgroups.util.SizeStreamable
serializedSize
-
Methods inherited from interface org.jgroups.util.Streamable
readFrom, writeTo
-
-
-
-
Field Detail
-
BYTES_MSG
static final short BYTES_MSG
- See Also:
- Constant Field Values
-
NIO_MSG
static final short NIO_MSG
- See Also:
- Constant Field Values
-
EMPTY_MSG
static final short EMPTY_MSG
- See Also:
- Constant Field Values
-
OBJ_MSG
static final short OBJ_MSG
- See Also:
- Constant Field Values
-
LONG_MSG
static final short LONG_MSG
- See Also:
- Constant Field Values
-
COMPOSITE_MSG
static final short COMPOSITE_MSG
- See Also:
- Constant Field Values
-
FRAG_MSG
static final short FRAG_MSG
- See Also:
- Constant Field Values
-
EARLYBATCH_MSG
static final short EARLYBATCH_MSG
- See Also:
- Constant Field Values
-
-
Method Detail
-
getType
short getType()
Returns the type of the message, e.g. BYTES_MSG, OBJ_MSG etc
-
getDest
Address getDest()
Returns the destination address to send the message to. A null value sends the message to all cluster members
-
dest
default Address dest()
-
setDest
Message setDest(Address new_dest)
Sets the destination address to send the message to. A null value sends the message to all cluster members
-
getSrc
Address getSrc()
Returns the address of the sender
-
src
default Address src()
-
putHeaderIfAbsent
Message putHeaderIfAbsent(short id, Header hdr)
Adds a header to a message if not present
-
getHeader
<T extends Header> T getHeader(short id)
Gets a header from the message
-
getHeaders
java.util.Map<java.lang.Short,Header> getHeaders()
Returns a hashmap of all header IDs and their associated headers
-
getNumHeaders
int getNumHeaders()
Returns the number of headers
-
printHeaders
java.lang.String printHeaders()
Returns a pretty-printed string of the headers
-
clearHeaders
Message clearHeaders()
Removes all headers: use carefully!
-
setFlag
Message setFlag(Message.Flag... flags)
Sets one or more flags (xor-ing existing flags)
-
setFlag
default Message setFlag(short flag, boolean transient_flags)
Sets the flags as a short; this way, multiple flags can be set in one operation- 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
-
setFlag
Message setFlag(short flag, boolean transient_flags, boolean xor)
Sets the flags as a short; this way, multiple flags can be set in one operation- 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 otherwisexor- When true, existing flags will be xor-ed with flag, otherwise not
-
setFlag
Message setFlag(Message.TransientFlag... flags)
Sets one or more transient flags (xor-ing). Transient flags are not marshalled
-
setFlagIfAbsent
boolean setFlagIfAbsent(Message.TransientFlag flag)
Atomically sets a transient flag if not set. Returns true if the flags was set, else false (already set)
-
getFlags
short getFlags(boolean transient_flags)
Returns the flags as an or-ed short- Parameters:
transient_flags- Returns transient flags if true, else regular flags
-
getFlags
default short getFlags()
-
clearFlag
Message clearFlag(Message.Flag... flags)
Removes a number of flags from the message. No-op for a flag if it is not set
-
clearFlag
Message clearFlag(Message.TransientFlag... flags)
Removes a number of transient flags from the message. No-op for a flag if it is not set
-
isFlagSet
boolean isFlagSet(Message.Flag flag)
Returns true if a flag is set, false otherwise
-
isFlagSet
boolean isFlagSet(Message.TransientFlag flag)
Returns true if a transient flag is set, false otherwise
-
copy
Message copy(boolean copy_payload, boolean copy_headers)
Copies a message- Parameters:
copy_payload- If true, the payload is copied, else it is null in the copied messagecopy_headers- If true, the headers are copied
-
hasPayload
boolean hasPayload()
Returns true if the message has a payload, e.g. a byte[] array in aBytesMessageor an object in anObjectMessage. This is more generic thanhasArray(), as it is not just applicable to a byte array.- Returns:
- True if the message has a payload
-
hasArray
boolean hasArray()
Returns true if this message has a byte[] array as payload (even if it's null!), false otherwise
-
getArray
byte[] getArray()
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 (hasArray()is false).
Note that this is a convenience method, as most messages are of typeBytesMessage. It is recommended to downcast aMessageto the correct subtype and use the methods available there to get/set the payload.
-
getOffset
int getOffset()
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 (ifhasArray()is false).
Note that this is a convenience method, as most messages are of typeBytesMessage.
-
getLength
int getLength()
Returns the length of the byte[] array payload. If the message does not have a byte[] array payload (hasArray()is false), then the serialized size may be returned, or an implementation may choose to throw an exception
-
length
default int length()
-
setArray
Message setArray(byte[] b, int offset, int length)
Sets the byte array in a message.
Throws an exception if the message does not have a byte[] array payload (hasArray()is false).
Note that this is a convenience method, as most messages are of typeBytesMessage. It is recommended to downcast aMessageto the correct subtype and use the methods available there to get/set the payload.
-
setArray
default Message setArray(byte[] b)
-
setArray
Message setArray(ByteArray buf)
Sets the byte array in a message.
Throws an exception if the message does not have a byte[] array payload (hasArray()is false).
Note that this is a convenience method, as most messages are of typeBytesMessage. It is recommended to downcast aMessageto the correct subtype and use the methods available there to get/set the payload.
-
getObject
<T> T getObject()
Gets an object from the payload. If the payload is a byte[] array (e.g. as inBytesMessage), 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 inObjectMessage), the object will be returned directly.
-
setObject
Message setObject(java.lang.Object obj)
Sets an object in a message. In aObjectMessage, the object is set directly. In aBytesMessage, the object is serialized into a byte[] array and then the array is set as the payload of the message
-
getPayload
<T> T getPayload()
Returns the payload without any conversion (e.g. as ingetObject()orgetArray())- Type Parameters:
T- The type of the object- Returns:
- The payload
-
setPayload
Message setPayload(java.lang.Object pl)
Sets the payload- Parameters:
pl- The paylolad
-
size
int size()
Returns the exact size of the marshalled message- Returns:
- The number of bytes for the marshalled message
-
sizeNoAddrs
int sizeNoAddrs(Address src)
Returns the exact size of the marshalled message without destination (and possibly source) address
-
writeToNoAddrs
void writeToNoAddrs(Address src, java.io.DataOutput out) throws java.io.IOException
Writes the message to an output stream excluding the destination (and possibly source) address- Throws:
java.io.IOException
-
writePayload
void writePayload(java.io.DataOutput out) throws java.io.IOException- Throws:
java.io.IOException
-
readPayload
void readPayload(java.io.DataInput in) throws java.io.IOException, java.lang.ClassNotFoundException- Throws:
java.io.IOExceptionjava.lang.ClassNotFoundException
-
-