Class ZMsg
java.lang.Object
org.zeromq.ZMsg
- All Implemented Interfaces:
Iterable<ZFrame>, Collection<ZFrame>, Deque<ZFrame>, Queue<ZFrame>, SequencedCollection<ZFrame>
The ZMsg class provides methods to send and receive multipart messages
across 0MQ sockets. This class provides a list-like container interface,
with methods to work with the overall container. ZMsg messages are
composed of zero or more ZFrame objects.
// Send a simple single-frame string message on a ZMQSocket "output" socket object
ZMsg.newStringMsg("Hello").send(output);
// Add several frames into one message
ZMsg msg = new ZMsg();
for (int i = 0 ; i < 10 ; i++) {
msg.addString("Frame" + i);
}
msg.send(output);
// Receive message from ZMQSocket "input" socket object and iterate over frames
ZMsg receivedMessage = ZMsg.recvMsg(input);
for (ZFrame f : receivedMessage) {
// Do something with frame f (of type ZFrame)
}
Based on zmsg.c in czmq-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final ArrayDeque<ZFrame> Hold internal list of ZFrame objects -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanadd(byte[] data) booleanbooleanbooleanaddAll(Collection<? extends ZFrame> arg0) addFirst(byte[] data) voidaddLast(byte[] data) voidAdd a String as a new ZFrame to the end of listappend(byte[] data) Adds bytes as a new frame in the message.Adds a string as a new frame in the message.voidclear()booleanbooleancontainsAll(Collection<?> arg0) longvoiddestroy()Destructor.dump()dump(Appendable out) Dump the message in human readable format.Creates copy of this ZMsg.element()booleangetFirst()getLast()inthashCode()booleanisEmpty()iterator()static ZMsgload(DataInputStream file) Load / append a ZMsg from an open DataInputStreamstatic ZMsgnewStringMsg(String... strings) Create a new ZMsg from one or more StringsbooleanbooleanofferFirst(ZFrame e) booleanpeek()peekLast()poll()pollLast()pop()Pop a ZFrame and return the toString() representation of it.push(byte[] data) voidstatic ZMsgrecvMsg(ZMQ.Socket socket) Receives message from socket, returns ZMsg object or null if the recv was interrupted.static ZMsgrecvMsg(ZMQ.Socket socket, boolean wait) Receives message from socket, returns ZMsg object or null if the recv was interrupted.static ZMsgrecvMsg(ZMQ.Socket socket, int flag) Receives message from socket, returns ZMsg object or null if the recv was interrupted.static voidrecvMsg(ZMQ.Socket socket, int flags, Consumer<ZMsg> handler) This API is in DRAFT state and is subject to change at ANY time until declared stable handle incoming message with a handlerstatic voidrecvMsg(ZMQ.Socket socket, int flags, Consumer<ZMsg> handler, Consumer<ZMQException> exceptionHandler) This API is in DRAFT state and is subject to change at ANY time until declared stable handle incoming message with a handlerremove()booleanbooleanremoveAll(Collection<?> arg0) booleanbooleanbooleanretainAll(Collection<?> arg0) static booleansave(ZMsg msg, DataOutputStream file) Save message to an open data output stream.booleansend(ZMQ.Socket socket) Send message to 0MQ socket.booleansend(ZMQ.Socket socket, boolean destroy) Send message to 0MQ socket, destroys contents after sending if destroy param is set to true.intsize()Object[]toArray()<T> T[]toArray(T[] arg0) toString()Returns pretty string representation of multipart message: [ frame0, frame1, ..., frameN ]unwrap()Pop frame off front of message, caller now owns frame.Push frame plus empty frame to front of message, before 1st frame.Methods inherited from interface Collection
parallelStream, removeIf, spliterator, stream, toArray
-
Field Details
-
frames
Hold internal list of ZFrame objects
-
-
Constructor Details
-
ZMsg
public ZMsg()Class Constructor
-
-
Method Details
-
destroy
public void destroy()Destructor. Explicitly destroys all ZFrames contains in the ZMsg -
contentSize
public long contentSize()- Returns:
- total number of bytes contained in all ZFrames in this ZMsg
-
addString
-
duplicate
Creates copy of this ZMsg. Also duplicates all frame content.- Returns:
- The duplicated ZMsg object, else null if this ZMsg contains an empty frame set
-
wrap
-
unwrap
Pop frame off front of message, caller now owns frame. If next frame is empty, pops and destroys that empty frame (e.g. useful when unwrapping ROUTER socket envelopes)- Returns:
- Unwrapped frame
-
send
Send message to 0MQ socket.- Parameters:
socket- 0MQ socket to send ZMsg on.- Returns:
- true if send is success, false otherwise
-
send
Send message to 0MQ socket, destroys contents after sending if destroy param is set to true. If the message has no frames, sends nothing but still destroy()s the ZMsg object- Parameters:
socket- 0MQ socket to send ZMsg on.- Returns:
- true if send is success, false otherwise
-
recvMsg
Receives message from socket, returns ZMsg object or null if the recv was interrupted. Does a blocking recv, if you want not to block then use the ZLoop class or ZMQ.Poller to check for socket input before receiving or recvMsg with flag ZMQ.DONTWAIT.- Parameters:
socket-- Returns:
- ZMsg object, null if interrupted
-
recvMsg
Receives message from socket, returns ZMsg object or null if the recv was interrupted.- Parameters:
socket-wait- true to wait for next message, false to do a non-blocking recv.- Returns:
- ZMsg object, null if interrupted
-
recvMsg
Receives message from socket, returns ZMsg object or null if the recv was interrupted. Setting the flag to ZMQ.DONTWAIT does a non-blocking recv.- Parameters:
socket-flag- see ZMQ constants- Returns:
- ZMsg object, null if interrupted
-
recvMsg
public static void recvMsg(ZMQ.Socket socket, int flags, Consumer<ZMsg> handler, Consumer<ZMQException> exceptionHandler) This API is in DRAFT state and is subject to change at ANY time until declared stable handle incoming message with a handler- Parameters:
socket-flags- see ZMQ constantshandler- handler to handle incoming messageexceptionHandler- handler to handle exceptions
-
recvMsg
This API is in DRAFT state and is subject to change at ANY time until declared stable handle incoming message with a handler- Parameters:
socket-flags- see ZMQ constantshandler- handler to handle incoming message
-
save
Save message to an open data output stream. Data saved as: 4 bytes: number of frames For every frame: 4 bytes: byte size of frame data + n bytes: frame byte data- Parameters:
msg- ZMsg to savefile- DataOutputStream- Returns:
- True if saved OK, else false
-
load
Load / append a ZMsg from an open DataInputStream- Parameters:
file- DataInputStream connected to file- Returns:
- ZMsg object
-
newStringMsg
-
equals
-
hashCode
public int hashCode()- Specified by:
hashCodein interfaceCollection<ZFrame>- Overrides:
hashCodein classObject
-
dump
Dump the message in human readable format. This should only be used for debugging and tracing, inefficient in handling large messages. -
dump
-
addFirst
-
addFirst
-
addLast
-
addLast
-
push
-
push
-
add
-
add
public boolean add(byte[] data) -
append
-
append
Adds bytes as a new frame in the message.- Parameters:
data- the value to add- Returns:
- this
-
iterator
-
addAll
-
clear
public void clear()- Specified by:
clearin interfaceCollection<ZFrame>
-
containsAll
- Specified by:
containsAllin interfaceCollection<ZFrame>
-
isEmpty
public boolean isEmpty()- Specified by:
isEmptyin interfaceCollection<ZFrame>
-
removeAll
- Specified by:
removeAllin interfaceCollection<ZFrame>
-
retainAll
- Specified by:
retainAllin interfaceCollection<ZFrame>
-
toArray
- Specified by:
toArrayin interfaceCollection<ZFrame>
-
toArray
public <T> T[] toArray(T[] arg0) - Specified by:
toArrayin interfaceCollection<ZFrame>
-
add
-
addFirst
-
addLast
-
contains
-
descendingIterator
- Specified by:
descendingIteratorin interfaceDeque<ZFrame>
-
element
-
getFirst
-
getLast
-
offer
-
offerFirst
- Specified by:
offerFirstin interfaceDeque<ZFrame>
-
offerLast
-
peek
-
peekFirst
-
peekLast
-
poll
-
pollFirst
-
pollLast
-
pop
-
popString
Pop a ZFrame and return the toString() representation of it.- Returns:
- toString version of pop'ed frame, or null if no frame exists.
-
push
-
remove
-
remove
-
removeFirst
- Specified by:
removeFirstin interfaceDeque<ZFrame>- Specified by:
removeFirstin interfaceSequencedCollection<ZFrame>
-
removeFirstOccurrence
- Specified by:
removeFirstOccurrencein interfaceDeque<ZFrame>
-
removeLast
- Specified by:
removeLastin interfaceDeque<ZFrame>- Specified by:
removeLastin interfaceSequencedCollection<ZFrame>
-
removeLastOccurrence
- Specified by:
removeLastOccurrencein interfaceDeque<ZFrame>
-
size
-
append
-
toString
-