Package org.zeromq
Class ZMsg
java.lang.Object
org.zeromq.ZMsg
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
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionboolean
add
(byte[] data) boolean
boolean
boolean
addAll
(Collection<? extends ZFrame> arg0) void
addFirst
(byte[] data) void
void
void
addLast
(byte[] data) void
void
void
Add a String as a new ZFrame to the end of listvoid
clear()
boolean
boolean
containsAll
(Collection<?> arg0) long
Return total number of bytes contained in all ZFrames in this ZMsgvoid
destroy()
Destructor.void
dump
(Appendable out) Dump the message in human readable format.Creates copy of this ZMsg.element()
boolean
getFirst()
getLast()
int
hashCode()
boolean
isEmpty()
iterator()
static ZMsg
load
(DataInputStream file) Load / append a ZMsg from an open DataInputStreamstatic ZMsg
newStringMsg
(String... strings) Create a new ZMsg from one or more Stringsboolean
boolean
offerFirst
(ZFrame e) boolean
peek()
peekLast()
poll()
pollLast()
pop()
Pop a ZFrame and return the toString() representation of it.void
push
(byte[] data) void
void
static ZMsg
recvMsg
(ZMQ.Socket socket) Receives message from socket, returns ZMsg object or null if the recv was interrupted.static ZMsg
recvMsg
(ZMQ.Socket socket, int flag) Receives message from socket, returns ZMsg object or null if the recv was interrupted.remove()
boolean
boolean
removeAll
(Collection<?> arg0) boolean
boolean
boolean
retainAll
(Collection<?> arg0) static boolean
save
(ZMsg msg, DataOutputStream file) Save message to an open data output stream.void
send
(ZMQ.Socket socket) Send message to 0MQ socket.void
send
(ZMQ.Socket socket, boolean destroy) Send message to 0MQ socket, destroys contents after sending if destroy param is set to true.int
size()
Object[]
toArray()
<T> T[]
toArray
(T[] arg0) toString()
Convert the message to a string, for use in debugging.unwrap()
Pop frame off front of message, caller now owns frame.void
Push frame plus empty frame to front of message, before 1st frame.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Collection
parallelStream, removeIf, spliterator, stream, toArray
-
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()Return total number of bytes contained in all ZFrames in this ZMsg- Returns:
-
addString
Add a String as a new ZFrame to the end of list- Parameters:
str
- String to add to list
-
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
Push frame plus empty frame to front of message, before 1st frame. Message takes ownership of frame, will destroy it when message is sent.- Parameters:
frame
-
-
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.
-
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.
-
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:
-
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.- Parameters:
socket
-flag
- see ZMQ constants- Returns:
-
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
Create a new ZMsg from one or more Strings- Parameters:
strings
- Strings to add as frames.- Returns:
- ZMsg object
-
equals
- Specified by:
equals
in interfaceCollection<ZFrame>
- Overrides:
equals
in classObject
-
hashCode
public int hashCode()- Specified by:
hashCode
in interfaceCollection<ZFrame>
- Overrides:
hashCode
in classObject
-
dump
Dump the message in human readable format. This should only be used for debugging and tracing, inefficient in handling large messages. -
toString
Convert the message to a string, for use in debugging. -
addFirst
-
addFirst
public void addFirst(byte[] data) -
addLast
-
addLast
public void addLast(byte[] data) -
push
-
push
public void push(byte[] data) -
add
-
add
public boolean add(byte[] data) -
iterator
-
addAll
-
clear
public void clear()- Specified by:
clear
in interfaceCollection<ZFrame>
-
containsAll
- Specified by:
containsAll
in interfaceCollection<ZFrame>
-
isEmpty
public boolean isEmpty()- Specified by:
isEmpty
in interfaceCollection<ZFrame>
-
removeAll
- Specified by:
removeAll
in interfaceCollection<ZFrame>
-
retainAll
- Specified by:
retainAll
in interfaceCollection<ZFrame>
-
toArray
- Specified by:
toArray
in interfaceCollection<ZFrame>
-
toArray
public <T> T[] toArray(T[] arg0) - Specified by:
toArray
in interfaceCollection<ZFrame>
-
add
-
addFirst
-
addLast
-
contains
-
descendingIterator
- Specified by:
descendingIterator
in interfaceDeque<ZFrame>
-
element
-
getFirst
-
getLast
-
offer
-
offerFirst
- Specified by:
offerFirst
in 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:
removeFirst
in interfaceDeque<ZFrame>
-
removeFirstOccurrence
- Specified by:
removeFirstOccurrence
in interfaceDeque<ZFrame>
-
removeLast
- Specified by:
removeLast
in interfaceDeque<ZFrame>
-
removeLastOccurrence
- Specified by:
removeLastOccurrence
in interfaceDeque<ZFrame>
-
size
public int size()
-