Mina2.0框架源码剖析(八)

本文解析了AbstractPollingIoConnector抽象类,该类用于客户端连接的轮询策略。通过详细解释其核心成员变量和方法,包括连接队列、取消连接队列、连接处理流程及Worker线程的工作原理。

这篇来看看AbstractPollingIoConnector抽象类,它用于用于实现客户端连接的轮询策略。处理逻辑基本上和上一篇文章说的AbstractPollingIoAcceptor类似,它继承自AbstractIoConnector,两个泛型参数分别是所处理的会话和客户端socket连接。底层的sockets会被不断检测,并当有任何一个socket需要被处理时就会被唤醒去处理。这个类封装了客户端socketbind,connectdispose等动作,其成员变量Executor用于发起连接请求,另一个AbstractPollingIoProcessor用于处理已经连接客户端的I/O操作请求,如读写和关闭连接。

其最重要的几个成员变量是:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> private final Queue < ConnectionRequest > connectQueue = new ConcurrentLinkedQueue < ConnectionRequest > (); // 连接队列
private final Queue < ConnectionRequest > cancelQueue = new ConcurrentLinkedQueue < ConnectionRequest > (); // 取消连接队列

先来看看当服务端调用connect后的处理过程:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> protected final ConnectFutureconnect0(
SocketAddressremoteAddress,SocketAddresslocalAddress,
IoSessionInitializer
<? extends ConnectFuture > sessionInitializer){
Hhandle
= null ;
boolean success = false ;
try {
handle
= newHandle(localAddress);
if (connect(handle,remoteAddress)){ // 若已经连接服务器成功
ConnectFuturefuture = new DefaultConnectFuture();
Tsession
= newSession(processor,handle); // 创建新会话
finishSessionInitialization(session,future,sessionInitializer); // 结束会话初始化
session.getProcessor().add(session); // 将剩下的处理交给IoProcessor
success = true ;
return future;
}
success
= true ;
}
catch (Exceptione){
return DefaultConnectFuture.newFailedFuture(e);
}
finally {
if ( ! success && handle != null ){
try {
close(handle);
}
catch (Exceptione){
ExceptionMonitor.getInstance().exceptionCaught(e);
}
}
}
ConnectionRequestrequest
= new ConnectionRequest(handle,sessionInitializer);
connectQueue.add(request);
// 连接请求加入连接队列中
startupWorker(); // 开启工作线程处理连接请求
wakeup(); // 中断select操作
return request;
}

