XNIO API 3.0.7.GA

org.xnio
Class XnioWorker

java.lang.Object
  extended by java.util.concurrent.AbstractExecutorService
      extended by org.xnio.XnioWorker
All Implemented Interfaces:
Executor, ExecutorService, Configurable

public abstract class XnioWorker
extends AbstractExecutorService
implements Configurable, ExecutorService

A worker for I/O channel notification.

Since:
3.0
Author:
David M. Lloyd

Constructor Summary
protected XnioWorker(Xnio xnio, ThreadGroup threadGroup, OptionMap optionMap, Runnable terminationTask)
          Construct a new instance.
 
Method Summary
 IoFuture<ConnectedMessageChannel> acceptDatagram(SocketAddress destination, ChannelListener<? super ConnectedMessageChannel> openListener, ChannelListener<? super BoundChannel> bindListener, OptionMap optionMap)
          Accept a message connection at a destination address.
protected  IoFuture<ConnectedMessageChannel> acceptLocalDatagram(LocalSocketAddress destination, ChannelListener<? super ConnectedMessageChannel> openListener, ChannelListener<? super BoundChannel> bindListener, OptionMap optionMap)
          Implementation helper method to accept a local (UNIX domain) datagram connection.
protected  IoFuture<ConnectedStreamChannel> acceptLocalStream(LocalSocketAddress destination, ChannelListener<? super ConnectedStreamChannel> openListener, ChannelListener<? super BoundChannel> bindListener, OptionMap optionMap)
          Implementation helper method to accept a local (UNIX domain) stream connection.
 IoFuture<ConnectedStreamChannel> acceptStream(SocketAddress destination, ChannelListener<? super ConnectedStreamChannel> openListener, ChannelListener<? super BoundChannel> bindListener, OptionMap optionMap)
          Accept a stream connection at a destination address.
protected  IoFuture<ConnectedStreamChannel> acceptTcpStream(InetSocketAddress destination, ChannelListener<? super ConnectedStreamChannel> openListener, ChannelListener<? super BoundChannel> bindListener, OptionMap optionMap)
          Implementation helper method to accept a TCP connection.
abstract  boolean awaitTermination(long timeout, TimeUnit unit)
           
 IoFuture<ConnectedMessageChannel> connectDatagram(SocketAddress destination, ChannelListener<? super ConnectedMessageChannel> openListener, ChannelListener<? super BoundChannel> bindListener, OptionMap optionMap)
          Connect to a remote stream server.
 IoFuture<ConnectedMessageChannel> connectDatagram(SocketAddress bindAddress, SocketAddress destination, ChannelListener<? super ConnectedMessageChannel> openListener, ChannelListener<? super BoundChannel> bindListener, OptionMap optionMap)
          Connect to a remote datagram server.
protected  IoFuture<ConnectedMessageChannel> connectLocalDatagram(LocalSocketAddress bindAddress, LocalSocketAddress destination, ChannelListener<? super ConnectedMessageChannel> openListener, ChannelListener<? super BoundChannel> bindListener, OptionMap optionMap)
          Implementation helper method to connect to a local (UNIX domain) datagram server.
protected  IoFuture<ConnectedStreamChannel> connectLocalStream(LocalSocketAddress bindAddress, LocalSocketAddress destinationAddress, ChannelListener<? super ConnectedStreamChannel> openListener, ChannelListener<? super BoundChannel> bindListener, OptionMap optionMap)
          Implementation helper method to connect to a local (UNIX domain) server.
 IoFuture<ConnectedStreamChannel> connectStream(SocketAddress destination, ChannelListener<? super ConnectedStreamChannel> openListener, ChannelListener<? super BoundChannel> bindListener, OptionMap optionMap)
          Connect to a remote stream server.
 IoFuture<ConnectedStreamChannel> connectStream(SocketAddress destination, ChannelListener<? super ConnectedStreamChannel> openListener, OptionMap optionMap)
          Connect to a remote stream server.
 IoFuture<ConnectedStreamChannel> connectStream(SocketAddress bindAddress, SocketAddress destination, ChannelListener<? super ConnectedStreamChannel> openListener, ChannelListener<? super BoundChannel> bindListener, OptionMap optionMap)
          Connect to a remote stream server.
protected  IoFuture<ConnectedStreamChannel> connectTcpStream(InetSocketAddress bindAddress, InetSocketAddress destinationAddress, ChannelListener<? super ConnectedStreamChannel> openListener, ChannelListener<? super BoundChannel> bindListener, OptionMap optionMap)
          Implementation helper method to connect to a TCP server.
protected  IoFuture<ConnectedMessageChannel> connectUdpDatagram(InetSocketAddress bindAddress, InetSocketAddress destination, ChannelListener<? super ConnectedMessageChannel> openListener, ChannelListener<? super BoundChannel> bindListener, OptionMap optionMap)
          Implementation helper method to connect to a UDP server.
