org.zeromq
Class ZMsg

java.lang.Object
  extended by org.zeromq.ZMsg
All Implemented Interfaces:
Iterable<ZFrame>, Collection<ZFrame>, Deque<ZFrame>, Queue<ZFrame>

public class ZMsg
extends Object
implements Iterable<ZFrame>, Deque<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


Constructor Summary
ZMsg()
          Class Constructor
 
Method Summary
 boolean add(byte[] data)
           
 boolean add(String stringValue)
           
 boolean add(ZFrame e)
           
 boolean addAll(Collection<? extends ZFrame> arg0)
           
 void addFirst(byte[] data)
           
 void addFirst(String stringValue)
           
 void addFirst(ZFrame e)
           
 void addLast(byte[] data)
           
 void addLast(String stringValue)
           
 void addLast(ZFrame e)
           
 void addString(String str)
          Add a String as a new ZFrame to the end of list
 void clear()
           
 boolean contains(Object o)
           
 boolean containsAll(Collection<?> arg0)
           
 long contentSize()
          Return total number of bytes contained in all ZFrames in this ZMsg
 Iterator<ZFrame> descendingIterator()
           
 void destroy()
          Destructor.
 void dump(Appendable out)
          Dump the message in human readable format.
 ZMsg duplicate()
          Creates copy of this ZMsg.
 ZFrame element()
           
 boolean equals(Object o)
           
 ZFrame getFirst()
           
 ZFrame getLast()
           
 int hashCode()
           
 boolean isEmpty()
           
 Iterator<ZFrame> iterator()
           
static ZMsg load(DataInputStream file)
          Load / append a ZMsg from an open DataInputStream
static ZMsg newStringMsg(String... strings)
          Create a new ZMsg from one or more Strings
 boolean offer(ZFrame e)
           
 boolean offerFirst(ZFrame e)
           
 boolean offerLast(ZFrame e)
           
 ZFrame peek()
           
 ZFrame peekFirst()
           
 ZFrame peekLast()
           
 ZFrame poll()
           
 ZFrame pollFirst()
           
 ZFrame pollLast()
           
 ZFrame pop()
           
 String popString()
          Pop a ZFrame and return the toString() representation of it.
 void push(byte[] data)
           
 void push(String str)
           
 void push(ZFrame e)
           
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.
 ZFrame remove()
           
 boolean remove(Object o)
           
 boolean removeAll(Collection<?> arg0)
           
 ZFrame removeFirst()
           
 boolean removeFirstOccurrence(Object o)
           
 ZFrame removeLast()
           
 boolean removeLastOccurrence(Object o)
           
 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)
           
 ZFrame unwrap()
          Pop frame off front of message, caller now owns frame.
 void wrap(ZFrame frame)
          Push frame plus empty frame to front of message, before 1st frame.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ZMsg

public ZMsg()
Class Constructor

Method Detail

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

public void addString(String str)
Add a String as a new ZFrame to the end of list

Parameters:
str - String to add to list

duplicate

public ZMsg 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

public void wrap(ZFrame frame)
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

public ZFrame 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

public void send(ZMQ.Socket socket)
Send message to 0MQ socket.

Parameters:
socket - 0MQ socket to send ZMsg on.

send

public void send(ZMQ.Socket socket,
                 boolean destroy)
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

public static ZMsg recvMsg(ZMQ.Socket socket)
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

public static ZMsg recvMsg(ZMQ.Socket socket,
                           int flag)
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

public static boolean save(ZMsg msg,
                           DataOutputStream file)
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 save
file - DataOutputStream
Returns:
True if saved OK, else false

load

public static ZMsg load(DataInputStream file)
Load / append a ZMsg from an open DataInputStream

Parameters:
file - DataInputStream connected to file
Returns:
ZMsg object

newStringMsg

public static ZMsg newStringMsg(String... strings)
Create a new ZMsg from one or more Strings

Parameters:
strings - Strings to add as frames.
Returns:
ZMsg object

equals

public boolean equals(Object o)
Specified by:
equals in interface Collection<ZFrame>
Overrides:
equals in class Object

hashCode

public int hashCode()
Specified by:
hashCode in interface Collection<ZFrame>
Overrides:
hashCode in class Object

dump

public void dump(Appendable out)
Dump the message in human readable format. This should only be used for debugging and tracing, inefficient in handling large messages.


addFirst

public void addFirst(String stringValue)

addFirst

public void addFirst(byte[] data)

addLast

public void addLast(String stringValue)

addLast

