Package org.jgroups
Class ObjectMessage
- java.lang.Object
-
- org.jgroups.BaseMessage
-
- org.jgroups.ObjectMessage
-
- All Implemented Interfaces:
Constructable<Message>
,Message
,SizeStreamable
,Streamable
public class ObjectMessage extends BaseMessage
AMessage
with an object as payload. The object won't get serialized until sent by the transport.
Note that the object passed to the constructor (or set withsetObject(Object)
) must not be changed after the creation of anObjectMessage
.- 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 SizeStreamable
obj
-
Fields inherited from class org.jgroups.BaseMessage
dest, flags, headers, sender, 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 ObjectMessage()
ObjectMessage(Address dest)
Constructs a message given a destination addressObjectMessage(Address dest, java.lang.Object obj)
Constructs a message given a destination address and the payload objectObjectMessage(Address dest, SizeStreamable obj)
Constructs a message given a destination address and the payload object
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected Message
copyPayload(Message copy)
Copies the payloadjava.util.function.Supplier<Message>
create()
Creates an instance of the class implementing this interfacebyte[]
getArray()
Returns a reference to the payload (byte array).int
getLength()
Returns the length of the byte[] array payload.<T> T
getObject()
Gets an object from the payload.int
getOffset()
Returns the offset of the byte[] array at which user data starts.short
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
isWrapped()
protected int
payloadSize()
void
readPayload(java.io.DataInput in)
ObjectMessage
setArray(byte[] b, int off, int len)
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 typeBytesMessage
.ObjectMessage
setArray(ByteArray buf)
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 typeBytesMessage
.ObjectMessage
setObject(java.lang.Object obj)
Sets the object.ObjectMessage
setObject(SizeStreamable obj)
Sets the payload to aSizeStreamable
(or null).ObjectMessage
setSizeStreamable(SizeStreamable s)
ObjectMessage
setWrapped(boolean b)
java.lang.String
toString()
void
writePayload(java.io.DataOutput out)
-
Methods inherited from class org.jgroups.BaseMessage
clearFlag, clearFlag, clearHeaders, copy, createHeaders, getDest, getFlags, getHeader, getHeaders, getNumHeaders, getPayload, getSrc, headers, headers, isFlagSet, isFlagSet, printHeaders, putHeader, putHeader, putHeaderIfAbsent, readFrom, serializedSize, setDest, setFlag, setFlag, setFlag, setFlagIfAbsent, setPayload, setSrc, size, sizeNoAddrs, writeTo, writeToNoAddrs
-
-
-
-
Field Detail
-
obj
protected SizeStreamable obj
-
-
Constructor Detail
-
ObjectMessage
public ObjectMessage()
-
ObjectMessage
public ObjectMessage(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.
-
ObjectMessage
public ObjectMessage(Address dest, java.lang.Object obj)
Constructs a message given a destination address and the payload object- 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.obj
- To be used as payload.
-
ObjectMessage
public ObjectMessage(Address dest, SizeStreamable obj)
Constructs a message given a destination address and the payload object- 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.obj
- TheSizeStreamable
object to be used as payload. Note that this constructor has fewer checks (e.g. instanceof) thanObjectMessage(org.jgroups.Address,java.lang.Object)
.
-
-
Method Detail
-
create
public java.util.function.Supplier<Message> create()
Description copied from interface:Constructable
Creates an instance of the class implementing this interface
-
getType
public short getType()
Description copied from interface:Message
Returns the type of the message, e.g. BYTES_MSG, OBJ_MSG etc
-
hasPayload
public boolean hasPayload()
Description copied from interface:Message
Returns true if the message has a payload, e.g. a byte[] array in aBytesMessage
or an object in anObjectMessage
. This is more generic thanMessage.hasArray()
, as it is not just applicable to a byte array.- 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
-
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 (ifMessage.hasArray()
is false).
Note that this is a convenience method, as most messages are of typeBytesMessage
.
-
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
-
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 typeBytesMessage
. It is recommended to downcast aMessage
to the correct subtype and use the methods available there to get/set the payload.
-
setArray
public ObjectMessage setArray(byte[] b, int off, int len)
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 typeBytesMessage
. It is recommended to downcast aMessage
to the correct subtype and use the methods available there to get/set the payload.
-
setArray
public ObjectMessage 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 typeBytesMessage
. It is recommended to downcast aMessage
to the correct subtype and use the methods available there to get/set the payload.
-
isWrapped
public boolean isWrapped()
-
setWrapped
public ObjectMessage setWrapped(boolean b)
-
setObject
public ObjectMessage setObject(java.lang.Object obj)
Sets the object. If the object doesn't implementSizeStreamable
, or is a primitive type, it will be wrapped into anObjectWrapperSerializable
(which does implement SizeStreamable)
-
setObject
public ObjectMessage setObject(SizeStreamable obj)
Sets the payload to aSizeStreamable
(or null). This method has fewer checks (e.g. instanceof) thansetObject(Object)
.- Parameters:
obj
- TheSizeStreamable
to be used as payload (or null)
-
setSizeStreamable
public ObjectMessage setSizeStreamable(SizeStreamable s)
-
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 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.
-
writePayload
public void writePayload(java.io.DataOutput out) throws java.io.IOException
- Throws:
java.io.IOException
-
readPayload
public void readPayload(java.io.DataInput in) throws java.io.IOException, java.lang.ClassNotFoundException
- Throws:
java.io.IOException
java.lang.ClassNotFoundException
-
copyPayload
protected Message copyPayload(Message copy)
Description copied from class:BaseMessage
Copies the payload- Overrides:
copyPayload
in classBaseMessage
-
toString
public java.lang.String toString()
- Overrides:
toString
in classBaseMessage
-
payloadSize
protected int payloadSize()
- Specified by:
payloadSize
in classBaseMessage
-
-