protected  AcceptingChannel<? extends ConnectedStreamChannel> createLocalStreamServer(LocalSocketAddress bindAddress, ChannelListener<? super AcceptingChannel<ConnectedStreamChannel>> acceptListener, OptionMap optionMap)
          Implementation helper method to create a UNIX domain stream server.
 void createOneWayPipe(ChannelListener<? super StreamSourceChannel> sourceListener, ChannelListener<? super StreamSinkChannel> sinkListener, OptionMap optionMap)
          Open a unidirectional stream pipe.
 void createPipe(ChannelListener<? super StreamChannel> leftOpenListener, ChannelListener<? super StreamChannel> rightOpenListener, OptionMap optionMap)
          Open a bidirectional stream pipe.
 AcceptingChannel<? extends ConnectedStreamChannel> createStreamServer(SocketAddress bindAddress, ChannelListener<? super AcceptingChannel<ConnectedStreamChannel>> acceptListener, OptionMap optionMap)
          Create a stream server, for TCP or UNIX domain servers.
protected  AcceptingChannel<? extends ConnectedStreamChannel> createTcpServer(InetSocketAddress bindAddress, ChannelListener<? super AcceptingChannel<ConnectedStreamChannel>> acceptListener, OptionMap optionMap)
          Implementation helper method to create a TCP stream server.
 MulticastMessageChannel createUdpServer(InetSocketAddress bindAddress, ChannelListener<? super MulticastMessageChannel> bindListener, OptionMap optionMap)
          Create a UDP server.
 MulticastMessageChannel createUdpServer(InetSocketAddress bindAddress, OptionMap optionMap)
          Create a UDP server.
 void execute(Runnable command)
          Execute a command in the task pool.
 String getName()
          Get the name of this worker.
<T> T
getOption(Option<T> option)
          Get the value of a channel option.
protected  Runnable getTerminationTask()
          Get the user task to run once termination is complete.
 Xnio getXnio()
          Get the XNIO provider which produced this worker.
abstract  boolean isShutdown()
           
abstract  boolean isTerminated()
           
<T> T
setOption(Option<T> option, T value)
          Set an option for this channel.
abstract  void shutdown()
           
abstract  List<Runnable> shutdownNow()
           
protected  void shutDownTaskPool()
          Initiate shutdown of the task thread pool.
protected  List<Runnable> shutDownTaskPoolNow()
          Shut down the task thread pool immediately and return its pending tasks.
 boolean supportsOption(Option<?> option)
          Determine whether an option is supported on this channel.
protected  void taskPoolTerminated()
          Callback to indicate that the task thread pool has terminated.
 
Methods inherited from class java.util.concurrent.AbstractExecutorService
invokeAll, invokeAll, invokeAny, invokeAny, newTaskFor, newTaskFor, submit, submit, submit
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.concurrent.ExecutorService
invokeAll, invokeAll, invokeAny, invokeAny, submit, submit, submit
 

Constructor Detail

XnioWorker

protected XnioWorker(Xnio xnio,
                     ThreadGroup threadGroup,
                     OptionMap optionMap,
                     Runnable terminationTask)
Construct a new instance. Intended to be called only from implementations. To construct an XNIO worker, use the Xnio.createWorker(OptionMap) method.

Parameters:
xnio - the XNIO provider which produced this worker instance
threadGroup - the thread group for worker threads
optionMap - the option map to use to configure this worker
terminationTask -
Method Detail

createStreamServer

