Package org.jgroups
Class BatchMessage
- java.lang.Object
-
- org.jgroups.BaseMessage
-
- org.jgroups.BatchMessage
-
- All Implemented Interfaces:
java.lang.Iterable<Message>
,Constructable<Message>
,Message
,SizeStreamable
,Streamable
public class BatchMessage extends BaseMessage implements java.lang.Iterable<Message>
A message that contains a batch of messages. This message will wrap several messages, so lower protocol layers only have to process them once. This increases throughput for cases such as when average message size is small, or there is a heavy processing cost (e.g.SEQUENCER
).
Similar toCompositeMessage
but with some optimizations made for the specific use case.
This class is unsynchronized; the envisaged use case is that a BatchMessage is created with a number of messages, or messages are added, but then the instance is not modified anymore and sent.- Since:
- 5.x
- Author:
- Bela Ban, Chris Johnson
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected class
BatchMessage.BatchMessageIterator
-
Nested classes/interfaces inherited from interface org.jgroups.Message
Message.Flag, Message.TransientFlag
-
-
Field Summary
Fields Modifier and Type Field Description protected int
index
protected static MessageFactory
mf
protected Message[]
msgs
protected Address
orig_src
-
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 BatchMessage()
BatchMessage(Address dest, int capacity)
BatchMessage(Address dest, Address src, Message[] msgs, int index)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description BatchMessage
add(Message msg)
Adds the message at the end of the array.BatchMessage
add(Message... messages)
BatchMessage
copy(boolean copy_payload, boolean copy_headers)
Create a shallow copy of thisBatchMessage
.java.util.function.Supplier<Message>
create()
Creates an instance of the class implementing this interfaceprotected void
ensureCapacity(int size)
protected Message
ensureSameDest(Message msg)
<T extends Message>
Tget(int index)
byte[]
getArray()
Returns a reference to the payload (byte array).int
getLength()
Returns the length of the byte[] array payload.Message[]
getMessages()
int
getNumberOfMessages()
<T> T
getObject()
Gets an object from the payload.int
getOffset()
Returns the offset of the byte[] array at which user data starts.Address
getOrigSender()
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.java.util.Iterator<Message>
iterator()
void
readPayload(java.io.DataInput in)
BatchMessage
setArray(byte[] b, int o, int l)
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
.BatchMessage
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
.BatchMessage
setObject(java.lang.Object obj)
Sets an object in a message.BatchMessage
setOrigSender(Address s)
int
size()
Returns the exact size of the marshalled messagejava.lang.String
toString()
void
writePayload(java.io.DataOutput out)
-
Methods inherited from class org.jgroups.BaseMessage
clearFlag, clearFlag, clearHeaders, copyPayload, 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, writeTo, writeToNoAddrs
-
-
-
-
Field Detail
-
msgs
protected Message[] msgs
-
index
protected int index
-
orig_src
protected Address orig_src
-
mf
protected static final MessageFactory mf
-
-
Method Detail
-
create
public java.util.function.Supplier<Message> create()
Description copied from interface:Constructable
Creates an instance of the class implementing this interface- Specified by:
create
in interfaceConstructable<Message>
-
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.- Specified by:
hasPayload
in interfaceMessage
- 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
-
getNumberOfMessages
public int getNumberOfMessages()
-
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
.
-
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 BatchMessage setArray(byte[] b, int o, int l)
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 BatchMessage 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.
-
setObject
public BatchMessage setObject(java.lang.Object obj)
Description copied from interface:Message
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
-
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.
-
getMessages
public Message[] getMessages()
-
getOrigSender
public Address getOrigSender()
-
setOrigSender
public BatchMessage setOrigSender(Address s)
-
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
-
add
public BatchMessage add(Message msg)
Adds the message at the end of the array. Increases the array if needed
-
add
public BatchMessage add(Message... messages)
-
get
public <T extends Message> T get(int index)
-
copy
public BatchMessage copy(boolean copy_payload, boolean copy_headers)
Create a shallow copy of thisBatchMessage
.- Specified by:
copy
in interfaceMessage
- Overrides:
copy
in classBaseMessage
- Parameters:
copy_payload
- If true, the payload is copied, else it is null in the copied messagecopy_headers
- If true, the headers are copied
-
toString
public java.lang.String toString()
- Overrides:
toString
in classBaseMessage
-
size
public int size()
Description copied from interface:Message
Returns the exact size of the marshalled message- Specified by:
size
in interfaceMessage
- Overrides:
size
in classBaseMessage
- Returns:
- The number of bytes for the marshalled message
-
iterator
public java.util.Iterator<Message> iterator()
- Specified by:
iterator
in interfacejava.lang.Iterable<Message>
-
writePayload
public void writePayload(java.io.DataOutput out) throws java.io.IOException
- Specified by:
writePayload
in interfaceMessage
- Throws:
java.io.IOException
-
readPayload
public void readPayload(java.io.DataInput in) throws java.io.IOException, java.lang.ClassNotFoundException
- Specified by:
readPayload
in interfaceMessage
- Throws:
java.io.IOException
java.lang.ClassNotFoundException
-
ensureCapacity
protected void ensureCapacity(int size)
-
-