public void addLast(byte[] data)

push

public void push(String str)

push

public void push(byte[] data)

add

public boolean add(String stringValue)

add

public boolean add(byte[] data)

iterator

public Iterator<ZFrame> iterator()
Specified by:
iterator in interface Iterable<ZFrame>
Specified by:
iterator in interface Collection<ZFrame>
Specified by:
iterator in interface Deque<ZFrame>

addAll

public boolean addAll(Collection<? extends ZFrame> arg0)
Specified by:
addAll in interface Collection<ZFrame>

clear

public void clear()
Specified by:
clear in interface Collection<ZFrame>

containsAll

public boolean containsAll(Collection<?> arg0)
Specified by:
containsAll in interface Collection<ZFrame>

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Collection<ZFrame>

removeAll

public boolean removeAll(Collection<?> arg0)
Specified by:
removeAll in interface Collection<ZFrame>

retainAll

public boolean retainAll(Collection<?> arg0)
Specified by:
retainAll in interface Collection<ZFrame>

toArray

public Object[] toArray()
Specified by:
toArray in interface Collection<ZFrame>

toArray

public <T> T[] toArray(T[] arg0)
Specified by:
toArray in interface Collection<ZFrame>

add

public boolean add(ZFrame e)
Specified by:
add in interface Collection<ZFrame>
Specified by:
add in interface Deque<ZFrame>
Specified by:
add in interface Queue<ZFrame>

addFirst

public void addFirst(ZFrame e)
Specified by:
addFirst in interface Deque<ZFrame>

addLast

public void addLast(ZFrame e)
Specified by:
addLast in interface Deque<ZFrame>

contains

public boolean contains(Object o)
Specified by:
contains in interface Collection<ZFrame>
Specified by:
contains in interface Deque<ZFrame>

descendingIterator

public Iterator<ZFrame> descendingIterator()
Specified by:
descendingIterator in interface Deque<ZFrame>

element

public ZFrame element()
Specified by:
element in interface Deque<ZFrame>
Specified by:
element in interface Queue<ZFrame>

getFirst

public ZFrame getFirst()
Specified by:
getFirst in interface Deque<ZFrame>

getLast

public ZFrame getLast()
Specified by:
getLast in interface Deque<ZFrame>

offer

public boolean offer(ZFrame e)
Specified by:
offer in interface Deque<ZFrame>
Specified by:
offer in interface Queue<ZFrame>

offerFirst

public boolean offerFirst(ZFrame e)
Specified by:
offerFirst in interface Deque<ZFrame>

offerLast

public boolean offerLast(ZFrame e)
Specified by:
offerLast in interface Deque<ZFrame>

peek

public ZFrame peek()
Specified by:
peek in interface Deque<ZFrame>
Specified by:
peek in interface Queue<ZFrame>

peekFirst

public ZFrame peekFirst()
Specified by:
peekFirst in interface Deque<ZFrame>

peekLast

public ZFrame peekLast()
Specified by:
peekLast in interface Deque<ZFrame>

poll

public ZFrame poll()
Specified by:
poll in interface Deque<ZFrame>
Specified by:
poll in interface Queue<ZFrame>

pollFirst

public ZFrame pollFirst()
Specified by:
pollFirst in interface Deque<ZFrame>

pollLast

public ZFrame pollLast()
Specified by:
pollLast in interface Deque<ZFrame>

pop

public ZFrame pop()
Specified by:
pop in interface Deque<ZFrame>

popString

public String 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

public void push(ZFrame e)
Specified by:
push in interface Deque<ZFrame>

remove

public ZFrame remove()
Specified by:
remove in interface Deque<ZFrame>
Specified by:
remove in interface Queue<ZFrame>

remove

public boolean remove(Object o)
Specified by:
remove in interface Collection<ZFrame>
Specified by:
remove in interface Deque<ZFrame>

removeFirst

public ZFrame removeFirst()
Specified by:
removeFirst in interface Deque<ZFrame>

removeFirstOccurrence

public boolean removeFirstOccurrence(Object o)
Specified by:
removeFirstOccurrence in interface Deque<ZFrame>

removeLast

public ZFrame removeLast()
Specified by:
removeLast in interface Deque<ZFrame>

removeLastOccurrence

public boolean removeLastOccurrence(Object o)
Specified by:
removeLastOccurrence in interface Deque<ZFrame>

size

public int size()
Specified by:
size in interface Collection<ZFrame>
Specified by:
size in interface Deque<ZFrame>


Copyright © 2013. All Rights Reserved.