Package org.zeromq

Class ZMQ.Context

java.lang.Object
org.zeromq.ZMQ.Context
All Implemented Interfaces:
Closeable, AutoCloseable
Enclosing class:
ZMQ

public static class ZMQ.Context extends Object implements Closeable
Container for all sockets in a single process, acting as the transport for inproc sockets, which are the fastest way to connect threads in one process.
  • Constructor Details

    • Context

      protected Context(int ioThreads)
      Class constructor.
      Parameters:
      ioThreads - size of the threads pool to handle I/O operations.
  • Method Details

    • isTerminated

      public boolean isTerminated()
      Returns true if terminate() has been called on ctx.
    • getIOThreads

      public int getIOThreads()
      The size of the 0MQ thread pool to handle I/O operations.
    • setIOThreads

      public boolean setIOThreads(int ioThreads)
      Set the size of the 0MQ thread pool to handle I/O operations.
      Throws:
      IllegalStateException - If context was already initialized by the creation of a socket
    • getMaxSockets

      public int getMaxSockets()
      The maximum number of sockets allowed on the context
    • setMaxSockets

      public boolean setMaxSockets(int maxSockets)
      Sets the maximum number of sockets allowed on the context
      Throws:
      IllegalStateException - If context was already initialized by the creation of a socket
    • getBlocky

      @Deprecated public boolean getBlocky()
      Deprecated.
      use isBlocky() instead
    • isBlocky

      public boolean isBlocky()
    • setBlocky

      public boolean setBlocky(boolean block)
    • isIPv6

      public boolean isIPv6()
    • getIPv6

      public boolean getIPv6()
    • setIPv6

      public boolean setIPv6(boolean ipv6)
    • 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.
      Throws:
      IllegalStateException - If context was already initialized by the creation of a socket
    • 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.
      Throws:
      IllegalStateException - If context was already initialized by the creation of a socket
    • getNotificationExceptionHandler

      public Thread.UncaughtExceptionHandler getNotificationExceptionHandler()
      Returns:
      The handler invoked when a non-fatal exceptions is thrown in zmq.poll.Poller#run()
    • setThreadFactor

      public void setThreadFactor(BiFunction<Runnable,String,Thread> threadFactory)
      Used to define a custom thread factory. It can be used to create thread that will be bounded to a CPU for performance or tweaks the created thread. It the UncaughtExceptionHandler is not set, the created thread UncaughtExceptionHandler will not be changed, so the factory can also be used to set it.
      Parameters:
      threadFactory - the thread factory used by Poller
      Throws:
      IllegalStateException - If context was already initialized by the creation of a socket
    • getThreadFactory

      public BiFunction<Runnable,String,Thread> getThreadFactory()
      Returns:
      the current thread factory
    • term

      public void term()
      This is an explicit "destructor". It can be called to ensure the corresponding 0MQ Context has been disposed of.
    • isClosed

      public boolean isClosed()
    • socket

      public ZMQ.Socket socket(SocketType type)
      Creates a ØMQ socket within the specified context and return an opaque handle to the newly created socket.
      The type argument specifies the socket type, which determines the semantics of communication over the socket.
      The newly created socket is initially unbound, and not associated with any endpoints.
      In order to establish a message flow a socket must first be connected to at least one endpoint with ZMQ.Socket.connect(String), or at least one endpoint must be created for accepting incoming connections with ZMQ.Socket.bind(String).
      Parameters:
      type - the socket type.
      Returns:
      the newly created Socket.
    • socket

      @Deprecated public ZMQ.Socket socket(int type)
      Deprecated.
    • selector

      public Selector selector()
      Create a new Selector within this context.
      Returns:
      the newly created Selector.
    • close

      public boolean close(Selector selector)
      Closes a Selector that was created within this context.
      Parameters:
      selector - the Selector to close.
      Returns:
      true if the selector was closed. otherwise false (mostly because it was not created by the context).
    • poller

      public ZMQ.Poller poller()
      Create a new Poller within this context, with a default size. DO NOT FORGET TO CLOSE THE POLLER AFTER USE with ZMQ.Poller.close()
      Returns:
      the newly created Poller.
    • poller

      public ZMQ.Poller poller(int size)
      Create a new Poller within this context, with a specified initial size. DO NOT FORGET TO CLOSE THE POLLER AFTER USE with ZMQ.Poller.close()
      Parameters:
      size - the poller initial size.
      Returns:
      the newly created Poller.
    • close

      public void close()
      Destroys the ØMQ context context. Context termination is performed in the following steps:
      • Any blocking operations currently in progress on sockets open within context shall return immediately with an error code of ETERM. With the exception of ZMQ.Socket.close(), any further operations on sockets open within context shall fail with an error code of ETERM.
      • After interrupting all blocking calls, this method shall block until the following conditions are satisfied:
      Warning
      As ZMQ_LINGER defaults to "infinite", by default this method will block indefinitely if there are any pending connects or sends. We strongly recommend to
      • set ZMQ_LINGER to zero on all sockets
      • close all sockets, before calling this method
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable