Methods Summary |
---|
public void | close()
enabled = false;
Selector s;
while ( (s = selectors.poll()) != null ) s.close();
spare.set(0);
active.set(0);
if ( SHARED && getSharedSelector()!=null ) {
getSharedSelector().close();
SHARED_SELECTOR = null;
}
|
public java.nio.channels.Selector | get()
if ( SHARED ) {
return getSharedSelector();
}
if ( (!enabled) || active.incrementAndGet() >= maxSelectors ) {
if ( enabled ) active.decrementAndGet();
return null;
}
Selector s = null;
try {
s = selectors.size()>0?selectors.poll():null;
if (s == null) s = Selector.open();
else spare.decrementAndGet();
}catch (NoSuchElementException x ) {
try {s = Selector.open();}catch (IOException iox){}
} finally {
if ( s == null ) active.decrementAndGet();//we were unable to find a selector
}
return s;
|
public int | getMaxSelectors()
return maxSelectors;
|
public int | getMaxSpareSelectors()
return maxSpareSelectors;
|
protected static java.nio.channels.Selector | getSharedSelector()
if (SHARED && SHARED_SELECTOR == null) {
synchronized ( NioSelectorPool.class ) {
if ( SHARED_SELECTOR == null ) {
SHARED_SELECTOR = Selector.open();
log.info("Using a shared selector for servlet write/read");
}
}
}
return SHARED_SELECTOR;
|
public boolean | isEnabled()
return enabled;
|
public void | open()
enabled = true;
getSharedSelector();
|
public void | put(java.nio.channels.Selector s)
if ( SHARED ) return;
if ( enabled ) active.decrementAndGet();
if ( enabled && (maxSpareSelectors==-1 || spare.get() < Math.min(maxSpareSelectors,maxSelectors)) ) {
spare.incrementAndGet();
selectors.offer(s);
}
else s.close();
|
public int | read(java.nio.ByteBuffer buf, NioChannel socket, java.nio.channels.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.
return read(buf,socket,selector,readTimeout,true);
|
public int | read(java.nio.ByteBuffer buf, NioChannel socket, java.nio.channels.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.
if ( SHARED && block) {
return NioBlockingSelector.read(buf,socket,readTimeout);
}
SelectionKey key = null;
int read = 0;
boolean timedout = false;
int keycount = 1; //assume we can write
long time = System.currentTimeMillis(); //start the timeout timer
try {
while ( (!timedout) ) {
int cnt = 0;
if ( keycount > 0 ) { //only read if we were registered for a read
cnt = socket.read(buf);
if (cnt == -1) throw new EOFException();
read += cnt;
if (cnt > 0) continue; //read some more
if (cnt==0 && (read>0 || (!block) ) ) break; //we are done reading
}
if ( selector != null ) {//perform a blocking read
//register OP_WRITE to the selector
if (key==null) key = socket.getIOChannel().register(selector, SelectionKey.OP_READ);
else key.interestOps(SelectionKey.OP_READ);
keycount = selector.select(readTimeout);
}
if (readTimeout > 0 && (selector == null || keycount == 0) ) timedout = (System.currentTimeMillis()-time)>=readTimeout;
}//while
if ( timedout ) throw new SocketTimeoutException();
} finally {
if (key != null) {
key.cancel();
if (selector != null) selector.selectNow();//removes the key from this selector
}
}
return read;
|
public void | setEnabled(boolean enabled)
this.enabled = enabled;
|
public void | setMaxSelectors(int maxSelectors)
this.maxSelectors = maxSelectors;
|
public void | setMaxSpareSelectors(int maxSpareSelectors)
this.maxSpareSelectors = maxSpareSelectors;
|
public int | write(java.nio.ByteBuffer buf, NioChannel socket, java.nio.channels.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.
return write(buf,socket,selector,writeTimeout,true);
|
public int | write(java.nio.ByteBuffer buf, NioChannel socket, java.nio.channels.Selector selector, long writeTimeout, boolean block)
if ( SHARED && block) {
return NioBlockingSelector.write(buf,socket,writeTimeout);
}
SelectionKey key = null;
int written = 0;
boolean timedout = false;
int keycount = 1; //assume we can write
long time = System.currentTimeMillis(); //start the timeout timer
if ( socket.getBufHandler().getWriteBuffer()!= buf ) {
socket.getBufHandler().getWriteBuffer().put(buf);
buf = socket.getBufHandler().getWriteBuffer();
}
try {
while ( (!timedout) && buf.hasRemaining() ) {
int cnt = 0;
if ( keycount > 0 ) { //only write if we were registered for a write
cnt = socket.write(buf); //write the data
if (cnt == -1) throw new EOFException();
written += cnt;
if (cnt > 0) {
time = System.currentTimeMillis(); //reset our timeout timer
continue; //we successfully wrote, try again without a selector
}
if (cnt==0 && (!block)) break; //don't block
}
if ( selector != null ) {
//register OP_WRITE to the selector
if (key==null) key = socket.getIOChannel().register(selector, SelectionKey.OP_WRITE);
else key.interestOps(SelectionKey.OP_WRITE);
keycount = selector.select(writeTimeout);
}
if (writeTimeout > 0 && (selector == null || keycount == 0) ) timedout = (System.currentTimeMillis()-time)>=writeTimeout;
}//while
if ( timedout ) throw new SocketTimeoutException();
} finally {
if (key != null) {
key.cancel();
if (selector != null) selector.selectNow();//removes the key from this selector
}
}
return written;
|