mysql连接是nio吗_Linux 下使用NIO长连接,内存泄漏问题

入口方法是:

private void writeMessage() {

if (this.socketClosed) {

return;

}

try {

SelectionKey key = getSelectionKey();

if (key != null && !key.isValid()) {

handleFailureToClose(new java.nio.channels.CancelledKeyException());

return;

}

if (key != null && (key.interestOps() & SelectionKey.OP_WRITE) == 0) {

boolean finished = doWrite();

if (!finished)

key.interestOps(key.interestOps() | SelectionKey.OP_WRITE);

}

} catch (CancelledKeyException ce) {

logger.error(ce);

handleFailureToClose(ce);

} catch (IOException e) {

logger.error(e);

handleFailureToClose(e);

}

}

对于一次不能写完数据的情况采用了注册写事件的方式。下面这段是selector对于事件的轮询处理代码:

private void flush() {

if (flushingClientConnections.isEmpty())

return;

do {

AbstractClientConnection clientConnection = flushingClientConnections

.poll();

if (clientConnection == null)

break;

try {

boolean flushedAll = flushNow(clientConnection);

if (!flushedAll) {

flushingClientConnections.add(clientConnection);

}

} catch (Exception e) {

logger.error("flush error : {}", e);

}

} while (!flushingClientConnections.isEmpty());

}

private boolean flushNow(AbstractClientConnection clientConnection) {

try {

// Clear OP_WRITE

setInterestedInWrite(clientConnection, false);

boolean finished = clientConnection.doWrite();

if (!finished) {

setInterestedInWrite(clientConnection, true);

return false;

}

} catch (Exception e) {

logger.error("flushNow error : {}", e);

return false;

}

return true;

}

protected void setInterestedInWrite(

AbstractClientConnection clientConnection, boolean isInterested)

throws Exception {

SelectionKey key = clientConnection.getSelectionKey();

if (key == null) {

return;

}

int oldInterestOps = key.interestOps();

int newInterestOps = oldInterestOps;

if (isInterested) {

newInterestOps |= SelectionKey.OP_WRITE;

} else {

newInterestOps &= ~SelectionKey.OP_WRITE;

}

if (oldInterestOps != newInterestOps) {

key.interestOps(newInterestOps);

}

}

注释Clear OP_WRITE的地方已经对于写操作完成的事件,反注册了写事件。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值