Package org.xnio

Class XnioWorker

All Implemented Interfaces:
Executor, ExecutorService, org.wildfly.common.context.Contextual<XnioWorker>, Configurable, XnioIoFactory

public abstract class XnioWorker extends AbstractExecutorService implements Configurable, ExecutorService, XnioIoFactory, org.wildfly.common.context.Contextual<XnioWorker>
A worker for I/O channel notification.
Since:
3.0
Author:
David M. Lloyd
  • Constructor Details

    • XnioWorker

      protected XnioWorker(XnioWorker.Builder builder)
      Construct a new instance. Intended to be called only from implementations.
      Parameters:
      builder - the worker builder
  • Method Details

    • getContextManager

      public static org.wildfly.common.context.ContextManager<XnioWorker> getContextManager()
      Get the context manager for XNIO workers.
      Returns:
      the context manager (not null)
    • getInstanceContextManager

      public org.wildfly.common.context.ContextManager<XnioWorker> getInstanceContextManager()
      Get the instance context manager for XNIO workers by delegating to getContextManager().
      Specified by:
      getInstanceContextManager in interface org.wildfly.common.context.Contextual<XnioWorker>
      Returns:
      the context manager (not null)
    • createStreamServer

      @Deprecated public AcceptingChannel<? extends ConnectedStreamChannel> createStreamServer(SocketAddress bindAddress, ChannelListener<? super AcceptingChannel<ConnectedStreamChannel>> acceptListener, OptionMap optionMap) throws IOException
      Deprecated.
      Create a stream server, for TCP or UNIX domain servers. The type of server is determined by the bind address.
      Parameters:
      bindAddress - the address to bind to
      acceptListener - the initial accept listener
      optionMap - the initial configuration for the server
      Returns:
      the acceptor
      Throws:
      IOException - if the server could not be created
    • createStreamConnectionServer

      public AcceptingChannel<StreamConnection> createStreamConnectionServer(SocketAddress bindAddress, ChannelListener<? super AcceptingChannel<StreamConnection>> acceptListener, OptionMap optionMap) throws IOException
      Create a stream server, for TCP or UNIX domain servers. The type of server is determined by the bind address.
      Parameters:
      bindAddress - the address to bind to
      acceptListener - the initial accept listener
      optionMap - the initial configuration for the server
      Returns:
      the acceptor
      Throws:
      IOException - if the server could not be created
    • createTcpConnectionServer

      protected AcceptingChannel<StreamConnection> createTcpConnectionServer(InetSocketAddress bindAddress, ChannelListener<? super AcceptingChannel<StreamConnection>> acceptListener, OptionMap optionMap) throws IOException
      Implementation helper method to create a TCP stream server.
      Parameters:
      bindAddress - the address to bind to
      acceptListener - the initial accept listener
      optionMap - the initial configuration for the server
      Returns:
      the acceptor
      Throws:
      IOException - if the server could not be created
    • createLocalStreamConnectionServer

      protected AcceptingChannel<StreamConnection> createLocalStreamConnectionServer(LocalSocketAddress bindAddress, ChannelListener<? super AcceptingChannel<StreamConnection>> acceptListener, OptionMap optionMap) throws IOException
      Implementation helper method to create a UNIX domain stream server.
      Parameters:
      bindAddress - the address to bind to
      acceptListener - the initial accept listener
      optionMap - the initial configuration for the server
      Returns:
      the acceptor
      Throws:
      IOException - if the server could not be created
    • connectStream

      @Deprecated public IoFuture<ConnectedStreamChannel> connectStream(SocketAddress destination, ChannelListener<? super ConnectedStreamChannel> openListener, OptionMap optionMap)
      Deprecated.
      Connect to a remote stream server. The protocol family is determined by the type of the socket address given.
      Parameters:
      destination - the destination address
      openListener - the listener which will be notified when the channel is open, or null for none
      optionMap - the option map
      Returns:
      the future result of this operation
    • connectStream

      @Deprecated public IoFuture<ConnectedStreamChannel> connectStream(SocketAddress destination, ChannelListener<? super ConnectedStreamChannel> openListener, ChannelListener<? super BoundChannel> bindListener, OptionMap optionMap)
      Deprecated.
      Connect to a remote stream server. The protocol family is determined by the type of the socket address given.
      Parameters:
      destination - the destination address
      openListener - the listener which will be notified when the channel is open, or null for none
      bindListener - the listener which will be notified when the channel is bound, or null for none
      optionMap - the option map
      Returns:
      the future result of this operation
    • connectStream

      @Deprecated public IoFuture<ConnectedStreamChannel> connectStream(SocketAddress bindAddress, SocketAddress destination, ChannelListener<? super ConnectedStreamChannel> openListener, ChannelListener<? super BoundChannel> bindListener, OptionMap optionMap)
      Deprecated.
      Connect to a remote stream server. The protocol family is determined by the type of the socket addresses given (which must match).
      Parameters:
      bindAddress - the local address to bind to
      destination - the destination address
      openListener - the listener which will be notified when the channel is open, or null for none
      bindListener - the listener which will be notified when the channel is bound, or null for none
      optionMap - the option map
      Returns:
      the future result of this operation
    • openStreamConnection

      public IoFuture<StreamConnection> openStreamConnection(SocketAddress destination, ChannelListener<? super StreamConnection> openListener, OptionMap optionMap)
      Description copied from interface: XnioIoFactory
      Connect to a remote stream server. The protocol family is determined by the type of the socket address given. If an open listener is used, the channel should not be accessed via the returned IoFuture, and vice-versa.
      Specified by:
      openStreamConnection in interface XnioIoFactory
      Parameters:
      destination - the destination address
      openListener - the listener which will be notified when the channel is open, or null for none
      optionMap - the option map
      Returns:
      the future result of this operation
    • openStreamConnection

      public IoFuture<StreamConnection> openStreamConnection(SocketAddress destination, ChannelListener<? super StreamConnection> openListener, ChannelListener<? super BoundChannel> bindListener, OptionMap optionMap)
      Description copied from interface: XnioIoFactory
      Connect to a remote stream server. The protocol family is determined by the type of the socket address given. If an open listener is used, the channel should not be accessed via the returned IoFuture, and vice-versa.
      Specified by:
      openStreamConnection in interface XnioIoFactory
      Parameters:
      destination - the destination address
      openListener - the listener which will be notified when the channel is open, or null for none
      bindListener - the listener which will be notified when the channel is bound, or null for none
      optionMap - the option map
      Returns:
      the future result of this operation
    • openStreamConnection

      public IoFuture<StreamConnection> openStreamConnection(SocketAddress bindAddress, SocketAddress destination, ChannelListener<? super StreamConnection> openListener, ChannelListener<? super BoundChannel> bindListener, OptionMap optionMap)
      Description copied from interface: XnioIoFactory
      Connect to a remote stream server. The protocol family is determined by the type of the socket addresses given (which must match). If an open listener is used, the channel should not be accessed via the returned IoFuture, and vice-versa.
      Specified by:
      openStreamConnection in interface XnioIoFactory
      Parameters:
      bindAddress - the local address to bind to
      destination - the destination address
      openListener - the listener which will be notified when the channel is open, or null for none
      bindListener - the listener which will be notified when the channel is bound, or null for none
      optionMap - the option map
      Returns:
      the future result of this operation
    • acceptStream

      @Deprecated public IoFuture<ConnectedStreamChannel> acceptStream(SocketAddress destination, ChannelListener<? super ConnectedStreamChannel> openListener, ChannelListener<? super BoundChannel> bindListener, OptionMap optionMap)
      Deprecated.
      Accept a stream connection at a destination address. If a wildcard address is specified, then a destination address is chosen in a manner specific to the OS and/or channel type.
      Parameters:
      destination - the destination (bind) address
      openListener - the listener which will be notified when the channel is open, or null for none
      bindListener - the listener which will be notified when the acceptor is bound, or null for none
      optionMap - the option map
      Returns:
      the future connection
    • acceptStreamConnection

      public IoFuture<StreamConnection> acceptStreamConnection(SocketAddress destination, ChannelListener<? super StreamConnection> openListener, ChannelListener<? super BoundChannel> bindListener, OptionMap optionMap)
      Description copied from interface: XnioIoFactory
      Accept a stream connection at a destination address. If a wildcard address is specified, then a destination address is chosen in a manner specific to the OS and/or channel type.
      Specified by:
      acceptStreamConnection in interface XnioIoFactory
      Parameters:
      destination - the destination (bind) address
      openListener - the listener which will be notified when the channel is open, or null for none
      bindListener - the listener which will be notified when the acceptor is bound, or null for none
      optionMap - the option map
      Returns:
      the future connection
    • connectDatagram

      @Deprecated public IoFuture<ConnectedMessageChannel> connectDatagram(SocketAddress destination, ChannelListener<? super ConnectedMessageChannel> openListener, ChannelListener<? super BoundChannel> bindListener, OptionMap optionMap)
      Deprecated.
      Connect to a remote datagram server. The protocol family is determined by the type of the socket address given.
      Parameters:
      destination - the destination address
      openListener - the listener which will be notified when the channel is open, or null for none
      bindListener - the listener which will be notified when the channel is bound, or null for none
      optionMap - the option map
      Returns:
      the future result of this operation
    • connectDatagram

      @Deprecated public IoFuture<ConnectedMessageChannel> connectDatagram(SocketAddress bindAddress, SocketAddress destination, ChannelListener<? super ConnectedMessageChannel> openListener, ChannelListener<? super BoundChannel> bindListener, OptionMap optionMap)
      Deprecated.
      Connect to a remote datagram server. The protocol family is determined by the type of the socket addresses given (which must match).
      Parameters:
      bindAddress - the local address to bind to
      destination - the destination address
      openListener - the listener which will be notified when the channel is open, or null for none
      bindListener - the listener which will be notified when the channel is bound, or null for none
      optionMap - the option map
      Returns:
      the future result of this operation
    • openMessageConnection

      public IoFuture<MessageConnection> openMessageConnection(SocketAddress destination, ChannelListener<? super MessageConnection> openListener, OptionMap optionMap)
      Description copied from interface: XnioIoFactory
      Connect to a remote message server. The protocol family is determined by the type of the socket address given. If an open listener is used, the channel should not be accessed via the returned IoFuture, and vice-versa.
      Specified by:
      openMessageConnection in interface XnioIoFactory
      Parameters:
      destination - the destination address
      openListener - the listener which will be notified when the channel is open, or null for none
      optionMap - the option map
      Returns:
      the future result of this operation
    • acceptDatagram

      @Deprecated public IoFuture<ConnectedMessageChannel> acceptDatagram(SocketAddress destination, ChannelListener<? super ConnectedMessageChannel> openListener, ChannelListener<? super BoundChannel> bindListener, OptionMap optionMap)
      Deprecated.
      Accept a message connection at a destination address. If a wildcard address is specified, then a destination address is chosen in a manner specific to the OS and/or channel type.
      Parameters:
      destination - the destination (bind) address
      openListener - the listener which will be notified when the channel is open, or null for none
      bindListener - the listener which will be notified when the acceptor is bound, or null for none
      optionMap - the option map
      Returns:
      the future connection
    • acceptMessageConnection

      public IoFuture<MessageConnection> acceptMessageConnection(SocketAddress destination, ChannelListener<? super MessageConnection> openListener, ChannelListener<? super BoundChannel> bindListener, OptionMap optionMap)
      Description copied from interface: XnioIoFactory
      Accept a message connection at a destination address. If a wildcard address is specified, then a destination address is chosen in a manner specific to the OS and/or channel type. If an open listener is used, the channel should not be accessed via the returned IoFuture, and vice-versa.
      Specified by:
      acceptMessageConnection in interface XnioIoFactory
      Parameters:
      destination - the destination (bind) address
      openListener - the listener which will be notified when the channel is open, or null for none
      bindListener - the listener which will be notified when the acceptor is bound, or null for none
      optionMap - the option map
      Returns:
      the future connection
    • createUdpServer

      public MulticastMessageChannel createUdpServer(InetSocketAddress bindAddress, ChannelListener<? super MulticastMessageChannel> bindListener, OptionMap optionMap) throws IOException
      Create a UDP server. The UDP server can be configured to be multicast-capable; this should only be done if multicast is needed, since some providers have a performance penalty associated with multicast. The provider's default executor will be used to execute listener methods.
      Parameters:
      bindAddress - the bind address
      bindListener - the initial open-connection listener
      optionMap - the initial configuration for the server
      Returns:
      the UDP server channel
      Throws:
      IOException - if the server could not be created
      Since:
      3.0
    • createUdpServer

      public MulticastMessageChannel createUdpServer(InetSocketAddress bindAddress, OptionMap optionMap) throws IOException
      Create a UDP server. The UDP server can be configured to be multicast-capable; this should only be done if multicast is needed, since some providers have a performance penalty associated with multicast. The provider's default executor will be used to execute listener methods.
      Parameters:
      bindAddress - the bind address
      optionMap - the initial configuration for the server
      Returns:
      the UDP server channel
      Throws:
      IOException - if the server could not be created
      Since:
      3.0
    • createPipe

      @Deprecated public void createPipe(ChannelListener<? super StreamChannel> leftOpenListener, ChannelListener<? super StreamChannel> rightOpenListener, OptionMap optionMap) throws IOException
      Deprecated.
      Users should prefer the simpler createFullDuplexPipe() instead.
      Open a bidirectional stream pipe.
      Parameters:
      leftOpenListener - the left-hand open listener
      rightOpenListener - the right-hand open listener
      optionMap - the pipe channel configuration
      Throws:
      IOException - if the pipe could not be created
    • createOneWayPipe

      @Deprecated public void createOneWayPipe(ChannelListener<? super StreamSourceChannel> sourceListener, ChannelListener<? super StreamSinkChannel> sinkListener, OptionMap optionMap) throws IOException
      Deprecated.
      Users should prefer the simpler createHalfDuplexPipe() instead.
      Open a unidirectional stream pipe.
      Parameters:
      sourceListener - the source open listener
      sinkListener - the sink open listener
      optionMap - the pipe channel configuration
      Throws:
      IOException - if the pipe could not be created
    • getInflatingChannel

      public StreamSourceChannel getInflatingChannel(StreamSourceChannel delegate, OptionMap options) throws IOException
      Create a stream channel that decompresses the source data according to the configuration in the given option map.
      Parameters:
      delegate - the compressed channel
      options - the configuration options for the channel
      Returns:
      a decompressed channel
      Throws:
      IOException - if the channel could not be constructed
    • getInflatingChannel

      protected StreamSourceChannel getInflatingChannel(StreamSourceChannel delegate, Inflater inflater) throws IOException
      Create a stream channel that decompresses the source data according to the configuration in the given inflater.
      Parameters:
      delegate - the compressed channel
      inflater - the inflater to use
      Returns:
      a decompressed channel
      Throws:
      IOException - if the channel could not be constructed
    • getDeflatingChannel

      public StreamSinkChannel getDeflatingChannel(StreamSinkChannel delegate, OptionMap options) throws IOException
      Create a stream channel that compresses to the destination according to the configuration in the given option map.
      Parameters:
      delegate - the channel to compress to
      options - the configuration options for the channel
      Returns:
      a compressed channel
      Throws:
      IOException - if the channel could not be constructed
    • getDeflatingChannel

      protected StreamSinkChannel getDeflatingChannel(StreamSinkChannel delegate, Deflater deflater) throws IOException
      Create a stream channel that compresses to the destination according to the configuration in the given inflater.
      Parameters:
      delegate - the channel to compress to
      deflater - the deflater to use
      Returns:
      a compressed channel
      Throws:
      IOException - if the channel could not be constructed
    • createFullDuplexPipe

      public ChannelPipe<StreamChannel,StreamChannel> createFullDuplexPipe() throws IOException
      Description copied from interface: XnioIoFactory
      Create a two-way stream pipe.
      Specified by:
      createFullDuplexPipe in interface XnioIoFactory
      Returns:
      the created pipe
      Throws:
      IOException - if the pipe could not be created
    • createFullDuplexPipeConnection

      public ChannelPipe<StreamConnection,StreamConnection> createFullDuplexPipeConnection() throws IOException
      Description copied from interface: XnioIoFactory
      Create a two-way stream pipe.
      Specified by:
      createFullDuplexPipeConnection in interface XnioIoFactory
      Returns:
      the created pipe
      Throws:
      IOException - if the pipe could not be created
    • createHalfDuplexPipe

      public ChannelPipe<StreamSourceChannel,StreamSinkChannel> createHalfDuplexPipe() throws IOException
      Description copied from interface: XnioIoFactory
      Create a one-way stream pipe.
      Specified by:
      createHalfDuplexPipe in interface XnioIoFactory
      Returns:
      the created pipe
      Throws:
      IOException - if the pipe could not be created
    • createFullDuplexPipeConnection

      public ChannelPipe<StreamConnection,StreamConnection> createFullDuplexPipeConnection(XnioIoFactory peer) throws IOException
      Description copied from interface: XnioIoFactory
      Create a two-way stream pipe. The left side will be associated with this factory, and the right side will be associated with the given peer.
      Specified by:
      createFullDuplexPipeConnection in interface XnioIoFactory
      Parameters:
      peer - the peer to use for controlling the remote (right) side
      Returns:
      the created pipe
      Throws:
      IOException - if the pipe could not be created
    • createHalfDuplexPipe

      public ChannelPipe<StreamSourceChannel,StreamSinkChannel> createHalfDuplexPipe(XnioIoFactory peer) throws IOException
      Description copied from interface: XnioIoFactory
      Create a one-way stream pipe. The left (source) side will be associated with this factory, and the right (sink) side will be associated with the given peer.
      Specified by:
      createHalfDuplexPipe in interface XnioIoFactory
      Parameters:
      peer - the peer to use for the sink (right) side
      Returns:
      the created pipe
      Throws:
      IOException - if the pipe could not be created
    • shutdown

      public abstract void shutdown()
      Shut down this worker. This method returns immediately. Upon return worker shutdown will have commenced but not necessarily completed. When worker shutdown is complete, the termination task (if one was defined) will be executed.
      Specified by:
      shutdown in interface ExecutorService
    • shutdownNow

      public abstract List<Runnable> shutdownNow()
      Immediately terminate the worker. Any outstanding tasks are collected and returned in a list. Upon return worker shutdown will have commenced but not necessarily completed; however the worker will only complete its current tasks instead of completing all tasks.
      Specified by:
      shutdownNow in interface ExecutorService
      Returns:
      the list of outstanding tasks
    • isShutdown

      public abstract boolean isShutdown()
      Determine whether the worker has been shut down. Will return true once either shutdown method has been called.
      Specified by:
      isShutdown in interface ExecutorService
      Returns:
      true the worker has been shut down
    • isTerminated

      public abstract boolean isTerminated()
      Determine whether the worker has terminated. Will return true once all worker threads are exited (with the possible exception of the thread running the termination task, if any).
      Specified by:
      isTerminated in interface ExecutorService
      Returns:
      true if the worker is terminated
    • awaitTermination

      public abstract boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException
      Wait for termination.
      Specified by:
      awaitTermination in interface ExecutorService
      Parameters:
      timeout - the amount of time to wait
      unit - the unit of time
      Returns:
      true if termination completed before the timeout expired
      Throws:
      InterruptedException - if the operation was interrupted
    • awaitTermination

      public abstract void awaitTermination() throws InterruptedException
      Wait for termination.
      Throws:
      InterruptedException - if the operation was interrupted
    • getIoThread

      public final XnioIoThread getIoThread()
      Get an I/O thread from this worker. The thread may be chosen based on arbitrary rules.
      Returns:
      the I/O thread
    • getIoThread

      public abstract XnioIoThread getIoThread(int hashCode)
      Get an I/O thread from this worker. The thread is chosen based on the given hash code.
      Parameters:
      hashCode - the hash code
      Returns:
      the thread
    • getTerminationTask

      protected Runnable getTerminationTask()
      Get the user task to run once termination is complete.
      Returns:
      the termination task
    • taskPoolTerminated

      protected void taskPoolTerminated()
      Callback to indicate that the task thread pool has terminated. Not called if the task pool is external.
    • shutDownTaskPool

      protected void shutDownTaskPool()
      Initiate shutdown of the task thread pool. When all the tasks and threads have completed, the taskPoolTerminated() method is called.
    • shutDownTaskPoolNow

      protected List<Runnable> shutDownTaskPoolNow()
      Shut down the task thread pool immediately and return its pending tasks.
      Returns:
      the pending task list
    • isTaskPoolExternal

      protected boolean isTaskPoolExternal()
      Determine whether the worker task pool is managed externally. Externally managed task pools will never respond to shut down requests.
      Returns:
      true if the task pool is externally managed, false otherwise
    • execute

      public void execute(Runnable command)
      Execute a command in the task pool.
      Specified by:
      execute in interface Executor
      Parameters:
      command - the command to run
    • getIoThreadCount

      public abstract int getIoThreadCount()
      Get the number of I/O threads configured on this worker.
      Returns:
      the number of I/O threads configured on this worker
    • supportsOption

      public boolean supportsOption(Option<?> option)
      Description copied from interface: Configurable
      Determine whether an option is supported on this channel.
      Specified by:
      supportsOption in interface Configurable
      Parameters:
      option - the option
      Returns:
      true if it is supported
    • getOption

      public <T> T getOption(Option<T> option) throws IOException
      Description copied from interface: Configurable
      Get the value of a channel option.
      Specified by:
      getOption in interface Configurable
      Type Parameters:
      T - the type of the option value
      Parameters:
      option - the option to get
      Returns:
      the value of the option, or null if it is not set
      Throws:
      IOException - if an I/O error occurred when reading the option
    • setOption

      public <T> T setOption(Option<T> option, T value) throws IllegalArgumentException, IOException
      Description copied from interface: Configurable
      Set an option for this channel. Unsupported options are ignored.
      Specified by:
      setOption in interface Configurable
      Type Parameters:
      T - the type of the option value
      Parameters:
      option - the option to set
      value - the value of the option to set
      Returns:
      the previous option value, if any
      Throws:
      IllegalArgumentException - if the value is not acceptable for this option
      IOException - if an I/O error occurred when modifying the option
    • getXnio

      public Xnio getXnio()
      Get the XNIO provider which produced this worker.
      Returns:
      the XNIO provider
    • getName

      public String getName()
      Get the name of this worker.
      Returns:
      the name of the worker
    • chooseThread

      protected abstract XnioIoThread chooseThread()
      Choose a thread randomly from this worker.
      Returns:
      the thread
    • getCoreWorkerPoolSize

      protected final int getCoreWorkerPoolSize()
      Get the core worker pool size.
      Returns:
      the core worker pool size
    • getBusyWorkerThreadCount

      protected final int getBusyWorkerThreadCount()
      Get an estimate of the number of busy threads in the worker pool.
      Returns:
      the estimated number of busy threads in the worker pool
    • getWorkerPoolSize

      protected final int getWorkerPoolSize()
      Get an estimate of the number of threads in the worker pool.
      Returns:
      the estimated number of threads in the worker pool
    • getMaxWorkerPoolSize

      protected final int getMaxWorkerPoolSize()
      Get the maximum worker pool size.
      Returns:
      the maximum worker pool size
    • getWorkerQueueSize

      protected final int getWorkerQueueSize()
      Get an estimate of the number of tasks in the worker queue.
      Returns:
      the estimated number of tasks
    • getBindAddressTable

      protected org.wildfly.common.net.CidrAddressTable<InetSocketAddress> getBindAddressTable()
      Get the bind address table.
      Returns:
      the bind address table
    • getBindAddress

      public InetSocketAddress getBindAddress(InetAddress destination)
      Get the expected bind address for the given destination, if any.
      Returns:
      the expected bind address for the given destination, or null if no explicit bind will be done
    • getMXBean

      public abstract XnioWorkerMXBean getMXBean()
    • registerServerMXBean

      protected abstract ManagementRegistration registerServerMXBean(XnioServerMXBean metrics)