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 allMessageimplementations. 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 Addressdestprotected shortflagsprotected Header[]headersprotected Addresssenderprotected bytetransient_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 MessageclearFlag(Message.Flag... flags)Clears a number of flags in a messageMessageclearFlag(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 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 MessagecopyPayload(Message copy)Copies the payloadprotected static Header[]createHeaders(int size)AddressgetDest()Returns the destination address to send the message to.shortgetFlags(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 headersintgetNumHeaders()Returns the number of headers<T> TgetPayload()Returns the payload without any conversion (e.g.AddressgetSrc()Returns the address of the senderHeader[]headers()Messageheaders(Header[] hdrs)booleanisFlagSet(Message.Flag flag)Checks if a given flag is setbooleanisFlagSet(Message.TransientFlag flag)Returns true if a transient flag is set, false otherwiseprotected abstract intpayloadSize()java.lang.StringprintHeaders()Returns a pretty-printed string of the headersMessageputHeader(short id, Header hdr)Puts a header given an ID into the hashmap.protected MessageputHeader(short id, Header hdr, boolean replace_if_present)MessageputHeaderIfAbsent(short id, Header hdr)Adds a header to a message if not presentvoidreadFrom(java.io.DataInput in)Read the state of the current object (including superclasses) from instream Note that the input stream must not be closedintserializedSize()Returns the size (in bytes) of the marshalled objectMessagesetDest(Address new_dest)Sets the destination address to send the message to.MessagesetFlag(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 a number of flags in a messageMessagesetFlag(Message.TransientFlag... flags)Same assetFlag(Flag...)except that transient flags are not marshalledbooleansetFlagIfAbsent(Message.TransientFlag flag)Atomically checks if a given flag is set and - if not - sets it.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) addressjava.lang.StringtoString()voidwriteTo(java.io.DataOutput out)Write the entire state of the current object (including superclasses) to outstream.voidwriteToNoAddrs(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:MessageReturns 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:MessageSets 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:MessageReturns the address of the sender
-
setSrc
public Message setSrc(Address new_src)
Description copied from interface:MessageSets the address of the sender of this message
-
getNumHeaders
public int getNumHeaders()
Description copied from interface:MessageReturns the number of headers- Specified by:
getNumHeadersin interfaceMessage
-
getHeaders
public java.util.Map<java.lang.Short,Header> getHeaders()
Description copied from interface:MessageReturns a hashmap of all header IDs and their associated headers- Specified by:
getHeadersin interfaceMessage
-
headers
public Header[] headers()
-
printHeaders
public java.lang.String printHeaders()
Description copied from interface:MessageReturns a pretty-printed string of the headers- Specified by:
printHeadersin 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:MessageSets 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:MessageRemoves 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:MessageReturns 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:
setFlagIfAbsentin 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:MessageAdds a header to a message if not present- Specified by:
putHeaderIfAbsentin interfaceMessage
-
getHeader
public <T extends Header> T getHeader(short id)
Description copied from interface:MessageGets a header from the message
-
clearHeaders
public Message clearHeaders()
Removes all headers: use carefully!- Specified by:
clearHeadersin interfaceMessage
-
getPayload
public <T> T getPayload()
Description copied from interface:MessageReturns the payload without any conversion (e.g. as inMessage.getObject()orMessage.getArray())- Specified by:
getPayloadin interfaceMessage- Type Parameters:
T- The type of the object- Returns:
- The payload
-
setPayload
public Message setPayload(java.lang.Object pl)
Description copied from interface:MessageSets the payload- Specified by:
setPayloadin interfaceMessage- Parameters:
pl- The paylolad
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
payloadSize
protected abstract int payloadSize()
-
serializedSize
public int serializedSize()
Description copied from interface:SizeStreamableReturns the size (in bytes) of the marshalled object- Specified by:
serializedSizein interfaceSizeStreamable
-
size
public int size()
Description copied from interface:MessageReturns the exact size of the marshalled message
-
sizeNoAddrs
public int sizeNoAddrs(Address src)
Description copied from interface:MessageReturns the exact size of the marshalled message without destination (and possibly source) address- Specified by:
sizeNoAddrsin interfaceMessage
-
writeTo
public void writeTo(java.io.DataOutput out) throws java.io.IOExceptionDescription copied from interface:StreamableWrite the entire state of the current object (including superclasses) to outstream. Note that the output stream must not be closed- Specified by:
writeToin interfaceStreamable- Throws:
java.io.IOException
-
writeToNoAddrs
public void writeToNoAddrs(Address src, java.io.DataOutput out) throws java.io.IOException
Description copied from interface:MessageWrites the message to an output stream excluding the destination (and possibly source) address- Specified by:
writeToNoAddrsin interfaceMessage- Throws:
java.io.IOException
-
readFrom
public void readFrom(java.io.DataInput in) throws java.io.IOException, java.lang.ClassNotFoundExceptionDescription copied from interface:StreamableRead the state of the current object (including superclasses) from instream Note that the input stream must not be closed- Specified by:
readFromin interfaceStreamable- Throws:
java.io.IOExceptionjava.lang.ClassNotFoundException
-
createHeaders
protected static Header[] createHeaders(int size)
-
-