本文整理匯總了Java中java.nio.channels.Selector.keys方法的典型用法代碼示例。如果您正苦於以下問題:Java Selector.keys方法的具體用法?Java Selector.keys怎麽用?Java Selector.keys使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.nio.channels.Selector
的用法示例。
在下文中一共展示了Selector.keys方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: socketTimeouts
import java.nio.channels.Selector; //導入方法依賴的package包/類
protected void socketTimeouts() {
long now = System.currentTimeMillis();
if ((now - lastCheck) < getSelectorTimeout())
return;
// timeout
Selector tmpsel = this.selector.get();
Set<SelectionKey> keys = (isListening() && tmpsel != null) ? tmpsel.keys() : null;
if (keys == null)
return;
for (Iterator<SelectionKey> iter = keys.iterator(); iter.hasNext();) {
SelectionKey key = iter.next();
try {
// if (key.interestOps() == SelectionKey.OP_READ) {
// //only timeout sockets that we are waiting for a read from
// ObjectReader ka = (ObjectReader) key.attachment();
// long delta = now - ka.getLastAccess();
// if (delta > (long) getTimeout()) {
// cancelledKey(key);
// }
// }
// else
if (key.interestOps() == 0) {
// check for keys that didn't make it in.
ObjectReader ka = (ObjectReader) key.attachment();
if (ka != null) {
long delta = now - ka.getLastAccess();
if (delta > getTimeout() && (!ka.isAccessed())) {
if (log.isWarnEnabled())
log.warn("Channel key is registered, but has had no interest ops for the last "
+ getTimeout() + " ms. (cancelled:" + ka.isCancelled() + "):" + key
+ " last access:" + new java.sql.Timestamp(ka.getLastAccess())
+ " Possible cause: all threads used, perform thread dump");
ka.setLastAccess(now);
// key.interestOps(SelectionKey.OP_READ);
} // end if
} else {
cancelledKey(key);
} // end if
} // end if
} catch (CancelledKeyException ckx) {
cancelledKey(key);
}
}
lastCheck = System.currentTimeMillis();
}
示例2: socketTimeouts
import java.nio.channels.Selector; //導入方法依賴的package包/類
protected void socketTimeouts() {
long now = System.currentTimeMillis();
if ( (now-lastCheck) < getSelectorTimeout() ) return;
//timeout
Selector tmpsel = this.selector.get();
Set<SelectionKey> keys = (isListening()&&tmpsel!=null)?tmpsel.keys():null;
if ( keys == null ) return;
for (Iterator<SelectionKey> iter = keys.iterator(); iter.hasNext();) {
SelectionKey key = iter.next();
try {
// if (key.interestOps() == SelectionKey.OP_READ) {
// //only timeout sockets that we are waiting for a read from
// ObjectReader ka = (ObjectReader) key.attachment();
// long delta = now - ka.getLastAccess();
// if (delta > (long) getTimeout()) {
// cancelledKey(key);
// }
// }
// else
if ( key.interestOps() == 0 ) {
//check for keys that didn't make it in.
ObjectReader ka = (ObjectReader) key.attachment();
if ( ka != null ) {
long delta = now - ka.getLastAccess();
if (delta > getTimeout() && (!ka.isAccessed())) {
if (log.isWarnEnabled())
log.warn("Channel key is registered, but has had no interest ops for the last "+getTimeout()+" ms. (cancelled:"+ka.isCancelled()+"):"+key+" last access:"+new java.sql.Timestamp(ka.getLastAccess())+" Possible cause: all threads used, perform thread dump");
ka.setLastAccess(now);
//key.interestOps(SelectionKey.OP_READ);
}//end if
} else {
cancelledKey(key);
}//end if
}//end if
}catch ( CancelledKeyException ckx ) {
cancelledKey(key);
}
}
lastCheck = System.currentTimeMillis();
}
示例3: handleTimeout
import java.nio.channels.Selector; //導入方法依賴的package包/類
private void handleTimeout(){
Selector tmpsel = selector;
Set keys = (stoped == false && tmpsel!=null)?tmpsel.keys():null;
if ( keys == null ) {
return;
}
Iterator it = keys.iterator();
long now = System.currentTimeMillis();
//cancel timeout and no interestOps key,close socket and channel
while(it.hasNext()){
SelectionKey key = (SelectionKey) it.next();
if(key.channel() instanceof ServerSocketChannel){
continue;
}
if(key.isValid() == false){
continue;
}
try{
PushTask task = (PushTask)key.attachment();
if(task == null){
cancelKey(key);
continue;
}
if(task.isWritePending() == false && now - task.getLastActive() > sockTimout){
cancelKey(key);
}
}catch(CancelledKeyException e){
cancelKey(key);
}
}
}
示例4: handleTimeout
import java.nio.channels.Selector; //導入方法依賴的package包/類
private void handleTimeout(){
Selector tmpsel = selector;
Set keys = (stoped == false && tmpsel!=null)?tmpsel.keys():null;
if ( keys == null ) {
return;
}
Iterator it = keys.iterator();
long now = System.currentTimeMillis();
//cancel timeout and no interestOps keys,close socket and channel
while(it.hasNext()){
SelectionKey key = (SelectionKey) it.next();
if(key.channel() instanceof ServerSocketChannel){
continue;
}
if(key.isValid() == false){
continue;
}
try{
MessengerTask task = (MessengerTask)key.attachment();
if(task == null){
cancelKey(key);
continue;
}
if(task.isWritePending() == false && now - task.getLastActive() > sockTimout){
cancelKey(key);
}
}catch(CancelledKeyException e){
cancelKey(key);
}
}
}
示例5: debugPrint
import java.nio.channels.Selector; //導入方法依賴的package包/類
void debugPrint(Selector selector) {
System.err.println("Selector: debugprint start");
Set<SelectionKey> keys = selector.keys();
for (SelectionKey key : keys) {
SelectableChannel c = key.channel();
int ops = key.interestOps();
System.err.printf("selector chan:%s ops:%d\n", c, ops);
}
System.err.println("Selector: debugprint end");
}