public AcceptingChannel<? extends ConnectedStreamChannel> createStreamServer(SocketAddress bindAddress,
                                                                             ChannelListener<? super AcceptingChannel<ConnectedStreamChannel>> 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

createTcpServer

protected AcceptingChannel<? extends ConnectedStreamChannel> createTcpServer(InetSocketAddress bindAddress,
                                                                             ChannelListener<? super AcceptingChannel<ConnectedStreamChannel>> 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

createLocalStreamServer

protected AcceptingChannel<? extends ConnectedStreamChannel> createLocalStreamServer(LocalSocketAddress bindAddress,
                                                                                     ChannelListener<? super AcceptingChannel<ConnectedStreamChannel>> 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

public IoFuture<ConnectedStreamChannel> connectStream(SocketAddress destination,
                                                      ChannelListener<? super ConnectedStreamChannel> openListener,
                                                      OptionMap optionMap)
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

public IoFuture<ConnectedStreamChannel> connectStream(SocketAddress destination,
                                                      ChannelListener<? super ConnectedStreamChannel> openListener,
                                                      ChannelListener<? super BoundChannel> bindListener,
                                                      OptionMap optionMap)
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

public IoFuture<ConnectedStreamChannel> connectStream(SocketAddress bindAddress,
                                                      SocketAddress destination,
                                                      ChannelListener<? super ConnectedStreamChannel> openListener,
                                                      ChannelListener<? super BoundChannel> bindListener,
                                                      OptionMap optionMap)
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

connectTcpStream

protected IoFuture<ConnectedStreamChannel> connectTcpStream(InetSocketAddress bindAddress,
                                                            InetSocketAddress destinationAddress,
                                                            ChannelListener<? super ConnectedStreamChannel> openListener,
                                                            ChannelListener<? super BoundChannel> bindListener,
                                                            OptionMap optionMap)
Implementation helper method to connect to a TCP server.

Parameters:
bindAddress - the bind address
destinationAddress - 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 @return the future result of this operation
Returns:
the future result of this operation

connectLocalStream

protected IoFuture<ConnectedStreamChannel> connectLocalStream(LocalSocketAddress bindAddress,
                                                              LocalSocketAddress destinationAddress,
                                                              ChannelListener<? super ConnectedStreamChannel> openListener,
                                                              ChannelListener<? super BoundChannel> bindListener,
                                                              OptionMap optionMap)
Implementation helper method to connect to a local (UNIX domain) server.

Parameters:
bindAddress - the bind address
destinationAddress - 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

public IoFuture<ConnectedStreamChannel> acceptStream(SocketAddress destination,
                                                     ChannelListener<? super ConnectedStreamChannel> openListener,
                                                     ChannelListener<? super BoundChannel> bindListener,
                                                     OptionMap optionMap)
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

acceptLocalStream

protected IoFuture<ConnectedStreamChannel> acceptLocalStream(LocalSocketAddress destination,
                                                             ChannelListener<? super ConnectedStreamChannel> openListener,
                                                             ChannelListener<? super BoundChannel> bindListener,
                                                             OptionMap optionMap)
Implementation helper method to accept a local (UNIX domain) stream connection.

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

acceptTcpStream

protected IoFuture<ConnectedStreamChannel> acceptTcpStream(InetSocketAddress destination,
                                                           ChannelListener<? super ConnectedStreamChannel> openListener,
                                                           ChannelListener<? super BoundChannel> bindListener,
                                                           OptionMap optionMap)
Implementation helper method to accept a TCP connection.

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

public IoFuture<ConnectedMessageChannel> connectDatagram(SocketAddress destination,
                                                         ChannelListener<? super ConnectedMessageChannel> openListener,
                                                         ChannelListener<? super BoundChannel> bindListener,
                                                         OptionMap optionMap)
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

connectDatagram

public IoFuture<ConnectedMessageChannel> connectDatagram(SocketAddress bindAddress,
                                                         SocketAddress destination,
                                                         ChannelListener<? super ConnectedMessageChannel> openListener,
                                                         ChannelListener<? super BoundChannel> bindListener,
                                                         OptionMap optionMap)
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

connectUdpDatagram

protected IoFuture<ConnectedMessageChannel> connectUdpDatagram(InetSocketAddress bindAddress,
                                                               InetSocketAddress destination,
                                                               ChannelListener<? super ConnectedMessageChannel> openListener,
                                                               ChannelListener<? super BoundChannel> bindListener,
                                                               OptionMap optionMap)
Implementation helper method to connect to a UDP server.

Parameters:
bindAddress - the bind address
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

connectLocalDatagram

protected IoFuture<ConnectedMessageChannel> connectLocalDatagram(LocalSocketAddress bindAddress,
                                                                 LocalSocketAddress destination,
                                                                 ChannelListener<? super ConnectedMessageChannel> openListener,
                                                                 ChannelListener<? super BoundChannel> bindListener,
                                                                 OptionMap optionMap)
Implementation helper method to connect to a local (UNIX domain) datagram server.

Parameters:
bindAddress - the bind address
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

acceptDatagram

public IoFuture<ConnectedMessageChannel> acceptDatagram(SocketAddress destination,
                                                        ChannelListener<? super ConnectedMessageChannel> openListener,
                                                        ChannelListener<? super BoundChannel> bindListener,
                                                        OptionMap optionMap)
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

acceptLocalDatagram

protected IoFuture<ConnectedMessageChannel> acceptLocalDatagram(LocalSocketAddress destination,
                                                                ChannelListener<? super ConnectedMessageChannel> openListener,
                                                                ChannelListener<? super BoundChannel> bindListener,
                                                                OptionMap optionMap)
Implementation helper method to accept a local (UNIX domain) datagram connection.

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

public void createPipe(ChannelListener<? super StreamChannel> leftOpenListener,
                       ChannelListener<? super StreamChannel> rightOpenListener,
                       OptionMap optionMap)
                throws IOException
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

public void createOneWayPipe(ChannelListener<? super StreamSourceChannel> sourceListener,
                             ChannelListener<? super StreamSinkChannel> sinkListener,
                             OptionMap optionMap)
                      throws IOException
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

shutdown

public abstract void shutdown()
Specified by:
shutdown in interface ExecutorService

shutdownNow

public abstract List<Runnable> shutdownNow()
Specified by:
shutdownNow in interface ExecutorService

isShutdown

public abstract boolean isShutdown()
Specified by:
isShutdown in interface ExecutorService

isTerminated

public abstract boolean isTerminated()
Specified by:
isTerminated in interface ExecutorService

awaitTermination

public abstract boolean awaitTermination(long timeout,
                                         TimeUnit unit)
                                  throws InterruptedException
Specified by:
awaitTermination in interface ExecutorService
Throws:
InterruptedException

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.


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

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

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

XNIO API 3.0.7.GA

Copyright © 2010 JBoss, a division of Red Hat, Inc.