Package org.jgroups
Class NioMessage
- java.lang.Object
-
- org.jgroups.BaseMessage
-
- org.jgroups.NioMessage
-
- All Implemented Interfaces:
Constructable<Message>
,Message
,SizeStreamable
,Streamable
public class NioMessage extends BaseMessage
AMessage
with a (heap-based or direct)ByteBuffer
as payload.
Note that the payload of an NioMessage must not be modified after sending it (ie.JChannel.send(Message)
; serialization depends on position and limit to be correct.- 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 java.nio.ByteBuffer
buf
The payloadprotected boolean
use_direct_memory_for_allocations
If true, use direct memory when creatingByteBuffer
payloads, e.g.-
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 NioMessage()
NioMessage(Address dest)
Constructs a message given a destination addressNioMessage(Address dest, java.nio.ByteBuffer buf)
Constructs a message given a destination and source address and the payload byte buffer
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description NioMessage
copy(boolean copy_payload, boolean copy_headers)
Create a copy of the message.
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 callBaseMessage.putHeader(short,Header)
again.protected Message
copyPayload(Message copy)
Copies the payloadjava.util.function.Supplier<Message>
create()
Creates an instance of the class implementing this interfaceprotected java.nio.ByteBuffer
createBuffer(byte[] array, int offset, int length)
byte[]
getArray()
Returns a reference to the payload (byte array).java.nio.ByteBuffer
getBuf()
Returns the byte buffer.int
getLength()
Returns the length of the byte[] array payload.<T> T
getObject()
Gets an object from the payload.<T> T
getObject(java.lang.ClassLoader loader)
Tries to unmarshal the byte buffer payload into an objectint
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
isDirect()
protected int
payloadSize()
void
readPayload(java.io.DataInput in)
NioMessage
setArray(byte[] b, int offset, int length)
Sets the internal buffer to point to a subset of a given buffer.NioMessage
setArray(ByteArray b)
Sets the bufferNioMessage
setBuf(java.nio.ByteBuffer b)
NioMessage
setObject(java.lang.Object obj)
Takes an object and uses Java serialization to generate the byte[] buffer which is set in the message.java.lang.String
toString()
boolean
useDirectMemory()
NioMessage
useDirectMemory(boolean b)
void
writePayload(java.io.DataOutput out)
-
Methods inherited from class org.jgroups.BaseMessage
clearFlag, clearFlag, clearHeaders, 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
-
buf
protected java.nio.ByteBuffer buf
The payload
-
use_direct_memory_for_allocations
protected boolean use_direct_memory_for_allocations
If true, use direct memory when creatingByteBuffer
payloads, e.g. onsetArray(byte[], int, int)
,setArray(ByteArray)
,setObject(Object)
or when getting read from the network (readPayload(DataInput)
).
Unless this flag is set to true, a directByteBuffer
payload will become a heap-based payload by a receiver when sent over the network. This may be useful if we want to use off-heap (direct) memory only for sending, but not receiving of messages.
If we want the receiver to create a direct-memory based message, set this flag to true before sending the message.
-
-
Constructor Detail
-
NioMessage
public NioMessage()
-
NioMessage
public NioMessage(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 all cluster members. Otherwise, it is sent to a single member.
-
NioMessage
public NioMessage(Address dest, java.nio.ByteBuffer buf)
Constructs a message given a destination and source address and the payload byte buffer- Parameters:
dest
- The Address of the receiver. If it is null, then the message is sent to all cluster members. Otherwise, it is sent to a single member.buf
- The payload. Note that this buffer must not be modified (e.g. buf[0]='x' is not allowed) since we don't copy the contents.
-
-
Method Detail
-
getBuf
public java.nio.ByteBuffer getBuf()
Returns the byte buffer. Do not read from/write to it, or else retransmissions will fail! UseByteBuffer.duplicate()
before, to create a copy, if the buffer needs to be read from
-
setBuf
public NioMessage setBuf(java.nio.ByteBuffer b)
-
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
-
useDirectMemory
public boolean useDirectMemory()
-
useDirectMemory
public NioMessage useDirectMemory(boolean b)
-
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
-
isDirect
public boolean isDirect()
-
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 NioMessage setArray(byte[] b, int offset, int length)
Sets the internal buffer to point to a subset of a given buffer. Note that the byte[] buffer passed as argument must not be modified. Reason: if we retransmit the message, it would still have a ref to the original byte[] buffer passed in as argument, and so we would retransmit a changed byte[] buffer !- Parameters:
b
- The reference to a given buffer. If null, we'll reset the buffer to nulloffset
- The initial positionlength
- The number of bytes
-
setArray
public NioMessage setArray(ByteArray b)
Sets the buffer Note that the byte[] buffer passed as argument must not be modified. Reason: if we retransmit the message, it would still have a ref to the original byte[] buffer passed in as argument, and so we would retransmit a changed byte[] buffer !
-
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.
-
getObject
public <T> T getObject(java.lang.ClassLoader loader)
Tries to unmarshal the byte buffer payload into an object- Returns:
- The object
-
setObject
public NioMessage setObject(java.lang.Object obj)
Takes an object and uses Java serialization to generate the byte[] buffer which is set in the message. Parameter 'obj' has to be serializable (e.g. implementing Serializable, Externalizable or Streamable, or be a basic type (e.g. Integer, Short etc)).
-
copy
public NioMessage copy(boolean copy_payload, boolean copy_headers)
Create a copy of the message.
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 callBaseMessage.putHeader(short,Header)
again.- Specified by:
copy
in interfaceMessage
- Overrides:
copy
in classBaseMessage
- Parameters:
copy_payload
- Copy the buffercopy_headers
- Copy the headers- Returns:
- Message with specified data
-
toString
public java.lang.String toString()
- Overrides:
toString
in classBaseMessage
-
copyPayload
protected Message copyPayload(Message copy)
Description copied from class:BaseMessage
Copies the payload- Overrides:
copyPayload
in classBaseMessage
-
payloadSize
protected int payloadSize()
- Specified by:
payloadSize
in classBaseMessage
-
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
- Throws:
java.io.IOException
-
createBuffer
protected java.nio.ByteBuffer createBuffer(byte[] array, int offset, int length)
-
-