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 ZContextprivate int
ZMQ_SNDHWM for normal socketsprivate final Set<ZMQ.Socket>
List of sockets managed by this ZContext -
Constructor Summary
ConstructorsModifierConstructorDescriptionZContext()
Class ConstructorZContext
(int ioThreads) private
ZContext
(ZMQ.Context context, boolean main, int ioThreads) -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
void
closeSelector
(Selector selector) Deprecated.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
Destroys managed socket within this context and remove from sockets listfork
(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
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
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.static ZContext
Creates new shadow context.
-
Field Details
-
context
Reference to underlying Context object -
sockets
List of sockets managed by this ZContext -
selectors
List of selectors managed by this ZContext -
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- 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
Destroys managed socket within this context and remove from sockets list- Parameters:
s
- org.zeromq.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
Creates 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
-
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 linger
-
setLinger
public void setLinger(int linger) - Parameters:
linger
- the linger to set
-
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
-
isMain
public boolean isMain()- Returns:
- the main
-
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.