真正的负责处理客户端请求的工作都是Worker线程完成的,

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> private class Worker implements Runnable{
public void run(){
int nHandles = 0 ;
while (selectable){
try {
int timeout = ( int )Math.min(getConnectTimeoutMillis(), 1000L ); // 等待超时时间
boolean selected = select(timeout); // 在超时时限内查看是否有可以被处理的选择键(状态
nHandles += registerNew(); // 取出连接队列队头的连接请求,将其注册一个用于连接的新的客户端socket,并把它加入连接轮询池中
if (selected){
nHandles
-= processSessions(selectedHandles()); // 处理连接请求
}
processTimedOutSessions(allHandles());
// 处理超时连接请求
nHandles -= cancelKeys();
if (nHandles == 0 ){
synchronized (lock){
if (connectQueue.isEmpty()){
worker
= null ;
break ;
}
}
}
}
catch (Throwablee){
ExceptionMonitor.getInstance().exceptionCaught(e);
try {
Thread.sleep(
1000 );
}
catch (InterruptedExceptione1){
ExceptionMonitor.getInstance().exceptionCaught(e1);
}
}
}
if (selectable && isDisposing()){
selectable
= false ;
try {
if (createdProcessor){
processor.dispose();
}
}
finally {
try {
synchronized (disposalLock){
if (isDisposing()){
destroy();
}
}
}
catch (Exceptione){
ExceptionMonitor.getInstance().exceptionCaught(e);
}
finally {
disposalFuture.setDone();
}
}
}
}
}
private int registerNew(){
int nHandles = 0 ;
for (;;){
ConnectionRequestreq
= connectQueue.poll(); // 取连接队列队头请求
if (req == null ){
break ;
}
Hhandle
= req.handle;
try {
register(handle,req);
// 注册一个用于连接的新的客户端socket,并把它加入连接轮询池中
nHandles ++ ;
}
catch (Exceptione){
req.setException(e);
try {
close(handle);
}
catch (Exceptione2){
ExceptionMonitor.getInstance().exceptionCaught(e2);
}
}
}
return nHandles;
}
private int processSessions(Iterator < H > handlers){ // 处理连接请求
int nHandles = 0 ;
while (handlers.hasNext()){
Hhandle
= handlers.next();
handlers.remove();
ConnectionRequestentry
= connectionRequest(handle);
boolean success = false ;
try {
if (finishConnect(handle)){ // 连接请求成功完成,创建一个新会话
Tsession = newSession(processor,handle);
finishSessionInitialization(session,entry,entry.getSessionInitializer());
// 结束会话初始化
session.getProcessor().add(session); // 将剩下的工作交给IoProcessor去处理
nHandles ++ ;
}
success
= true ;
}
catch (Throwablee){
entry.setException(e);
}
finally {
if ( ! success){ // 若连接失败,则将此连接请求放到取消连接队列中
cancelQueue.offer(entry);
}
}
}
return nHandles;
}
private void processTimedOutSessions(Iterator < H > handles){ // 处理超时的连接请求
long currentTime = System.currentTimeMillis(); // 当前时间

while (handles.hasNext()){
Hhandle
= handles.next();
ConnectionRequestentry
= connectionRequest(handle);
if (currentTime >= entry.deadline){ // 当前时间已经超出了连接请求的底限
entry.setException(
new ConnectException( " Connectiontimedout. " ));
cancelQueue.offer(entry);
// 将此连接请求放入取消连接队列中
}
}
}
private int cancelKeys(){ // 把取消队列中的连接请求给cancel掉
int nHandles = 0 ;
for (;;){
ConnectionRequestreq
= cancelQueue.poll();
if (req == null ){
break ;
}
Hhandle
= req.handle;
try {
close(handle);
// 关闭对应的客户端socket
} catch (Exceptione){
ExceptionMonitor.getInstance().exceptionCaught(e);
}
finally {
nHandles
++ ;
}
}
return nHandles;
}

作者:phinecos(洞庭散人)
出处:http://phinecos.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但请保留此段声明,并在文章页面明显位置给出原文连接。

基于可靠性评估序贯蒙特卡洛模拟法的配电网可靠性评估研究(Matlab代码实现)内容概要:本文围绕“基于可靠性评估序贯蒙特卡洛模拟法的配电网可靠性评估研究”,介绍了利用Matlab代码实现配电网可靠性的仿真分析方法。重点采用序贯蒙特卡洛模拟法对配电网进行长时间段的状态抽样与统计,通过模拟系统元件的故障与修复过程,评估配电网的关键可靠性指标,如系统停电频率、停电持续时间、负荷点可靠性等。该方法能够有效处理复杂网络结构与设备时序特性,提升评估精度,适用于含分布式电源、电动汽车等新型负荷接入的现代配电网。文中提供了完整的Matlab实现代码与案例分析,便于复现和扩展应用。; 适合人群:具备电力系统基础知识和Matlab编程能力的高校研究生、科研人员及电力行业技术人员,尤其适合从事配电网规划、运行与可靠性分析相关工作的人员; 使用场景及目标:①掌握序贯蒙特卡洛模拟法在电力系统可靠性评估中的基本原理与实现流程;②学习如何通过Matlab构建配电网仿真模型并进行状态转移模拟;③应用于含新能源接入的复杂配电网可靠性定量评估与优化设计; 阅读建议:建议结合文中提供的Matlab代码逐段调试运行,理解状态抽样、故障判断、修复逻辑及指标统计的具体实现方式,同时可扩展至不同网络结构或加入更多不确定性因素进行深化研究。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值