Package org.zeromq
Class ZContext
- java.lang.Object
-
- org.zeromq.ZContext
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public class ZContext extends java.lang.Object implements java.io.CloseableZContext 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
Fields Modifier and Type Field Description private ZMQ.ContextcontextReference to underlying Context objectprivate intioThreadsNumber of io threads allocated to this context, default 1private intlingerLinger timeout, default 0private booleanmainIndicates if context object is owned by main thread (useful for multi-threaded applications)private intpipehwmSend/receive HWM for pipesprivate intrcvhwmZMQ_RCVHWM for normal socketsprivate java.util.Set<java.nio.channels.Selector>selectorsList of selectors managed by this ZContextprivate java.util.Set<ZContext>shadowsList of ZContext in the shadowsprivate intsndhwmZMQ_SNDHWM for normal socketsprivate java.util.Set<ZMQ.Socket>socketsList of sockets managed by this ZContext
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidclose()voidcloseSelector(java.nio.channels.Selector selector)Deprecated.createSelector()was exposed by mistake.(package private) voidcloseSocket(ZMQ.Socket s)Close managed socket within this context and remove from sockets list.ZMQ.PollercreatePoller(int size)java.nio.channels.SelectorcreateSelector()Deprecated.this was exposed by mistake.ZMQ.SocketcreateSocket(int type)Deprecated.ZMQ.SocketcreateSocket(SocketType type)Creates a new managed socket within this ZContext instance.voiddestroy()Destructor.voiddestroySocket(ZMQ.Socket s)Deprecated.Not to be used any more.ZMQ.Socketfork(ZThread.IAttachedRunnable runnable, java.lang.Object... args)Create an attached thread, An attached thread gets a ctx and a PAIR pipe back to its parent.ZMQ.ContextgetContext()intgetIoThreads()intgetLinger()java.lang.Thread.UncaughtExceptionHandlergetNotificationExceptionHandler()java.util.List<ZMQ.Socket>getSockets()Return a copy of the list of currently open sockets.java.lang.Thread.UncaughtExceptionHandlergetUncaughtExceptionHandler()booleanisClosed()booleanisEmpty()booleanisMain()(package private) java.nio.channels.Selectorselector()Creates a selector.voidsetContext(ZMQ.Context ctx)Deprecated.This value should not be changed after the ZContext is initialized.voidsetIoThreads(int ioThreads)Deprecated.This value should not be changed after the context is initialized.voidsetLinger(int linger)voidsetMain(boolean main)Deprecated.This value should not be changed after the context is initialized.voidsetNotificationExceptionHandler(java.lang.Thread.UncaughtExceptionHandler handler)InPoller.run(), some non-fatal exceptions can be thrown.voidsetRcvHWM(int rcvhwm)Set initial receive HWM for all new normal sockets created in context.voidsetSndHWM(int sndhwm)Set initial receive HWM for all new normal sockets created in context.voidsetUncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler handler)Set the handler invoked when aPollerabruptly terminates due to an uncaught exception.ZContextshadow()Creates new shadow context.static ZContextshadow(ZContext ctx)Deprecated.use the instance method directly
-
-
-
Field Detail
-
context
private final ZMQ.Context context
Reference to underlying Context object
-
sockets
private final java.util.Set<ZMQ.Socket> sockets
List of sockets managed by this ZContext
-
selectors
private final java.util.Set<java.nio.channels.Selector> selectors
List of selectors managed by this ZContext
-
shadows
private final java.util.Set<ZContext> shadows
List of ZContext in the shadows
-
ioThreads
private final int ioThreads
Number of io threads allocated to this context, default 1
-
main
private final boolean main
Indicates if context object is owned by main thread (useful for multi-threaded applications)
-
linger
private volatile int linger
Linger timeout, default 0
-
pipehwm
private int pipehwm
Send/receive HWM for pipes
-
sndhwm
private volatile int sndhwm
ZMQ_SNDHWM for normal sockets
-
rcvhwm
private volatile int rcvhwm
ZMQ_RCVHWM for normal sockets
-
-
Constructor Detail
-
ZContext
public ZContext()
Class Constructor
-
ZContext
public ZContext(int ioThreads)
-
ZContext
private ZContext(ZContext parent, int ioThreads)
-
-
Method Detail
-
destroy
public void destroy()
Destructor. Call this to gracefully terminate context and close any managed 0MQ sockets
-
createSocket
public ZMQ.Socket createSocket(SocketType type)
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 public ZMQ.Socket createSocket(int type)
Deprecated.- Parameters:
type- socket type (see ZMQ static class members)- Returns:
- Newly created Socket object
-
destroySocket
@Deprecated public void destroySocket(ZMQ.Socket s)
Deprecated.Not to be used any more.ZMQ.Sockethandle 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.Socketobject to destroy
-
closeSocket
void closeSocket(ZMQ.Socket s)
Close managed socket within this context and remove from sockets list. There is no need to call this method as anyZMQ.Socketcreated by this context will call it on termination.- Parameters:
s-ZMQ.Socketobject to destroy
-
createSelector
@Deprecated public java.nio.channels.Selector createSelector()
Deprecated.this was exposed by mistake.Creates a selector. It needs to be closed bycloseSelector(Selector).- Returns:
- a newly created selector.
-
selector
java.nio.channels.Selector selector()
Creates a selector. Resource will be released when context will be closed.- Returns:
- a newly created selector.
-
closeSelector
@Deprecated public void closeSelector(java.nio.channels.Selector selector)
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
public ZMQ.Poller createPoller(int size)
-
shadow
@Deprecated public static ZContext shadow(ZContext ctx)
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
public 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
public ZMQ.Socket fork(ZThread.IAttachedRunnable runnable, java.lang.Object... args)
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 public void setIoThreads(int ioThreads)
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
public void setUncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler handler)
Set the handler invoked when aPollerabruptly 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
public java.lang.Thread.UncaughtExceptionHandler getUncaughtExceptionHandler()
- Returns:
- The handler invoked when a
Pollerabruptly terminates due to an uncaught exception.
-
setNotificationExceptionHandler
public void setNotificationExceptionHandler(java.lang.Thread.UncaughtExceptionHandler handler)
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
public java.lang.Thread.UncaughtExceptionHandler 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 public void setMain(boolean main)
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
public ZMQ.Context getContext()
- Returns:
- the context
-
setContext
@Deprecated public void setContext(ZMQ.Context ctx)
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
public java.util.List<ZMQ.Socket> getSockets()
Return a copy of the list of currently open sockets. Order is not meaningful.- Returns:
- the sockets
-
close
public void close()
- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable
-
isClosed
public boolean isClosed()
-
-