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 class
Message.Flag
static class
Message.TransientFlag
-
Field Summary
Fields Modifier and Type Field Description static short
BYTES_MSG
static short
COMPOSITE_MSG
static short
EARLYBATCH_MSG
static short
EMPTY_MSG
static short
FRAG_MSG
static short
LONG_MSG
static short
NIO_MSG
static short
OBJ_MSG
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description Message
clearFlag(Message.Flag... flags)
Removes a number of flags from the message.Message
clearFlag(Message.TransientFlag... flags)
Removes a number of transient flags from the message.Message
clearHeaders()
Removes all headers: use carefully!Message
copy(boolean copy_payload, boolean copy_headers)
Copies a messagedefault Address
dest()
default Message
dest(Address new_dest)
byte[]
getArray()
Returns a reference to the payload (byte array).Address
getDest()
Returns the destination address to send the message to.default short
getFlags()
short
getFlags(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 headersint
getLength()
Returns the length of the byte[] array payload.int
getNumHeaders()
Returns the number of headers<T> T
getObject()
Gets an object from the payload.int
getOffset()
Returns the offset of the byte[] array at which user data starts.<T> T
getPayload()
Returns the payload without any conversion (e.g.Address
getSrc()
Returns the address of the sendershort
getType()
Returns the type of the message, e.g.boolean
hasArray()
Returns true if this message has a byte[] array as payload (even if it's null!), false otherwiseboolean
hasPayload()
Returns true if the message has a payload, e.g.boolean
isFlagSet(Message.Flag flag)
Returns true if a flag is set, false otherwiseboolean
isFlagSet(Message.TransientFlag flag)
Returns true if a transient flag is set, false otherwisejava.lang.String
printHeaders()
Returns a pretty-printed string of the headersMessage
putHeader(short id, Header hdr)
Adds a header to the messageMessage
putHeaderIfAbsent(short id, Header hdr)
Adds a header to a message if not presentvoid
readPayload(java.io.DataInput in)
default Message
setArray(byte[] b)
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
.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
.Message
setDest(Address new_dest)
Sets the destination address to send the message to.default Message
setFlag(short flag, boolean transient_flags)
Sets the flags as a short; this way, multiple flags can be set in one operationMessage
setFlag(short flag, boolean transient_flags, boolean xor)
Sets the flags as a short; this way, multiple flags can be set in one operationMessage
setFlag(Message.Flag... flags)
Sets one or more flags (xor-ing existing flags)Message
setFlag(Message.TransientFlag... flags)
Sets one or more transient flags (xor-ing).boolean
setFlagIfAbsent(Message.TransientFlag flag)
Atomically sets a transient flag if not set.Message
setObject(java.lang.Object obj)
Sets an object in a message.Message
setPayload(java.lang.Object pl)
Sets the payloadMessage
setSrc(Address new_src)
Sets the address of the sender of this messageint
size()
Returns the exact size of the marshalled messagedefault Address
src()
default Message
src(Address new_src)
void
writePayload(java.io.DataOutput out)
void
writeToNoAddrs(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 aBytesMessage
or 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 aMessage
to 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
-
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 aMessage
to 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 aMessage
to 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
-
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.IOException
java.lang.ClassNotFoundException
-
-