Mina 框架源码解析-NioSocketAcceptor启动过程
在HeartBeatServer 中,有一句启动Server端的代码
// 创建一个非阻塞的server端的Socket
acceptor = new NioSocketAcceptor();
下面分析这个 new NioSocketAcceptor的过程:
1,NioSocketAcceptor
类头声明
public final class NioSocketAcceptor
extends AbstractPollingIoAcceptor<NioSession, ServerSocketChannel>
implements SocketAcceptor {
继承了AbstractPollingIoAcceptor
类,实现SocketAcceptor
接口
2,看AbstractPollingIoAcceptor
类,
类头声明
public abstract class AbstractPollingIoAcceptor<S extends AbstractIoSession, H>
extends AbstractIoAcceptor {
继承了AbstractIoAcceptor
类,AbstractIoAcceptor
类说明请看第3点
在AbstractPollingIoAcceptor
类加载的时候初始化了这些
private final Queue<AcceptorOperationFuture> registerQueue = new ConcurrentLinkedQueue<AcceptorOperationFuture>();
private final Queue<AcceptorOperationFuture> cancelQueue = new ConcurrentLinkedQueue<AcceptorOperationFuture>();
private final Map<SocketAddress, H> boundHandles = Collections.synchronizedMap(new HashMap<SocketAddress, H>());
private final ServiceOperationFuture disposalFuture = new ServiceOperationFuture();
/** The thread responsible of accepting incoming requests */
private AtomicReference<Acceptor> acceptorRef = new AtomicReference<Acceptor>();
3,AbstractIoAcceptor
类头声明
public abstract class AbstractIoAcceptor
extends AbstractIoService implements IoAcceptor {
继承AbstractIoService
类,AbstractIoService类实现IoService接口
public abstract class AbstractIoService implements IoService {
AbstractIoAcceptor
类在加载时初始化这些
private final List<SocketAddress> defaultLocalAddresses =
new ArrayList<SocketAddress>();
private final List<SocketAddress> unmodifiableDefaultLocalAddresses =
Collections.unmodifiableList(defaultLocalAddresses);
private final Set<SocketAddress> boundAddresses =
new HashSet<SocketAddress>();
private boolean disconnectOnUnbind = true;
/**
* 当bind或unbind操作的时候需要获得的锁对象
* The lock object which is acquired while bind or unbind operation is performed.
* Acquire this lock in your property setters which shouldn't be changed while
* the service is bound.
*/
protected final Object bindLock = new Object();
4,SocketAcceptor
接口,AbstractPollingIoAcceptor
构造函数等先不看,那只不过是实现一种规范而已。
5,回到NioSocketAcceptor
类,进入构造方法
public NioSocketAcceptor() {
/** 使用默认的Socket配置
* 调用父类的构造器 ,将默认配置传入到父类中,第二个参数NioProcessor.class 是动态类加载,会放到线程池上运行
* 在父类AbstractPollingIoAcceptor的构造方法中调用init()接口,在子类中会调用init()接口的实现,因而初始化了selector
* protected AbstractPollingIoAcceptor(IoSessionConfig sessionConfig,
* Class<? extends IoProcessor<S>> processorClass) {
* this(sessionConfig, null, new SimpleIoProcessorPool<S>(processorClass),true);
* }
* */
super(new DefaultSocketSessionConfig(), NioProcessor.class);//标记2
((DefaultSocketSessionConfig) getSessionConfig()).init(this);
}
super方法(标记2),调用父类AbstractPollingIoAcceptor
构造方法,初始化了DefaultSocketSessionConfig
类,并和NioProcessor
.class
传递给上一级构造方法,构造方法如下
protected AbstractPollingIoAcceptor(IoSessionConfig sessionConfig,
Class<? extends IoProcessor<S>> processorClass) {
//初始化selector,捕获一些异常,将处理器类加载进来
this(sessionConfig, null, new SimpleIoProcessorPool<S>(processorClass),
true);//标记3
}
this使用(标记3),接收到下级传来的DefaultSocketSessionConfig和NioProcessor,
并用SimpleIoProcessorPool初始化,(SimpleIoProcessorPool将在下一节介绍),再次传递给AbstractPollingIoAcceptor
类的构造方法
private AbstractPollingIoAcceptor(IoSessionConfig sessionConfig,
Executor executor, IoProcessor<S> processor,
boolean createdProcessor) {
super(sessionConfig, executor);//标记4
if (processor == null) {
throw new IllegalArgumentException("processor");
}
this.processor = processor;//new SimpleIoProcessorPool()
this.createdProcessor = createdProcessor;//是否创建新的处理池
try {
// Initialize the selector
init();//初始化selector,因此在此处是抽象类,所以最终执行的是实体类的init(),也就是继承了此抽象类的实体类NioSocketAcceptor
// The selector is now ready, we can switch the
// flag to true so that incoming connection can be accepted
selectable = true;
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeIoException("Failed to initialize.", e);
} finally {
if (!selectable) {
try {
destroy();
} catch (Exception e) {
ExceptionMonitor.getInstance().exceptionCaught(e);
}
}
}
}
super方法(标记4),调用父类AbstractIoAcceptor
的构造方法
protected AbstractIoAcceptor(IoSessionConfig sessionConfig, Executor executor) {
super(sessionConfig, executor);//标记5
defaultLocalAddresses.add(null);
}
此处super方法(标记5),调用父类AbstractIoService
的构造方法,线程池等在此处初始化
/**
* 构造器,必须提供一个session配置和一个线程执行器,如果线程执行器为空,则会自动创建一个新的线程池
* Constructor for {@link AbstractIoService}. You need to provide a default
* session configuration and an {@link Executor} for handling I/O events. If
* a null {@link Executor} is provided, a default one will be created using
* {@link Executors#newCachedThreadPool()}.
*
* @param sessionConfig
* the default configuration for the managed {@link IoSession}
* @param executor
* the {@link Executor} used for handling execution of I/O
* events. Can be <code>null</code>.
*/
protected AbstractIoService(IoSessionConfig sessionConfig, Executor executor) {
if (sessionConfig == null) {
throw new IllegalArgumentException("sessionConfig");
}
if (getTransportMetadata() == null) {
throw new IllegalArgumentException("TransportMetadata");
}
if (!getTransportMetadata().getSessionConfigType().isAssignableFrom(
sessionConfig.getClass())) {
throw new IllegalArgumentException("sessionConfig type: "
+ sessionConfig.getClass() + " (expected: "
+ getTransportMetadata().getSessionConfigType() + ")");
}
// Create the listeners, and add a first listener : a activation listener
// for this service, which will give information on the service state.
listeners = new IoServiceListenerSupport(this);
listeners.add(serviceActivationListener);
// Stores the given session configuration
this.sessionConfig = sessionConfig;
// Make JVM load the exception monitor before some transports
// change the thread context class loader.
ExceptionMonitor.getInstance();
if (executor == null) {
this.executor = Executors.newCachedThreadPool();
createdExecutor = true;
} else {
this.executor = executor;
createdExecutor = false;
}
threadName = getClass().getSimpleName() + '-' + id.incrementAndGet();
}
初始化Listener监听机制,就完成了new NioSocketAcceptor()的过程,Listener监听机制将在以后讲解,下一节是
Mina SimpleIoProcessor 的实现
分享到:
相关推荐
NULL 博文链接:https://sziitjiang.iteye.com/blog/1646625
mina-core-2.0.0-M6.jar mina-example-2.0.0-M6.jar mina-filter-codec-netty-2.0.0-M6.jar mina-filter-compression-2.0.0-M6.jar mina-integration-beans-2.0.0-M6.jar mina-integration-jmx-2.0.0-M6.jar mina-...
mina-core-2.0.0-RC1.jar,mina-filter-compression-2.0.0-RC1.jar,mina-transport-apr-2.0.0-RC1.jar
Mina框架测试所需要的三个jar包 :jcl-over-slf4j mina-core slf4j-api
mina-filter-compression-2.0.7.jar,mina 过滤器jar包,核心包之一
apache-mina-2.0.4 mina框架的源码及jar包
JAVA mina 框架源码,非常好。。。。。。。。。
mina-core-2.0.0-RC1-sources.jar
mina-core-2.0.0-M1.jar/mina-example-1.0.5.jar/slf4j-jdk14-1.6.1.jar/slf4j-log4j12-1.6.1.jar mina 所用jar
mina-core-2.0.0-M1-sources.jar是构建mina框架的主要文件
mina-integration-ognl-2.0.0-M4.jar mina-integration-ognl-2.0.0-M4.jar
Mina2.0框架源码剖析
mina-integration-beans-2.0.0-M4.jar mina-integration-beans-2.0.0-M4.jar
Mina是比较程序的Java socket框架,附件为mina框架源码
mina-transport-apr-2.0.2.jar
Mina 2.0快速入门与源码解析 很适合初学者使用!!!
mina-core-2.0.0-M1.jar包和slf4j相关jar包
整理:网站建设与网站制作公司-VeiSun.Com)简单介绍:MINA框架是对java的NIO包的一个封装,简化了NIO程序开发的难度
Java mina框架jar包, mina官网下载 mina-core-2.0.7.jar,正版 相关下载链接
mina-core-2.0.0-M6.jar