Package org.zeromq
Class ZMQ.Context
java.lang.Object
org.zeromq.ZMQ.Context
- All Implemented Interfaces:
Closeable
,AutoCloseable
- Enclosing class:
ZMQ
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.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Destroys the ØMQ context context.boolean
Closes a Selector that was created within this context.boolean
Deprecated.int
The size of the 0MQ thread pool to handle I/O operations.boolean
getIPv6()
int
The maximum number of sockets allowed on the contextboolean
isBlocky()
boolean
isClosed()
boolean
isIPv6()
boolean
Returns true if terminate() has been called on ctx.poller()
Create a new Poller within this context, with a default size.poller
(int size) Create a new Poller within this context, with a specified initial size.selector()
Create a new Selector within this context.boolean
setBlocky
(boolean block) boolean
setIOThreads
(int ioThreads) Set the size of the 0MQ thread pool to handle I/O operations.boolean
setIPv6
(boolean ipv6) boolean
setMaxSockets
(int maxSockets) Sets the maximum number of sockets allowed on the contextvoid
InPoller.run()
, some non-fatal exceptions can be thrown.void
setThreadFactor
(BiFunction<Runnable, String, Thread> threadFactory) Used to define a custom thread factory.void
Set the handler invoked when aPoller
abruptly terminates due to an uncaught exception.socket
(int type) Deprecated.socket
(SocketType type) Creates a ØMQ socket within the specified context and return an opaque handle to the newly created socket.void
term()
This is an explicit "destructor".
-
Constructor Details
-
Context
protected Context(int ioThreads) Class constructor.- Parameters:
ioThreads
- size of the threads pool to handle I/O operations.
-
-
Method Details
-
isTerminated
public boolean isTerminated()Returns true if terminate() has been called on ctx. -
getIOThreads
public int getIOThreads()The size of the 0MQ thread pool to handle I/O operations. -
setIOThreads
public boolean setIOThreads(int ioThreads) Set the size of the 0MQ thread pool to handle I/O operations.- Throws:
IllegalStateException
- If context was already initialized by the creation of a socket
-
getMaxSockets
public int getMaxSockets()The maximum number of sockets allowed on the context -
setMaxSockets
public boolean setMaxSockets(int maxSockets) Sets the maximum number of sockets allowed on the context- Throws:
IllegalStateException
- If context was already initialized by the creation of a socket
-
getBlocky
Deprecated.useisBlocky()
instead -
isBlocky
public boolean isBlocky() -
setBlocky
public boolean setBlocky(boolean block) -
isIPv6
public boolean isIPv6() -
getIPv6
public boolean getIPv6() -
setIPv6
public boolean setIPv6(boolean ipv6) -
setUncaughtExceptionHandler
Set the handler invoked when aPoller
abruptly terminates due to an uncaught exception.It default to the value of
Thread.getDefaultUncaughtExceptionHandler()
- Parameters:
handler
- The object to use as this thread's uncaught exception handler. If null then this thread has no explicit handler.- Throws:
IllegalStateException
- If context was already initialized by the creation of a socket
-
getUncaughtExceptionHandler
- Returns:
- The handler invoked when a
Poller
abruptly terminates due to an uncaught exception.
-
setNotificationExceptionHandler
InPoller.run()
, some non-fatal exceptions can be thrown. This handler will be notified, so they can be logged.Default to
Throwable.printStackTrace()
- Parameters:
handler
- The object to use as this thread's handler for recoverable exceptions notifications.- Throws:
IllegalStateException
- If context was already initialized by the creation of a socket
-
getNotificationExceptionHandler
- Returns:
- The handler invoked when a non-fatal exceptions is thrown in zmq.poll.Poller#run()
-
setThreadFactor
Used to define a custom thread factory. It can be used to create thread that will be bounded to a CPU for performance or tweaks the created thread. It the UncaughtExceptionHandler is not set, the created thread UncaughtExceptionHandler will not be changed, so the factory can also be used to set it.- Parameters:
threadFactory
- the thread factory used byPoller
- Throws:
IllegalStateException
- If context was already initialized by the creation of a socket
-
getThreadFactory
- Returns:
- the current thread factory
-
term
public void term()This is an explicit "destructor". It can be called to ensure the corresponding 0MQ Context has been disposed of. -
isClosed
public boolean isClosed() -
socket
Creates a ØMQ socket within the specified context and return an opaque handle to the newly created socket.
The type argument specifies the socket type, which determines the semantics of communication over the socket.
The newly created socket is initially unbound, and not associated with any endpoints.
In order to establish a message flow a socket must first be connected to at least one endpoint withZMQ.Socket.connect(String)
, or at least one endpoint must be created for accepting incoming connections withZMQ.Socket.bind(String)
.- Parameters:
type
- the socket type.- Returns:
- the newly created Socket.
-
socket
Deprecated. -
selector
Create a new Selector within this context.- Returns:
- the newly created Selector.
-
close
Closes a Selector that was created within this context.- Parameters:
selector
- the Selector to close.- Returns:
- true if the selector was closed. otherwise false (mostly because it was not created by the context).
-
poller
Create a new Poller within this context, with a default size. DO NOT FORGET TO CLOSE THE POLLER AFTER USE withZMQ.Poller.close()
- Returns:
- the newly created Poller.
-
poller
Create a new Poller within this context, with a specified initial size. DO NOT FORGET TO CLOSE THE POLLER AFTER USE withZMQ.Poller.close()
- Parameters:
size
- the poller initial size.- Returns:
- the newly created Poller.
-
close
public void close()Destroys the ØMQ context context. Context termination is performed in the following steps:- Any blocking operations currently in progress on sockets open within context
shall return immediately with an error code of ETERM.
With the exception of
ZMQ.Socket.close()
, any further operations on sockets open within context shall fail with an error code of ETERM. - After interrupting all blocking calls, this method shall block until the following conditions are satisfied:
- All sockets open within context have been closed with
ZMQ.Socket.close()
. - For each socket within context, all messages sent by the application with
ZMQ.Socket.send(java.lang.String)
have either been physically transferred to a network peer, or the socket's linger period set with theZMQ.Socket.setLinger(int)
socket option has expired.
- All sockets open within context have been closed with
As ZMQ_LINGER defaults to "infinite", by default this method will block indefinitely if there are any pending connects or sends. We strongly recommend to- set ZMQ_LINGER to zero on all sockets
- close all sockets, before calling this method
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Any blocking operations currently in progress on sockets open within context
shall return immediately with an error code of ETERM.
With the exception of
-
isBlocky()
instead