Package org.zeromq

Class ZContext

java.lang.Object
org.zeromq.ZContext
All Implemented Interfaces:
Closeable, AutoCloseable

public class ZContext extends Object implements Closeable
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 Details

    • context

      private final ZMQ.Context context
      Reference to underlying Context object
    • sockets

      private final Set<ZMQ.Socket> sockets
      List of sockets managed by this ZContext
    • selectors

      private final Set<Selector> selectors
      List of selectors managed by this ZContext
    • shadows

      private final 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 Details

    • ZContext

      public ZContext()
      Class Constructor
    • ZContext

      public ZContext(int ioThreads)
    • ZContext

      private ZContext(ZContext parent, int ioThreads)
  • Method Details

    • 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.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

      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 any ZMQ.Socket created by this context will call it on termination.
      Parameters:
      s - ZMQ.Socket object to destroy
    • createSelector

      @Deprecated public Selector createSelector()
      Deprecated.
      this was exposed by mistake.
      Creates a selector. It needs to be closed by closeSelector(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 public void closeSelector(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 by createSelector().
    • createPoller

      public ZMQ.Poller createPoller(int size)
    • shadow

      @Deprecated public static ZContext shadow(ZContext ctx)
      Deprecated.
      use the instance method directly
      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
    • 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, 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 thread
      args - 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(Thread.UncaughtExceptionHandler handler)
      Set the handler invoked when a Poller 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

      public Thread.UncaughtExceptionHandler getUncaughtExceptionHandler()
      Returns:
      The handler invoked when a Poller abruptly terminates due to an uncaught exception.
    • setNotificationExceptionHandler

      public void setNotificationExceptionHandler(Thread.UncaughtExceptionHandler handler)
      In Poller.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 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 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:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
    • isClosed

      public boolean isClosed()