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
    • 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(ZMQ.Context context, boolean main, 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
      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

      public void destroySocket(ZMQ.Socket s)
      Destroys managed socket within this context and remove from sockets list
      Parameters:
      s - org.zeromq.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

      public static ZContext shadow(ZContext ctx)
      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

      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 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 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()