org.apache.tomcat.util.net

Class NioSelectorPool

public class NioSelectorPool extends Object

Thread safe non blocking selector pool

Since: 6.0

Version: 1.0

Author: Filip Hanik

Field Summary
protected AtomicIntegeractive
protected NioBlockingSelectorblockingSelector
protected booleanenabled
protected static Loglog
protected intmaxSelectors
protected intmaxSpareSelectors
protected ConcurrentLinkedQueue<Selector>selectors
protected longsharedSelectorTimeout
protected AtomicIntegerspare
protected static booleanSHARED
protected SelectorSHARED_SELECTOR
protected static intthreadCount
Constructor Summary
NioSelectorPool()
Method Summary
voidclose()
Selectorget()
intgetMaxSelectors()
intgetMaxSpareSelectors()
ConcurrentLinkedQueuegetSelectors()
protected SelectorgetSharedSelector()
longgetSharedSelectorTimeout()
AtomicIntegergetSpare()
booleanisEnabled()
voidopen()
voidput(Selector s)
intread(ByteBuffer buf, NioChannel socket, Selector selector, long readTimeout)
Performs a blocking read using the bytebuffer for data to be read and a selector to block.
intread(ByteBuffer buf, NioChannel socket, Selector selector, long readTimeout, boolean block)
Performs a read using the bytebuffer for data to be read and a selector to register for events should you have the block=true.
voidsetEnabled(boolean enabled)
voidsetMaxSelectors(int maxSelectors)
voidsetMaxSpareSelectors(int maxSpareSelectors)
voidsetSharedSelectorTimeout(long sharedSelectorTimeout)
intwrite(ByteBuffer buf, NioChannel socket, Selector selector, long writeTimeout)
Performs a blocking write using the bytebuffer for data to be written and a selector to block.
intwrite(ByteBuffer buf, NioChannel socket, Selector selector, long writeTimeout, boolean block, MutableInteger lastWrite)

Field Detail

active

protected AtomicInteger active

blockingSelector

protected NioBlockingSelector blockingSelector

enabled

protected boolean enabled

log

protected static Log log

maxSelectors

protected int maxSelectors

maxSpareSelectors

protected int maxSpareSelectors

selectors

protected ConcurrentLinkedQueue<Selector> selectors

sharedSelectorTimeout

protected long sharedSelectorTimeout

spare

protected AtomicInteger spare

SHARED

protected static final boolean SHARED

SHARED_SELECTOR

protected Selector SHARED_SELECTOR

threadCount

protected static int threadCount

Constructor Detail

NioSelectorPool

public NioSelectorPool()

Method Detail

close

public void close()

get

public Selector get()

getMaxSelectors

public int getMaxSelectors()

getMaxSpareSelectors

public int getMaxSpareSelectors()

getSelectors

public ConcurrentLinkedQueue getSelectors()

getSharedSelector

protected Selector getSharedSelector()

getSharedSelectorTimeout

public long getSharedSelectorTimeout()

getSpare

public AtomicInteger getSpare()

isEnabled

public boolean isEnabled()

open

public void open()

put

public void put(Selector s)

read

public int read(ByteBuffer buf, NioChannel socket, Selector selector, long readTimeout)
Performs a blocking read using the bytebuffer for data to be read and a selector to block. If the selector parameter is null, then it will perform a busy read that could take up a lot of CPU cycles.

Parameters: buf ByteBuffer - the buffer containing the data, we will read as until we have read at least one byte or we timed out socket SocketChannel - the socket to write data to selector Selector - the selector to use for blocking, if null then a busy read will be initiated readTimeout long - the timeout for this read operation in milliseconds, -1 means no timeout

Returns: int - returns the number of bytes read

Throws: EOFException if read returns -1 SocketTimeoutException if the read times out IOException if an IO Exception occurs in the underlying socket logic

read

public int read(ByteBuffer buf, NioChannel socket, Selector selector, long readTimeout, boolean block)
Performs a read using the bytebuffer for data to be read and a selector to register for events should you have the block=true. If the selector parameter is null, then it will perform a busy read that could take up a lot of CPU cycles.

Parameters: buf ByteBuffer - the buffer containing the data, we will read as until we have read at least one byte or we timed out socket SocketChannel - the socket to write data to selector Selector - the selector to use for blocking, if null then a busy read will be initiated readTimeout long - the timeout for this read operation in milliseconds, -1 means no timeout block - true if you want to block until data becomes available or timeout time has been reached

Returns: int - returns the number of bytes read

Throws: EOFException if read returns -1 SocketTimeoutException if the read times out IOException if an IO Exception occurs in the underlying socket logic

setEnabled

public void setEnabled(boolean enabled)

setMaxSelectors

public void setMaxSelectors(int maxSelectors)

setMaxSpareSelectors

public void setMaxSpareSelectors(int maxSpareSelectors)

setSharedSelectorTimeout

public void setSharedSelectorTimeout(long sharedSelectorTimeout)

write

public int write(ByteBuffer buf, NioChannel socket, Selector selector, long writeTimeout)
Performs a blocking write using the bytebuffer for data to be written and a selector to block. If the selector parameter is null, then it will perform a busy write that could take up a lot of CPU cycles.

Parameters: buf ByteBuffer - the buffer containing the data, we will write as long as (buf.hasRemaining()==true) socket SocketChannel - the socket to write data to selector Selector - the selector to use for blocking, if null then a busy write will be initiated writeTimeout long - the timeout for this write operation in milliseconds, -1 means no timeout

Returns: int - returns the number of bytes written

Throws: EOFException if write returns -1 SocketTimeoutException if the write times out IOException if an IO Exception occurs in the underlying socket logic

write

public int write(ByteBuffer buf, NioChannel socket, Selector selector, long writeTimeout, boolean block, MutableInteger lastWrite)
Copyright © 2000-2011 Apache Software Foundation. All Rights Reserved.