Class ReceiverBase
- java.lang.Object
-
- org.apache.catalina.tribes.transport.ReceiverBase
-
- All Implemented Interfaces:
ChannelReceiver,Heartbeat,ListenCallback,RxTaskPool.TaskCreator
- Direct Known Subclasses:
NioReceiver
public abstract class ReceiverBase extends java.lang.Object implements ChannelReceiver, ListenCallback, RxTaskPool.TaskCreator
Base implementation of a channel receiver that handles TCP connections and provides configuration for socket options, thread pools, and message handling.
-
-
Field Summary
Fields Modifier and Type Field Description static intOPTION_DIRECT_BUFFEROption flag to use direct (off-heap) buffers for data transfer.protected static StringManagersmString manager for localized messages in this package.-
Fields inherited from interface org.apache.catalina.tribes.ChannelReceiver
MAX_UDP_SIZE
-
-
Constructor Summary
Constructors Constructor Description ReceiverBase()Default constructor for ReceiverBase.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidbind(java.net.ServerSocket socket, int portstart, int retries)Attempts to bind using the provided port and if that fails attempts to bind to each of the ports from portstart to (portstart + retries -1) until either there are no more ports or the bind is successful.protected intbindUdp(java.net.DatagramSocket socket, int portstart, int retries)Same as bind() except it does it for the UDP portbooleandoListen()Returns whether the receiver is in listen mode.intgetActiveCount()Return the current number of threads that are in use.java.lang.StringgetAddress()Returns the bind address as a string.intgetAutoBind()Returns the number of attempts to find an available port for auto-binding.java.net.InetAddressgetBind()Returns the bind address.ChannelgetChannel()Return the channel that is related to this ChannelReceiverlonggetCompletedTaskCount()Return the total number of tasks that have completed execution by the pool.booleangetDirect()Returns whether direct (off-heap) buffers are used.java.util.concurrent.ExecutorServicegetExecutor()Returns the executor service used by this receiver.java.lang.StringgetHost()Returns the host address for this receiver.MessageListenergetListener()Returns the message listener for this receiver.longgetMaxIdleTime()Returns the maximum idle time for threads in the executor pool.intgetMaxTasks()Returns the maximum number of tasks in the pool.intgetMaxThreads()Returns the maximum number of threads in the executor pool.MessageListenergetMessageListener()Returns the message listener for this receiver.intgetMinTasks()Returns the minimum number of tasks in the pool.intgetMinThreads()Returns the minimum number of threads in the executor pool.booleangetOoBInline()Returns whether OOBINLINE (out-of-band data) is enabled.intgetPoolSize()Return the current number of threads that are managed by the pool.intgetPort()Returns the port on which this receiver is listening.intgetRxBufSize()Returns the size of the receive buffer for TCP connections.intgetSecurePort()Returns the secure port number.longgetSelectorTimeout()Returns the TCP selector timeout in milliseconds.booleangetSoKeepAlive()Returns whether SO_KEEPALIVE is enabled.booleangetSoLingerOn()Returns whether SO_LINGER is enabled.intgetSoLingerTime()Returns the SO_LINGER timeout value in seconds.booleangetSoReuseAddress()Returns whether SO_REUSEADDR is enabled.intgetSoTrafficClass()Returns the IP traffic class (TOS) value for sockets.longgetTaskCount()Return the total number of tasks that have ever been scheduled for execution by the pool.RxTaskPoolgetTaskPool()Returns the task pool used by this receiver.booleangetTcpNoDelay()Returns whether TCP_NODELAY is enabled.intgetTimeout()Returns the socket timeout in milliseconds.intgetTxBufSize()Returns the size of the transmit buffer for TCP connections.intgetUdpPort()Returns the UDP port number.intgetUdpRxBufSize()Returns the size of the receive buffer for UDP connections.intgetUdpTxBufSize()Returns the size of the transmit buffer for UDP connections.booleangetUseBufferPool()Returns whether a buffer pool is used for message handling.intgetWorkerThreadOptions()Returns the options flags for worker threads.voidheartbeat()Performs a heartbeat operation.booleanisDaemon()Returns whether the worker threads are daemon threads.booleanisListening()Returns whether the receiver is currently listening for connections.voidmessageDataReceived(ChannelMessage data)Receives a channel message and forwards it to the listener if the listener accepts it.voidsetAddress(java.lang.String host)Sets the bind address for this receiver.voidsetAutoBind(int autoBind)Sets the number of attempts to find an available port for auto-binding.voidsetBind(java.net.InetAddress bind)Sets the bind address.voidsetChannel(Channel channel)Set the channel that is related to this ChannelReceivervoidsetDaemon(boolean daemon)Sets whether the worker threads should be daemon threads.voidsetDirect(boolean direct)Sets whether direct (off-heap) buffers should be used.voidsetExecutor(java.util.concurrent.ExecutorService executor)Sets the executor service for this receiver.voidsetHost(java.lang.String host)Sets the host address for this receiver.voidsetListen(boolean doListen)Sets whether the receiver should listen for incoming connections.voidsetListener(MessageListener listener)Sets the message listener for this receiver.voidsetMaxIdleTime(long maxIdleTime)Sets the maximum idle time for threads in the executor pool.voidsetMaxTasks(int maxTasks)Sets the maximum number of tasks in the pool.voidsetMaxThreads(int maxThreads)Sets the maximum number of threads in the executor pool.voidsetMessageListener(MessageListener listener)Sets the message listener for this receiver.voidsetMinTasks(int minTasks)Sets the minimum number of tasks in the pool.voidsetMinThreads(int minThreads)Sets the minimum number of threads in the executor pool.voidsetOoBInline(boolean ooBInline)Sets whether OOBINLINE (out-of-band data) should be enabled.voidsetPool(RxTaskPool pool)Sets the task pool for this receiver.voidsetPort(int port)Sets the port on which this receiver listens.voidsetRxBufSize(int rxBufSize)Sets the size of the receive buffer for TCP connections.voidsetSecurePort(int securePort)Sets the secure port number.voidsetSelectorTimeout(long selTimeout)Sets the TCP selector timeout in milliseconds.voidsetSoKeepAlive(boolean soKeepAlive)Sets whether SO_KEEPALIVE should be enabled.voidsetSoLingerOn(boolean soLingerOn)Sets whether SO_LINGER should be enabled.voidsetSoLingerTime(int soLingerTime)Sets the SO_LINGER timeout value in seconds.voidsetSoReuseAddress(boolean soReuseAddress)Sets whether SO_REUSEADDR should be enabled.voidsetSoTrafficClass(int soTrafficClass)Sets the IP traffic class (TOS) value for sockets.voidsetTcpNoDelay(boolean tcpNoDelay)Sets whether TCP_NODELAY should be enabled.voidsetTimeout(int timeout)Sets the socket timeout in milliseconds.voidsetTxBufSize(int txBufSize)Sets the size of the transmit buffer for TCP connections.voidsetUdpPort(int udpPort)Sets the UDP port number.voidsetUdpRxBufSize(int udpRxBufSize)Sets the size of the receive buffer for UDP connections.voidsetUdpTxBufSize(int udpTxBufSize)Sets the size of the transmit buffer for UDP connections.voidsetUseBufferPool(boolean useBufferPool)Sets whether a buffer pool should be used for message handling.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.-
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.RxTaskPool.TaskCreator
createRxTask
-
-
-
-
Field Detail
-
OPTION_DIRECT_BUFFER
public static final int OPTION_DIRECT_BUFFER
Option flag to use direct (off-heap) buffers for data transfer.- See Also:
- Constant Field Values
-
sm
protected static final StringManager sm
String manager for localized messages in this package.
-
-
Method Detail
-
start
public void start() throws java.io.IOExceptionStarts the receiver by creating the executor service and registering with JMX.- Specified by:
startin interfaceChannelReceiver- Throws:
java.io.IOException- if an I/O error occurs during startup
-
stop
public void stop()
Stops the receiver by shutting down the executor and unregistering from JMX.- Specified by:
stopin interfaceChannelReceiver
-
getMessageListener
public MessageListener getMessageListener()
Returns the message listener for this receiver.- Specified by:
getMessageListenerin interfaceChannelReceiver- Returns:
- the message listener
- See Also:
MessageListener
-
getPort
public int getPort()
Returns the port on which this receiver is listening.- Specified by:
getPortin interfaceChannelReceiver- Returns:
- the port number
-
getRxBufSize
public int getRxBufSize()
Returns the size of the receive buffer for TCP connections.- Returns:
- the receive buffer size
-
getTxBufSize
public int getTxBufSize()
Returns the size of the transmit buffer for TCP connections.- Returns:
- the transmit buffer size
-
setMessageListener
public void setMessageListener(MessageListener listener)
Sets the message listener for this receiver.- Specified by:
setMessageListenerin interfaceChannelReceiver- Parameters:
listener- the message listener to set- See Also:
MessageListener
-
setRxBufSize
public void setRxBufSize(int rxBufSize)
Sets the size of the receive buffer for TCP connections.- Parameters:
rxBufSize- the receive buffer size to set
-
setTxBufSize
public void setTxBufSize(int txBufSize)
Sets the size of the transmit buffer for TCP connections.- Parameters:
txBufSize- the transmit buffer size to set
-
getBind
public java.net.InetAddress getBind()
Returns the bind address.- Returns:
- the bind address
-
bind
protected void bind(java.net.ServerSocket socket, int portstart, int retries) throws java.io.IOExceptionAttempts to bind using the provided port and if that fails attempts to bind to each of the ports from portstart to (portstart + retries -1) until either there are no more ports or the bind is successful. The address to bind to is obtained via a call togetBind().- Parameters:
socket- The socket to bindportstart- Starting port for bind attemptsretries- Number of times to attempt to bind (port incremented between attempts)- Throws:
java.io.IOException- Socket bind error
-
bindUdp
protected int bindUdp(java.net.DatagramSocket socket, int portstart, int retries) throws java.io.IOExceptionSame as bind() except it does it for the UDP port- Parameters:
socket- The socket to bindportstart- Starting port for bind attemptsretries- Number of times to attempt to bind (port incremented between attempts)- Returns:
- int The retry count
- Throws:
java.io.IOException- Socket bind error
-
messageDataReceived
public void messageDataReceived(ChannelMessage data)
Receives a channel message and forwards it to the listener if the listener accepts it.- Specified by:
messageDataReceivedin interfaceListenCallback- Parameters:
data- the channel message received
-
getWorkerThreadOptions
public int getWorkerThreadOptions()
Returns the options flags for worker threads.- Returns:
- the worker thread options
-
setBind
public void setBind(java.net.InetAddress bind)
Sets the bind address.- Parameters:
bind- the bind address to set
-
getDirect
public boolean getDirect()
Returns whether direct (off-heap) buffers are used.- Returns:
- true if direct buffers are used
-
setDirect
public void setDirect(boolean direct)
Sets whether direct (off-heap) buffers should be used.- Parameters:
direct- true to use direct buffers
-
getAddress
public java.lang.String getAddress()
Returns the bind address as a string.- Returns:
- the bind address
-
getHost
public java.lang.String getHost()
Returns the host address for this receiver.- Specified by:
getHostin interfaceChannelReceiver- Returns:
- the host address
-
getSelectorTimeout
public long getSelectorTimeout()
Returns the TCP selector timeout in milliseconds.- Returns:
- the selector timeout
-
doListen
public boolean doListen()
Returns whether the receiver is in listen mode.- Returns:
- true if the receiver is listening
-
getListener
public MessageListener getListener()
Returns the message listener for this receiver.- Returns:
- the message listener
-
getTaskPool
public RxTaskPool getTaskPool()
Returns the task pool used by this receiver.- Returns:
- the task pool
-
getAutoBind
public int getAutoBind()
Returns the number of attempts to find an available port for auto-binding.- Returns:
- the auto-bind retry count
-
getMaxThreads
public int getMaxThreads()
Returns the maximum number of threads in the executor pool.- Returns:
- the maximum thread count
-
getMinThreads
public int getMinThreads()
Returns the minimum number of threads in the executor pool.- Returns:
- the minimum thread count
-
getTcpNoDelay
public boolean getTcpNoDelay()
Returns whether TCP_NODELAY is enabled.- Returns:
- true if TCP_NODELAY is enabled
-
getSoKeepAlive
public boolean getSoKeepAlive()
Returns whether SO_KEEPALIVE is enabled.- Returns:
- true if SO_KEEPALIVE is enabled
-
getOoBInline
public boolean getOoBInline()
Returns whether OOBINLINE (out-of-band data) is enabled.- Returns:
- true if OOBINLINE is enabled
-
getSoLingerOn
public boolean getSoLingerOn()
Returns whether SO_LINGER is enabled.- Returns:
- true if SO_LINGER is enabled
-
getSoLingerTime
public int getSoLingerTime()
Returns the SO_LINGER timeout value in seconds.- Returns:
- the SO_LINGER timeout
-
getSoReuseAddress
public boolean getSoReuseAddress()
Returns whether SO_REUSEADDR is enabled.- Returns:
- true if SO_REUSEADDR is enabled
-
getSoTrafficClass
public int getSoTrafficClass()
Returns the IP traffic class (TOS) value for sockets.- Returns:
- the traffic class value
-
getTimeout
public int getTimeout()
Returns the socket timeout in milliseconds.- Returns:
- the socket timeout
-
getUseBufferPool
public boolean getUseBufferPool()
Returns whether a buffer pool is used for message handling.- Returns:
- true if a buffer pool is used
-
getSecurePort
public int getSecurePort()
Returns the secure port number.- Specified by:
getSecurePortin interfaceChannelReceiver- Returns:
- the secure port number, or -1 if not configured
-
getMinTasks
public int getMinTasks()
Returns the minimum number of tasks in the pool.- Returns:
- the minimum task count
-
getMaxTasks
public int getMaxTasks()
Returns the maximum number of tasks in the pool.- Returns:
- the maximum task count
-
getExecutor
public java.util.concurrent.ExecutorService getExecutor()
Returns the executor service used by this receiver.- Returns:
- the executor service
-
isListening
public boolean isListening()
Returns whether the receiver is currently listening for connections.- Returns:
- true if the receiver is listening
-
setSelectorTimeout
public void setSelectorTimeout(long selTimeout)
Sets the TCP selector timeout in milliseconds.- Parameters:
selTimeout- the selector timeout in milliseconds
-
setListen
public void setListen(boolean doListen)
Sets whether the receiver should listen for incoming connections.- Parameters:
doListen- true to enable listening
-
setAddress
public void setAddress(java.lang.String host)
Sets the bind address for this receiver.- Parameters:
host- the host address to bind to
-
setHost
public void setHost(java.lang.String host)
Sets the host address for this receiver.- Parameters:
host- the host address to set
-
setListener
public void setListener(MessageListener listener)
Sets the message listener for this receiver.- Parameters:
listener- the message listener to set
-
setPool
public void setPool(RxTaskPool pool)
Sets the task pool for this receiver.- Parameters:
pool- the task pool to set
-
setPort
public void setPort(int port)
Sets the port on which this receiver listens.- Parameters:
port- the port number to set
-
setAutoBind
public void setAutoBind(int autoBind)
Sets the number of attempts to find an available port for auto-binding.- Parameters:
autoBind- the number of retry attempts
-
setMaxThreads
public void setMaxThreads(int maxThreads)
Sets the maximum number of threads in the executor pool.- Parameters:
maxThreads- the maximum thread count
-
setMinThreads
public void setMinThreads(int minThreads)
Sets the minimum number of threads in the executor pool.- Parameters:
minThreads- the minimum thread count
-
setTcpNoDelay
public void setTcpNoDelay(boolean tcpNoDelay)
Sets whether TCP_NODELAY should be enabled.- Parameters:
tcpNoDelay- true to enable TCP_NODELAY
-
setSoKeepAlive
public void setSoKeepAlive(boolean soKeepAlive)
Sets whether SO_KEEPALIVE should be enabled.- Parameters:
soKeepAlive- true to enable SO_KEEPALIVE
-
setOoBInline
public void setOoBInline(boolean ooBInline)
Sets whether OOBINLINE (out-of-band data) should be enabled.- Parameters:
ooBInline- true to enable OOBINLINE
-
setSoLingerOn
public void setSoLingerOn(boolean soLingerOn)
Sets whether SO_LINGER should be enabled.- Parameters:
soLingerOn- true to enable SO_LINGER
-
setSoLingerTime
public void setSoLingerTime(int soLingerTime)
Sets the SO_LINGER timeout value in seconds.- Parameters:
soLingerTime- the SO_LINGER timeout in seconds
-
setSoReuseAddress
public void setSoReuseAddress(boolean soReuseAddress)
Sets whether SO_REUSEADDR should be enabled.- Parameters:
soReuseAddress- true to enable SO_REUSEADDR
-
setSoTrafficClass
public void setSoTrafficClass(int soTrafficClass)
Sets the IP traffic class (TOS) value for sockets.- Parameters:
soTrafficClass- the traffic class value
-
setTimeout
public void setTimeout(int timeout)
Sets the socket timeout in milliseconds.- Parameters:
timeout- the socket timeout in milliseconds
-
setUseBufferPool
public void setUseBufferPool(boolean useBufferPool)
Sets whether a buffer pool should be used for message handling.- Parameters:
useBufferPool- true to use a buffer pool
-
setSecurePort
public void setSecurePort(int securePort)
Sets the secure port number.- Parameters:
securePort- the secure port number
-
setMinTasks
public void setMinTasks(int minTasks)
Sets the minimum number of tasks in the pool.- Parameters:
minTasks- the minimum task count
-
setMaxTasks
public void setMaxTasks(int maxTasks)
Sets the maximum number of tasks in the pool.- Parameters:
maxTasks- the maximum task count
-
setExecutor
public void setExecutor(java.util.concurrent.ExecutorService executor)
Sets the executor service for this receiver.- Parameters:
executor- the executor service to set
-
heartbeat
public void heartbeat()
Performs a heartbeat operation. No-op in this base implementation.
-
getUdpPort
public int getUdpPort()
Returns the UDP port number.- Specified by:
getUdpPortin interfaceChannelReceiver- Returns:
- the UDP port number, or -1 if not configured
-
setUdpPort
public void setUdpPort(int udpPort)
Sets the UDP port number.- Parameters:
udpPort- the UDP port number
-
getUdpRxBufSize
public int getUdpRxBufSize()
Returns the size of the receive buffer for UDP connections.- Returns:
- the UDP receive buffer size
-
setUdpRxBufSize
public void setUdpRxBufSize(int udpRxBufSize)
Sets the size of the receive buffer for UDP connections.- Parameters:
udpRxBufSize- the UDP receive buffer size
-
getUdpTxBufSize
public int getUdpTxBufSize()
Returns the size of the transmit buffer for UDP connections.- Returns:
- the UDP transmit buffer size
-
setUdpTxBufSize
public void setUdpTxBufSize(int udpTxBufSize)
Sets the size of the transmit buffer for UDP connections.- Parameters:
udpTxBufSize- the UDP transmit buffer size
-
getChannel
public Channel getChannel()
Description copied from interface:ChannelReceiverReturn the channel that is related to this ChannelReceiver- Specified by:
getChannelin interfaceChannelReceiver- Returns:
- Channel
-
setChannel
public void setChannel(Channel channel)
Description copied from interface:ChannelReceiverSet the channel that is related to this ChannelReceiver- Specified by:
setChannelin interfaceChannelReceiver- Parameters:
channel- The channel
-
getPoolSize
public int getPoolSize()
Return the current number of threads that are managed by the pool.- Returns:
- the current number of threads that are managed by the pool
-
getActiveCount
public int getActiveCount()
Return the current number of threads that are in use.- Returns:
- the current number of threads that are in use
-
getTaskCount
public long getTaskCount()
Return the total number of tasks that have ever been scheduled for execution by the pool.- Returns:
- the total number of tasks that have ever been scheduled for execution by the pool
-
getCompletedTaskCount
public long getCompletedTaskCount()
Return the total number of tasks that have completed execution by the pool.- Returns:
- the total number of tasks that have completed execution by the pool
-
isDaemon
public boolean isDaemon()
Returns whether the worker threads are daemon threads.- Returns:
- true if worker threads are daemon threads
-
getMaxIdleTime
public long getMaxIdleTime()
Returns the maximum idle time for threads in the executor pool.- Returns:
- the maximum idle time in milliseconds
-
setDaemon
public void setDaemon(boolean daemon)
Sets whether the worker threads should be daemon threads.- Parameters:
daemon- true to use daemon threads
-
setMaxIdleTime
public void setMaxIdleTime(long maxIdleTime)
Sets the maximum idle time for threads in the executor pool.- Parameters:
maxIdleTime- the maximum idle time in milliseconds
-
-