Class NioReceiver
- java.lang.Object
-
- org.apache.catalina.tribes.transport.ReceiverBase
-
- org.apache.catalina.tribes.transport.nio.NioReceiver
-
- All Implemented Interfaces:
java.lang.Runnable,ChannelReceiver,Heartbeat,ListenCallback,NioReceiverMBean,RxTaskPool.TaskCreator
public class NioReceiver extends ReceiverBase implements java.lang.Runnable, NioReceiverMBean
NIO-based receiver for cluster communication.
-
-
Field Summary
Fields Modifier and Type Field Description protected java.util.Deque<java.lang.Runnable>eventsQueue of events to be processed by the selector thread.protected longlastCheckTimestamp of the last socket timeout check.protected static StringManagersmThe string manager for this package.-
Fields inherited from class org.apache.catalina.tribes.transport.ReceiverBase
OPTION_DIRECT_BUFFER
-
Fields inherited from interface org.apache.catalina.tribes.ChannelReceiver
MAX_UDP_SIZE
-
-
Constructor Summary
Constructors Constructor Description NioReceiver()Default constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddEvent(java.lang.Runnable event)Adds a runnable event to the selector's event queue.protected voidbind()Binds the server socket and datagram channels to their respective ports.static voidcancelledKey(java.nio.channels.SelectionKey key)Handles a cancelled selection key by closing associated channels and cleaning up resources.AbstractRxTaskcreateRxTask()Creates a new receive task.voidevents()Processes all pending events in the event queue.protected voidlisten()Get data from channel and store in byte array send it to clusterprotected voidreadDataFromSocket(java.nio.channels.SelectionKey key)Sample data handler method for a channel with data ready to read.protected voidregisterChannel(java.nio.channels.Selector selector, java.nio.channels.SelectableChannel channel, int ops, java.lang.Object attach)Register the given channel with the given selector for the given operations of interestvoidrun()Start thread and listenprotected voidsocketTimeouts()Checks for socket timeouts and handles expired connections.voidstart()Starts the receiver by creating the executor service and registering with JMX.voidstop()Stops the receiver by shutting down the executor and unregistering from JMX.protected voidstopListening()Close Selector.-
Methods inherited from class org.apache.catalina.tribes.transport.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 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.apache.catalina.tribes.transport.nio.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 Detail
-
sm
protected static final StringManager sm
The string manager for this package.
-
events
protected final java.util.Deque<java.lang.Runnable> events
Queue of events to be processed by the selector thread.
-
lastCheck
protected long lastCheck
Timestamp of the last socket timeout check.
-
-
Method Detail
-
stop
public void stop()
Description copied from class:ReceiverBaseStops the receiver by shutting down the executor and unregistering from JMX.- Specified by:
stopin interfaceChannelReceiver- Overrides:
stopin classReceiverBase
-
start
public void start() throws java.io.IOExceptionDescription copied from class:ReceiverBaseStarts the receiver by creating the executor service and registering with JMX.- Specified by:
startin interfaceChannelReceiver- Overrides:
startin classReceiverBase- Throws:
java.io.IOException- if an I/O error occurs during startup
-
createRxTask
public AbstractRxTask createRxTask()
Description copied from interface:RxTaskPool.TaskCreatorCreates a new receive task.- Specified by:
createRxTaskin interfaceRxTaskPool.TaskCreator- Returns:
- a new receive task instance
-
bind
protected void bind() throws java.io.IOExceptionBinds the server socket and datagram channels to their respective ports.- Throws:
java.io.IOException- If binding fails
-
addEvent
public void addEvent(java.lang.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(java.nio.channels.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 java.lang.ExceptionGet data from channel and store in byte array send it to cluster- Throws:
java.io.IOException- IO errorjava.lang.Exception
-
stopListening
protected void stopListening()
Close Selector.- See Also:
ReceiverBase.stop()
-
registerChannel
protected void registerChannel(java.nio.channels.Selector selector, java.nio.channels.SelectableChannel channel, int ops, java.lang.Object attach) throws java.lang.ExceptionRegister the given channel with the given selector for the given operations of interest- Parameters:
selector- The selector to usechannel- The channelops- The operations to registerattach- Attachment object- Throws:
java.lang.Exception- IO error with channel
-
run
public void run()
Start thread and listen- Specified by:
runin interfacejava.lang.Runnable
-
readDataFromSocket
protected void readDataFromSocket(java.nio.channels.SelectionKey key) throws java.lang.ExceptionSample 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:
java.lang.Exception- IO error with channel
-
-