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 java.lang.Object implements Message
A common superclass for allMessage
implementations. It contains functionality to manage headers, flags and destination and source addresses.- Since:
- 5.0
- Author:
- Bela Ban
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.jgroups.Message
Message.Flag, Message.TransientFlag
-
-
Field Summary
Fields Modifier and Type Field Description protected Address
dest
protected short
flags
protected Header[]
headers
protected Address
sender
protected byte
transient_flags
-
Fields inherited from interface org.jgroups.Message
BYTES_MSG, COMPOSITE_MSG, EARLYBATCH_MSG, EMPTY_MSG, FRAG_MSG, LONG_MSG, NIO_MSG, OBJ_MSG
-
-
Constructor Summary
Constructors Constructor Description BaseMessage()
BaseMessage(Address dest)
Constructs a message given a destination address
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description Message
clearFlag(Message.Flag... flags)
Clears a number of flags in a messageMessage
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 the source- and destination addresses, flags and headers (if copy_headers is true).
If copy_payload is set, then methodcopyPayload(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 callMessage.putHeader(short,Header)
again.protected Message
copyPayload(Message copy)
Copies the payloadprotected static Header[]
createHeaders(int size)
Address
getDest()
Returns the destination address to send the message to.short
getFlags(boolean transient_flags)
Returns the internal representation of flags.<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
getNumHeaders()
Returns the number of headers<T> T
getPayload()
Returns the payload without any conversion (e.g.Address
getSrc()
Returns the address of the senderHeader[]
headers()
Message
headers(Header[] hdrs)
boolean
isFlagSet(Message.Flag flag)
Checks if a given flag is setboolean
isFlagSet(Message.TransientFlag flag)
Returns true if a transient flag is set, false otherwiseprotected abstract int
payloadSize()
java.lang.String
printHeaders()
Returns a pretty-printed string of the headersMessage
putHeader(short id, Header hdr)
Puts a header given an ID into the hashmap.protected Message
putHeader(short id, Header hdr, boolean replace_if_present)
Message
putHeaderIfAbsent(short id, Header hdr)
Adds a header to a message if not presentvoid
readFrom(java.io.DataInput in)
Read the state of the current object (including superclasses) from instream Note that the input stream must not be closedint
serializedSize()
Returns the size (in bytes) of the marshalled objectMessage
setDest(Address new_dest)
Sets the destination address to send the message to.Message
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 a number of flags in a messageMessage
setFlag(Message.TransientFlag... flags)
Same assetFlag(Flag...)
except that transient flags are not marshalledboolean
setFlagIfAbsent(Message.TransientFlag flag)
Atomically checks if a given flag is set and - if not - sets it.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 messageint
sizeNoAddrs(Address src)
Returns the exact size of the marshalled message without destination (and possibly source) addressjava.lang.String
toString()
void
writeTo(java.io.DataOutput out)
Write the entire state of the current object (including superclasses) to outstream.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 class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.jgroups.Constructable
create
-
-
-
-
Constructor Detail
-
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 Detail
-
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
-
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
-
getSrc
public Address getSrc()
Description copied from interface:Message
Returns the address of the sender
-
setSrc
public Message setSrc(Address new_src)
Description copied from interface:Message
Sets the address of the sender of this message
-
getNumHeaders
public int getNumHeaders()
Description copied from interface:Message
Returns the number of headers- Specified by:
getNumHeaders
in interfaceMessage
-
getHeaders
public java.util.Map<java.lang.Short,Header> getHeaders()
Description copied from interface:Message
Returns a hashmap of all header IDs and their associated headers- Specified by:
getHeaders
in interfaceMessage
-
headers
public Header[] headers()
-
printHeaders
public java.lang.String printHeaders()
Description copied from interface:Message
Returns a pretty-printed string of the headers- Specified by:
printHeaders
in interfaceMessage
-
setFlag
public Message setFlag(Message.Flag... flags)
Sets a number of flags in a message
-
setFlag
public Message setFlag(Message.TransientFlag... flags)
Same assetFlag(Flag...)
except that transient flags are not marshalled
-
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
-
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
-
clearFlag
public Message clearFlag(Message.Flag... flags)
Clears a number of flags in a 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
-
isFlagSet
public boolean isFlagSet(Message.Flag flag)
Checks if a given flag is set
-
isFlagSet
public boolean isFlagSet(Message.TransientFlag flag)
Description copied from interface:Message
Returns true if a transient flag is set, false otherwise
-
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 interfaceMessage
- 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 methodcopyPayload(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 callMessage.putHeader(short,Header)
again.
-
putHeader
public Message putHeader(short id, Header hdr)
Puts a header given an ID into the hashmap. Overwrites potential existing entry.
-
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 interfaceMessage
-
getHeader
public <T extends Header> T getHeader(short id)
Description copied from interface:Message
Gets a header from the message
-
clearHeaders
public Message clearHeaders()
Removes all headers: use carefully!- Specified by:
clearHeaders
in interfaceMessage
-
getPayload
public <T> T getPayload()
Description copied from interface:Message
Returns the payload without any conversion (e.g. as inMessage.getObject()
orMessage.getArray()
)- Specified by:
getPayload
in interfaceMessage
- Type Parameters:
T
- The type of the object- Returns:
- The payload
-
setPayload
public Message setPayload(java.lang.Object pl)
Description copied from interface:Message
Sets the payload- Specified by:
setPayload
in interfaceMessage
- Parameters:
pl
- The paylolad
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
payloadSize
protected abstract int payloadSize()
-
serializedSize
public int serializedSize()
Description copied from interface:SizeStreamable
Returns the size (in bytes) of the marshalled object- Specified by:
serializedSize
in interfaceSizeStreamable
-
size
public int size()
Description copied from interface:Message
Returns the exact size of the marshalled message
-
sizeNoAddrs
public int sizeNoAddrs(Address src)
Description copied from interface:Message
Returns the exact size of the marshalled message without destination (and possibly source) address- Specified by:
sizeNoAddrs
in interfaceMessage
-
writeTo
public void writeTo(java.io.DataOutput out) throws java.io.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 interfaceStreamable
- Throws:
java.io.IOException
-
writeToNoAddrs
public void writeToNoAddrs(Address src, java.io.DataOutput out) throws java.io.IOException
Description copied from interface:Message
Writes the message to an output stream excluding the destination (and possibly source) address- Specified by:
writeToNoAddrs
in interfaceMessage
- Throws:
java.io.IOException
-
readFrom
public void readFrom(java.io.DataInput in) throws java.io.IOException, java.lang.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 interfaceStreamable
- Throws:
java.io.IOException
java.lang.ClassNotFoundException
-
createHeaders
protected static Header[] createHeaders(int size)
-
-