# Class NioReceiver

java.lang.Object
org.apache.catalina.tribes.transport.ReceiverBase
org.apache.catalina.tribes.transport.nio.NioReceiver

All Implemented Interfaces:
  `Runnable, ChannelReceiver, Heartbeat, ListenCallback, NioReceiverMBean, RxTaskPool.TaskCreator`

public class NioReceiver extends ReceiverBase implements Runnable, NioReceiverMBean
NIO-based receiver for cluster communication.

## Field Summary

Fields
Modifier and Type
Field
Description
`protected final Deque<Runnable>`
`events`
Queue of events to be processed by the selector thread.
`protected long`
`lastCheck`
Timestamp of the last socket timeout check.
`protected static final StringManager`
`sm`
The string manager for this package.

### Fields inherited from class ReceiverBase

`OPTION_DIRECT_BUFFER`

### Fields inherited from interface ChannelReceiver

`MAX_UDP_SIZE`

## Constructor Summary

Constructors
Constructor
Description
`NioReceiver()`
Default constructor.

## Method Summary

Modifier and Type
Method
Description
`void`
`addEvent(Runnable event)`
Adds a runnable event to the selector's event queue.
`protected void`
`bind()`
Binds the server socket and datagram channels to their respective ports.
`static void`
`cancelledKey(SelectionKey key)`
Handles a cancelled selection key by closing associated channels and cleaning up resources.
`AbstractRxTask`
`createRxTask()`
Creates a new receive task.
`void`
`events()`
Processes all pending events in the event queue.
`protected void`
`listen()`
Get data from channel and store in byte array send it to cluster
`protected void`
`readDataFromSocket(SelectionKey key)`
Sample data handler method for a channel with data ready to read.
`protected void`
`registerChannel(Selector selector, SelectableChannel channel, int ops, Object attach)`
Register the given channel with the given selector for the given operations of interest
`void`
`run()`
Start thread and listen
`protected void`
`socketTimeouts()`
Checks for socket timeouts and handles expired connections.
`void`
`start()`
Starts the receiver by creating the executor service and registering with JMX.
`void`
`stop()`
Stops the receiver by shutting down the executor and unregistering from JMX.
`protected void`
`stopListening()`
Close Selector.

### Methods inherited from class ReceiverBase

`bind, bindUdp, doListen, getActiveCount, getAddress, getAutoBind, getBind, getChannel, getCompletedTaskCount, getDirect, getExecutor, getHost, getListener, getMaxIdleTime, getMaxTasks, getMaxThreads, getMessageListener, getMinTasks, getMinThreads, getOoBInline, getPoolSize, getPort, getRxBufSize, getSecurePort, getSelectorTimeout, getSoKeepAlive, getSoLingerOn, getSoLingerTime, getSoReuseAddress, getSoTrafficClass, getTaskCount, getTaskPool, getTcpNoDelay, getTimeout, getTxBufSize, getUdpPort, getUdpRxBufSize, getUdpTxBufSize, getUseBufferPool, getWorkerThreadOptions, heartbeat, isDaemon, isListening, messageDataReceived, setAddress, setAutoBind, setBind, setChannel, setDaemon, setDirect, setExecutor, setHost, setListen, setListener, setMaxIdleTime, setMaxTasks, setMaxThreads, setMessageListener, setMinTasks, setMinThreads, setOoBInline, setPool, setPort, setRxBufSize, setSecurePort, setSelectorTimeout, setSoKeepAlive, setSoLingerOn, setSoLingerTime, setSoReuseAddress, setSoTrafficClass, setTcpNoDelay, setTimeout, setTxBufSize, setUdpPort, setUdpRxBufSize, setUdpTxBufSize, setUseBufferPool`

### Methods inherited from class Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Methods inherited from interface NioReceiverMBean

`getActiveCount, getAddress, getAutoBind, getCompletedTaskCount, getDirect, getMaxIdleTime, getMaxThreads, getMinThreads, getOoBInline, getPoolSize, getPort, getRxBufSize, getSecurePort, getSelectorTimeout, getSoKeepAlive, getSoLingerOn, getSoLingerTime, getSoReuseAddress, getTaskCount, getTcpNoDelay, getTimeout, getTxBufSize, getUdpPort, getUdpRxBufSize, getUdpTxBufSize, getUseBufferPool, isListening`

## Field Details

### sm

protected static final StringManager sm
The string manager for this package.

### events

protected final Deque<Runnable> events
Queue of events to be processed by the selector thread.

### lastCheck

protected long lastCheck
Timestamp of the last socket timeout check.

## Constructor Details

### NioReceiver

public NioReceiver()
Default constructor.

## Method Details

### stop

public void stop()
Description copied from class: `ReceiverBase`
Stops the receiver by shutting down the executor and unregistering from JMX.

Specified by:
  `stop` in interface `ChannelReceiver`
Overrides:
  `stop` in class `ReceiverBase`

### start

public void start() throws IOException
Description copied from class: `ReceiverBase`
Starts the receiver by creating the executor service and registering with JMX.

Specified by:
  `start` in interface `ChannelReceiver`
Overrides:
  `start` in class `ReceiverBase`
Throws:
  `IOException` - if an I/O error occurs during startup

### createRxTask

public AbstractRxTask createRxTask()
Description copied from interface: `RxTaskPool.TaskCreator`
Creates a new receive task.

Specified by:
  `createRxTask` in interface `RxTaskPool.TaskCreator`
Returns:
  a new receive task instance

### bind

protected void bind() throws IOException
Binds the server socket and datagram channels to their respective ports.

Throws:
  `IOException` - If binding fails

### addEvent

public void addEvent(Runnable event)
Adds a runnable event to the selector's event queue.

Parameters:
  `event` - The event to add

### events

public void events()
Processes all pending events in the event queue.

### cancelledKey

public static void cancelledKey(SelectionKey key)
Handles a cancelled selection key by closing associated channels and cleaning up resources.

Parameters:
  `key` - The cancelled selection key

### socketTimeouts

protected void socketTimeouts()
Checks for socket timeouts and handles expired connections.

### listen

protected void listen() throws Exception
Get data from channel and store in byte array send it to cluster

Throws:
  `IOException` - IO error
  `Exception`

### stopListening

protected void stopListening()
Close Selector.

See Also:
  - `ReceiverBase.stop()`

### registerChannel

protected void registerChannel(Selector selector, SelectableChannel channel, int ops, Object attach) throws Exception
Register the given channel with the given selector for the given operations of interest

Parameters:
  `selector` - The selector to use
  `channel` - The channel
  `ops` - The operations to register
  `attach` - Attachment object
Throws:
  `Exception` - IO error with channel

### run

public void run()
Start thread and listen

Specified by:
  `run` in interface `Runnable`

### readDataFromSocket

protected void readDataFromSocket(SelectionKey key) throws Exception
Sample data handler method for a channel with data ready to read.

Parameters:
  `key` - A SelectionKey object associated with a channel determined by the selector to be ready for reading. If the channel returns an EOF condition, it is closed here, which automatically invalidates the associated key. The selector will then de-register the channel on the next select call.
Throws:
  `Exception` - IO error with channel