Package org.zeromq
Class ZContext
java.lang.Object
org.zeromq.ZContext
- All Implemented Interfaces:
Closeable
,AutoCloseable
ZContext provides a high-level ZeroMQ context management class
It manages open sockets in the context and automatically closes these before terminating the context.
It provides a simple way to set the linger timeout on sockets, and configure contexts for number of I/O threads.
Sets-up signal (interrupt) handling for the process.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final ZMQ.Context
Reference to underlying Context objectprivate final int
Number of io threads allocated to this context, default 1private int
Linger timeout, default 0private final boolean
Indicates if context object is owned by main thread (useful for multi-threaded applications)private int
Send/receive HWM for pipesprivate int
ZMQ_RCVHWM for normal socketsList of selectors managed by this ZContextList of ZContext in the shadowsprivate int
ZMQ_SNDHWM for normal socketsprivate final Set
<ZMQ.Socket> List of sockets managed by this ZContext -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
void
closeSelector
(Selector selector) Deprecated.(package private) void
Close managed socket within this context and remove from sockets list.createPoller
(int size) Deprecated.this was exposed by mistake.createSocket
(int type) Deprecated.createSocket
(SocketType type) Creates a new managed socket within this ZContext instance.void
destroy()
Destructor.void
Deprecated.Not to be used any more.fork
(ZThread.IAttachedRunnable runnable, Object... args) Create an attached thread, An attached thread gets a ctx and a PAIR pipe back to its parent.int
int
Return a copy of the list of currently open sockets.boolean
isClosed()
boolean
isEmpty()
boolean
isMain()
(package private) Selector
selector()
Creates a selector.void
setContext
(ZMQ.Context ctx) Deprecated.This value should not be changed after the ZContext is initialized.void
setIoThreads
(int ioThreads) Deprecated.This value should not be changed after the context is initialized.void
setLinger
(int linger) void
setMain
(boolean main) Deprecated.This value should not be changed after the context is initialized.void
InPoller.run()
, some non-fatal exceptions can be thrown.void
setRcvHWM
(int rcvhwm) Set initial receive HWM for all new normal sockets created in context.void
setSndHWM
(int sndhwm) Set initial receive HWM for all new normal sockets created in context.void
Set the handler invoked when aPoller
abruptly terminates due to an uncaught exception.shadow()
Creates new shadow context.static ZContext
Deprecated.use the instance method directly
-
Field Details
-
context
Reference to underlying Context object -
sockets
List of sockets managed by this ZContext -
selectors
List of selectors managed by this ZContext -
shadows
List of ZContext in the shadows -
ioThreads
private final int ioThreadsNumber of io threads allocated to this context, default 1 -
main
private final boolean mainIndicates if context object is owned by main thread (useful for multi-threaded applications) -
linger
private volatile int lingerLinger timeout, default 0 -
pipehwm
private int pipehwmSend/receive HWM for pipes -
sndhwm
private volatile int sndhwmZMQ_SNDHWM for normal sockets -
rcvhwm
private volatile int rcvhwmZMQ_RCVHWM for normal sockets
-
-
Constructor Details
-
ZContext
public ZContext()Class Constructor -
ZContext
public ZContext(int ioThreads) -
ZContext
-
-
Method Details
-
destroy
public void destroy()Destructor. Call this to gracefully terminate context and close any managed 0MQ sockets -
createSocket
Creates a new managed socket within this ZContext instance. Use this to get automatic management of the socket at shutdown.The newly created socket will inherited it's linger value from the one defined for this context.
- Parameters:
type
- socket type- Returns:
- Newly created Socket object
-
createSocket
Deprecated.- Parameters:
type
- socket type (see ZMQ static class members)- Returns:
- Newly created Socket object
-
destroySocket
Deprecated.Not to be used any more.ZMQ.Socket
handle the close itself. It also override linger settings.Destroys a managed socket within this context and remove from sockets list. This method should be used only for fast or emergency close as is set linger instead of using the socket current value.- Parameters:
s
-ZMQ.Socket
object to destroy
-
closeSocket
Close managed socket within this context and remove from sockets list. There is no need to call this method as anyZMQ.Socket
created by this context will call it on termination.- Parameters:
s
-ZMQ.Socket
object to destroy
-
createSelector
Deprecated.this was exposed by mistake.Creates a selector. It needs to be closed bycloseSelector(Selector)
.- Returns:
- a newly created selector.
-
selector
Selector selector()Creates a selector. Resource will be released when context will be closed.- Returns:
- a newly created selector.
-
closeSelector
Deprecated.createSelector()
was exposed by mistake. while waiting for the API to disappear, this method is provided to allow releasing resources.Closes a selector. This is a DRAFT method, and may change without notice.- Parameters:
selector
- the selector to close. It needs to have been created bycreateSelector()
.
-
createPoller
-
shadow
Deprecated.use the instance method directlyCreates new shadow context. Shares same underlying org.zeromq.Context instance but has own list of managed sockets, io thread count etc.- Parameters:
ctx
- Original ZContext to create shadow of- Returns:
- New ZContext
-
shadow
Creates new shadow context. Shares same underlying org.zeromq.Context instance but has own list of managed sockets, io thread count etc.- Returns:
- New ZContext
-
fork
Create an attached thread, An attached thread gets a ctx and a PAIR pipe back to its parent. It must monitor its pipe, and exit if the pipe becomes unreadable- Parameters:
runnable
- attached threadargs
- forked runnable args- Returns:
- pipe or null if there was an error
-
getIoThreads
public int getIoThreads()- Returns:
- the ioThreads
-
setIoThreads
Deprecated.This value should not be changed after the context is initialized.A deprecated function that does nothing.- Parameters:
ioThreads
- the number of ioThreads to set
-
getLinger
public int getLinger()- Returns:
- the default linger for sockets.
-
setLinger
public void setLinger(int linger) - Parameters:
linger
- the linger that will inherited by created socket.
-
setRcvHWM
public void setRcvHWM(int rcvhwm) Set initial receive HWM for all new normal sockets created in context. You can set this per-socket after the socket is created. The default, no matter the underlying ZeroMQ version, is 1,000.- Parameters:
rcvhwm
- the rcvhwm
-
setSndHWM
public void setSndHWM(int sndhwm) Set initial receive HWM for all new normal sockets created in context. You can set this per-socket after the socket is created. The default, no matter the underlying ZeroMQ version, is 1,000.- Parameters:
sndhwm
- the sndhwm
-
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.
-
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.
-
getNotificationExceptionHandler
- Returns:
- The handler invoked when a non-fatal exceptions is thrown in zmq.poll.Poller#run()
-
isMain
public boolean isMain()- Returns:
- the main
-
isEmpty
public boolean isEmpty()- Returns:
- true if no shadow context, no sockets and no selectors are alive.
-
setMain
Deprecated.This value should not be changed after the context is initialized.- Parameters:
main
- whether or not the context is being set to main
-
getContext
- Returns:
- the context
-
setContext
Deprecated.This value should not be changed after the ZContext is initialized.- Parameters:
ctx
- sets the underlying zmq.Context associated with this ZContext wrapper object
-
getSockets
Return a copy of the list of currently open sockets. Order is not meaningful.- Returns:
- the sockets
-
close
public void close()- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
-
isClosed
public boolean isClosed()
-
createSelector()
was exposed by mistake.