Class ZMQ
The ØMQ lightweight messaging kernel is a library which extends the standard socket interfaces with features traditionally provided by specialised messaging middleware products. ØMQ sockets provide an abstraction of asynchronous message queues, multiple messaging patterns, message filtering (subscriptions), seamless access to multiple transport protocols and more.
Following is an overview of ØMQ concepts, describes how ØMQ abstracts standard sockets and provides a reference manual for the functions provided by the ØMQ library.
Contexts
Before using any ØMQ library functions you must create a ØMQ context
using context(int)
.
When you exit your application you must destroy the context using ZMQ.Context.close()
.
Thread safety
A ØMQ context is thread safe and may be shared among as many application threads as necessary, without any additional locking required on the part of the caller.Individual ØMQ sockets are not thread safe except in the case where full memory barriers are issued when migrating a socket from one thread to another.
In practice this means applications can create a socket in one thread with *
ZMQ.Context.socket(SocketType)
and then pass it to a newly created thread as part of thread initialization.
Multiple contexts
Multiple contexts may coexist within a single application.Thus, an application can use ØMQ directly and at the same time make use of any number of additional libraries or components which themselves make use of ØMQ as long as the above guidelines regarding thread safety are adhered to.
Messages
A ØMQ message is a discrete unit of data passed between applications or components of the same application. ØMQ messages have no internal structure and from the point of view of ØMQ itself they are considered to be opaque binary data.Sockets
ØMQ sockets
present an abstraction of a asynchronous message queue,
with the exact queueing semantics depending on the socket type in use.
Transports
A ØMQ socket can use multiple different underlying transport mechanisms. Each transport mechanism is suited to a particular purpose and has its own advantages and drawbacks.
The following transport mechanisms are provided:
- Unicast transport using TCP
- Local inter-process communication transport
- Local in-process (inter-thread) communication transport
Proxies
ØMQ provides proxies to create fanout and fan-in topologies. A proxy connects a frontend socket to a backend socket and switches all messages between the two sockets, opaquely. A proxy may optionally capture all traffic to a third socket.
Security
A ØMQ socket can select a security mechanism. Both peers must use the same security mechanism.
The following security mechanisms are provided for IPC and TCP connections:
- Null security
- Plain-text authentication using username and password
- Elliptic curve authentication and encryption
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Container for all sockets in a single process, acting as the transport for inproc sockets, which are the fastest way to connect threads in one process.static class
Class that interfaces the generation of CURVE key pairs.static enum
static class
Inner class: Event.static class
Provides a mechanism for applications to multiplex input/output events in a level-triggered fashion over a set of socketsstatic class
static class
Abstracts an asynchronous message queue, with the exact queuing semantics depending on the socket type in use. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Charset
static final int
Deprecated.static final int
Socket flag to indicate a nonblocking send or recv mode.static final int
Deprecated.static final int
EVENT_ACCEPT_FAILED: could not accept client connection.static final int
EVENT_ACCEPTED: connection accepted to bound interface.static final int
EVENT_ALL: all events known.static final int
EVENT_BIND_FAILED: socket could not bind to an address.static final int
EVENT_CLOSE_FAILED: connection couldn't be closed.static final int
EVENT_CLOSED: connection closed.static final int
EVENT_CONNECT_DELAYED: synchronous connect failed, it's being polled.static final int
EVENT_CONNECT_RETRIED: asynchronous connect / reconnection attempt.static final int
EVENT_CONNECTED: connection established.static final int
Deprecated.static final int
EVENT_DISCONNECTED: broken session.static final int
EVENT_HANDSHAKE_PROTOCOL: protocol has been successfully negotiated.static final int
EVENT_LISTENING: socket bound to an address, ready to accept connections.static final int
EVENT_MONITOR_STOPPED: monitor has been stopped.static final int
Deprecated.static final int
Deprecated.static final byte[]
static final int
static final int
Deprecated.static final byte[]
static final byte[]
static final byte[]
static final int
Deprecated.static final int
Deprecated.static final int
Deprecated.static final int
Deprecated.static final int
Deprecated.static final int
Deprecated.static final int
Deprecated.static final int
Socket flag to indicate that more message parts are coming.static final int
Deprecated.static final int
Deprecated.static final int
Deprecated.static final byte[]
static final int
Deprecated.static final int
Deprecated.static final int
Deprecated.static final int
Deprecated.As of release 3.0 of zeromq, replaced byDEALER
static final int
Deprecated. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ZMQ.Context
context
(int ioThreads) Create a new Context.static boolean
device
(int type, ZMQ.Socket frontend, ZMQ.Socket backend) Deprecated.static int
static int
static int
static int
static String
static int
makeVersion
(int major, int minor, int patch) static void
msleep
(long millis) static int
poll
(Selector selector, ZMQ.PollItem[] items, int count, long timeout) static int
poll
(Selector selector, ZMQ.PollItem[] items, long timeout) static boolean
proxy
(ZMQ.Socket frontend, ZMQ.Socket backend, ZMQ.Socket capture) Starts the built-in 0MQ proxy in the current application thread.static boolean
proxy
(ZMQ.Socket frontend, ZMQ.Socket backend, ZMQ.Socket capture, ZMQ.Socket control) static void
sleep
(long seconds) static void
-
Field Details
-
SNDMORE
public static final int SNDMORESocket flag to indicate that more message parts are coming.- See Also:
-
DONTWAIT
public static final int DONTWAITSocket flag to indicate a nonblocking send or recv mode.- See Also:
-
NOBLOCK
public static final int NOBLOCK- See Also:
-
PAIR
Deprecated.- See Also:
-
PUB
Deprecated.- See Also:
-
SUB
Deprecated.- See Also:
-
REQ
Deprecated.- See Also:
-
REP
Deprecated.- See Also:
-
DEALER
Deprecated.- See Also:
-
XREQ
Deprecated.As of release 3.0 of zeromq, replaced byDEALER
Old alias for DEALER flag. Flag to specify a XREQ socket, receiving side must be a XREP.- See Also:
-
ROUTER
Deprecated.- See Also:
-
XREP
Deprecated.As of release 3.0 of zeromq, replaced byROUTER
Old alias for ROUTER flag. Flag to specify the receiving part of a XREQ socket.- See Also:
-
PULL
Deprecated.- See Also:
-
PUSH
Deprecated.- See Also:
-
XPUB
Deprecated.- See Also:
-
XSUB
Deprecated.- See Also:
-
STREAM
Deprecated.- See Also:
-
STREAMER
Deprecated.Flag to specify a STREAMER device.- See Also:
-
FORWARDER
Deprecated.Flag to specify a FORWARDER device.- See Also:
-
QUEUE
Deprecated.Flag to specify a QUEUE device.- See Also:
-
UPSTREAM
Deprecated.- See Also:
-
DOWNSTREAM
Deprecated.- See Also:
-
EVENT_CONNECTED
public static final int EVENT_CONNECTEDEVENT_CONNECTED: connection established. The EVENT_CONNECTED event triggers when a connection has been established to a remote peer. This can happen either synchronous or asynchronous. Value is the FD of the newly connected socket.- See Also:
-
EVENT_CONNECT_DELAYED
public static final int EVENT_CONNECT_DELAYEDEVENT_CONNECT_DELAYED: synchronous connect failed, it's being polled. The EVENT_CONNECT_DELAYED event triggers when an immediate connection attempt is delayed and its completion is being polled for. Value has no meaning.- See Also:
-
EVENT_DELAYED
Deprecated.- See Also:
-
EVENT_CONNECT_RETRIED
public static final int EVENT_CONNECT_RETRIEDEVENT_CONNECT_RETRIED: asynchronous connect / reconnection attempt. The EVENT_CONNECT_RETRIED event triggers when a connection attempt is being handled by reconnect timer. The reconnect interval's recomputed for each attempt. Value is the reconnect interval.- See Also:
-
EVENT_RETRIED
Deprecated.- See Also:
-
EVENT_LISTENING
public static final int EVENT_LISTENINGEVENT_LISTENING: socket bound to an address, ready to accept connections. The EVENT_LISTENING event triggers when a socket's successfully bound to a an interface. Value is the FD of the newly bound socket.- See Also:
-
EVENT_BIND_FAILED
public static final int EVENT_BIND_FAILEDEVENT_BIND_FAILED: socket could not bind to an address. The EVENT_BIND_FAILED event triggers when a socket could not bind to a given interface. Value is the errno generated by the bind call.- See Also:
-
EVENT_ACCEPTED
public static final int EVENT_ACCEPTEDEVENT_ACCEPTED: connection accepted to bound interface. The EVENT_ACCEPTED event triggers when a connection from a remote peer has been established with a socket's listen address. Value is the FD of the accepted socket.- See Also:
-
EVENT_ACCEPT_FAILED
public static final int EVENT_ACCEPT_FAILEDEVENT_ACCEPT_FAILED: could not accept client connection. The EVENT_ACCEPT_FAILED event triggers when a connection attempt to a socket's bound address fails. Value is the errno generated by accept.- See Also:
-
EVENT_CLOSED
public static final int EVENT_CLOSEDEVENT_CLOSED: connection closed. The EVENT_CLOSED event triggers when a connection's underlying descriptor has been closed. Value is the former FD of the for the closed socket. FD has been closed already!- See Also:
-
EVENT_CLOSE_FAILED
public static final int EVENT_CLOSE_FAILEDEVENT_CLOSE_FAILED: connection couldn't be closed. The EVENT_CLOSE_FAILED event triggers when a descriptor could not be released back to the OS. Implementation note: ONLY FOR IPC SOCKETS. Value is the errno generated by unlink.- See Also:
-
EVENT_DISCONNECTED
public static final int EVENT_DISCONNECTEDEVENT_DISCONNECTED: broken session. The EVENT_DISCONNECTED event triggers when the stream engine (tcp and ipc specific) detects a corrupted / broken session. Value is the FD of the socket.- See Also:
-
EVENT_MONITOR_STOPPED
public static final int EVENT_MONITOR_STOPPEDEVENT_MONITOR_STOPPED: monitor has been stopped. The EVENT_MONITOR_STOPPED event triggers when the monitor for a socket is stopped.- See Also:
-
EVENT_HANDSHAKE_PROTOCOL
public static final int EVENT_HANDSHAKE_PROTOCOLEVENT_HANDSHAKE_PROTOCOL: protocol has been successfully negotiated. The EVENT_HANDSHAKE_PROTOCOL event triggers when the stream engine (tcp and ipc) successfully negotiated a protocol version with the peer. Value is the version number (0 for unversioned, 3 for V3).- See Also:
-
EVENT_ALL
public static final int EVENT_ALLEVENT_ALL: all events known. The EVENT_ALL constant can be used to set up a monitor for all known events.- See Also:
-
MESSAGE_SEPARATOR
public static final byte[] MESSAGE_SEPARATOR -
SUBSCRIPTION_ALL
public static final byte[] SUBSCRIPTION_ALL -
PROXY_PAUSE
public static final byte[] PROXY_PAUSE -
PROXY_RESUME
public static final byte[] PROXY_RESUME -
PROXY_TERMINATE
public static final byte[] PROXY_TERMINATE -
CHARSET
-
-
Constructor Details
-
ZMQ
private ZMQ()
-
-
Method Details
-
context
Create a new Context.- Parameters:
ioThreads
- Number of threads to use, usually 1 is sufficient for most use cases.- Returns:
- the Context
-
device
Deprecated. -
proxy
Starts the built-in 0MQ proxy in the current application thread. The proxy connects a frontend socket to a backend socket. Conceptually, data flows from frontend to backend. Depending on the socket types, replies may flow in the opposite direction. The direction is conceptual only; the proxy is fully symmetric and there is no technical difference between frontend and backend.Before calling ZMQ.proxy() you must set any socket options, and connect or bind both frontend and backend sockets. The two conventional proxy models are:
ZMQ.proxy() runs in the current thread and returns only if/when the current context is closed.
- Parameters:
frontend
- ZMQ.Socketbackend
- ZMQ.Socketcapture
- If the capture socket is not NULL, the proxy shall send all messages, received on both frontend and backend, to the capture socket. The capture socket should be a ZMQ_PUB, ZMQ_DEALER, ZMQ_PUSH, or ZMQ_PAIR socket.
-
proxy
public static boolean proxy(ZMQ.Socket frontend, ZMQ.Socket backend, ZMQ.Socket capture, ZMQ.Socket control) -
poll
-
poll
-
getMajorVersion
public static int getMajorVersion()- Returns:
- Major version number of the ZMQ library.
-
getMinorVersion
public static int getMinorVersion()- Returns:
- Major version number of the ZMQ library.
-
getPatchVersion
public static int getPatchVersion()- Returns:
- Major version number of the ZMQ library.
-
getFullVersion
public static int getFullVersion()- Returns:
- Full version number of the ZMQ library used for comparing versions.
-
makeVersion
public static int makeVersion(int major, int minor, int patch) - Parameters:
major
- Version major component.minor
- Version minor component.patch
- Version patch component.- Returns:
- Comparible single int version number.
-
getVersionString
- Returns:
- String version number in the form major.minor.patch.
-
msleep
public static void msleep(long millis) -
sleep
public static void sleep(long seconds) -
sleep
-
ROUTER