Apache mina 源码再读3 I/O Service 源码剖析

2024-02-18 06:58

本文主要是介绍Apache mina 源码再读3 I/O Service 源码剖析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!


Base interface for all  IoAcceptor  and  IoConnector  that provide I/O service and manage   IoSession.

public interface IoService { }


IoAcceptor 和IoConnector  的基础接口IoService 来提供I/O服务和管理IoSession .




public interface IoService {


/**
     * Adds an {@link IoServiceListener} that listens any events related with
     * this service.
     */
    void addListener(IoServiceListener listener);


    /**
     * Removed an existing {@link IoServiceListener} that listens any events
     * related with this service.
     */
    void removeListener(IoServiceListener listener);


 /**
     * Returns the handler which will handle all connections managed by this service.
     */
    IoHandler getHandler();


    /**
     * Sets the handler which will handle all connections managed by this service.
     */
    void setHandler(IoHandler handler);


    /**
     * Returns the map of all sessions which are currently managed by this
     * service.  The key of map is the {@link IoSession#getId() ID} of the
     * session.
     *
     * @return the sessions. An empty collection if there's no session.
     */
    Map<Long, IoSession> getManagedSessions();


    /**
     * Returns the number of all sessions which are currently managed by this
     * service.
     */
    int getManagedSessionCount();


    /**
     * Returns the default configuration of the new {@link IoSession}s
     * created by this service.
     */
    IoSessionConfig getSessionConfig();


    /**
     * Returns the {@link IoFilterChainBuilder} which will build the
     * {@link IoFilterChain} of all {@link IoSession}s which is created
     * by this service.
     * The default value is an empty {@link DefaultIoFilterChainBuilder}.
     */
    IoFilterChainBuilder getFilterChainBuilder();


    /**
     * Sets the {@link IoFilterChainBuilder} which will build the
     * {@link IoFilterChain} of all {@link IoSession}s which is created
     * by this service.
     * If you specify <tt>null</tt> this property will be set to
     * an empty {@link DefaultIoFilterChainBuilder}.
     */
    void setFilterChainBuilder(IoFilterChainBuilder builder);


    /**
     * A shortcut for <tt>( ( DefaultIoFilterChainBuilder ) </tt>{@link #getFilterChainBuilder()}<tt> )</tt>.
     * Please note that the returned object is not a <b>real</b> {@link IoFilterChain}
     * but a {@link DefaultIoFilterChainBuilder}.  Modifying the returned builder
     * won't affect the existing {@link IoSession}s at all, because
     * {@link IoFilterChainBuilder}s affect only newly created {@link IoSession}s.
     *
     * @throws IllegalStateException if the current {@link IoFilterChainBuilder} is
     *                               not a {@link DefaultIoFilterChainBuilder}
     */
    DefaultIoFilterChainBuilder getFilterChain();


    /**
     * Returns a value of whether or not this service is active
     *
     * @return whether of not the service is active.
     */
    boolean isActive();


    /**
     * Returns the time when this service was activated.  It returns the last
     * time when this service was activated if the service is not active now.
     *
     * @return The time by using {@link System#currentTimeMillis()}
     */
    long getActivationTime();


    /**
     * Writes the specified {@code message} to all the {@link IoSession}s
     * managed by this service.  This method is a convenience shortcut for
     * {@link IoUtil#broadcast(Object, Collection)}.
     */
    Set<WriteFuture> broadcast(Object message);


    /**
     * Returns the {@link IoSessionDataStructureFactory} that provides
     * related data structures for a new session created by this service.
     */
    IoSessionDataStructureFactory getSessionDataStructureFactory();


    /**
     * Sets the {@link IoSessionDataStructureFactory} that provides
     * related data structures for a new session created by this service.
     */
    void setSessionDataStructureFactory(IoSessionDataStructureFactory sessionDataStructureFactory);


    /**
     * Returns the number of bytes scheduled to be written
     *
     * @return The number of bytes scheduled to be written
     */
    int getScheduledWriteBytes();


    /**
     * Returns the number of messages scheduled to be written
     *
     * @return The number of messages scheduled to be written
     */
    int getScheduledWriteMessages();


    /**
     * Returns the IoServiceStatistics object for this service.
     * 
     * @return The statistics object for this service.
     */
    IoServiceStatistics getStatistics();


}


An instance of IoService contains an Executor which will handle the incoming  events

一个包含Executor 接口的IoService 抽象类,来处理incoming events.

public abstract class AbstractIoService implements IoService { }



public abstract class AbstractIoService implements IoService {

    /**
     * The unique number identifying the Service. It's incremented
     * for each new IoService created.
     */
    private static final AtomicInteger id = new AtomicInteger();


    /**
     * The thread name built from the IoService inherited
     * instance class name and the IoService Id
     **/
    private final String threadName;


    /**
     * The associated executor, responsible for handling execution of I/O events.
     */


    private final Executor executor;
    /**
     * A flag used to indicate that the local executor has been created
     * inside this instance, and not passed by a caller.
     * 
     * If the executor is locally created, then it will be an instance
     * of the ThreadPoolExecutor class.
     */
    private final boolean createdExecutor;


    /**
     * The IoHandler in charge of managing all the I/O Events. It is
     */
    private IoHandler handler;


    /**
     * The default {@link IoSessionConfig} which will be used to configure new sessions.
     */
    protected final IoSessionConfig sessionConfig;


  /**
     * Current filter chain builder.
     */
    private IoFilterChainBuilder filterChainBuilder = new DefaultIoFilterChainBuilder();


    private IoSessionDataStructureFactory sessionDataStructureFactory = new DefaultIoSessionDataStructureFactory();


    /**
     * Maintains the {@link IoServiceListener}s of this service.
     */
    private final IoServiceListenerSupport listeners;


    /**
     * A lock object which must be acquired when related resources are
     * destroyed.
     */
    protected final Object disposalLock = new Object();


    private volatile boolean disposing;


    private volatile boolean disposed;


    /**
     * {@inheritDoc}
     */
    private IoServiceStatistics stats = new IoServiceStatistics(this);

}

A helper class which provides addition and removal of  IoServiceListener and firing

在AbstractIoService 中涉及到IoServiceListenerSupport 类,来关联IoServiceListener 监听器和fire事件。IoServiceStatistics 来统计与IoSession相关数据。


public interface IoServiceListener extends EventListener {/*** Invoked when a new service is activated by an {@link IoService}.** @param service the {@link IoService}*/void serviceActivated(IoService service) throws Exception;/*** Invoked when a service is idle.*/void serviceIdle(IoService service, IdleStatus idleStatus) throws Exception;/*** Invoked when a service is deactivated by an {@link IoService}.** @param service the {@link IoService}*/void serviceDeactivated(IoService service) throws Exception;/*** Invoked when a new session is created by an {@link IoService}.** @param session the new session*/void sessionCreated(IoSession session) throws Exception;/*** Invoked when a new session is closed by an {@link IoService}.* * @param session*            the new session*/void sessionClosed(IoSession session) throws Exception;/*** Invoked when a session is being destroyed by an {@link IoService}.* * @param session*            the session to be destroyed*/void sessionDestroyed(IoSession session) throws Exception;
}

public class IoServiceListenerSupport {/** The {@link IoService} that this instance manages. */private final IoService service;/** A list of {@link IoServiceListener}s. */private final List<IoServiceListener> listeners = new CopyOnWriteArrayList<IoServiceListener>();/** Tracks managed sessions. */private final ConcurrentMap<Long, IoSession> managedSessions = new ConcurrentHashMap<Long, IoSession>();/**  Read only version of {@link #managedSessions}. */private final Map<Long, IoSession> readOnlyManagedSessions = Collections.unmodifiableMap(managedSessions);private final AtomicBoolean activated = new AtomicBoolean();/** Time this listenerSupport has been activated */private volatile long activationTime;/** A counter used to store the maximum sessions we managed since the listenerSupport has been activated */private volatile int largestManagedSessionCount = 0;/** A global counter to count the number of sessions managed since the start */private volatile long cumulativeManagedSessionCount = 0;}


Processor 处理newSession 相关逻辑。


  /*** Loops over the new sessions blocking queue and returns the number of* sessions which are effectively created** @return The number of new sessions*/private int handleNewSessions() {int addedSessions = 0;//Acceptor 线程把新链接通过并发队列放入到IoProccessor线程中。IoProccessor线程优先新链接的socketfor (S session = newSessions.poll(); session != null; session = newSessions.poll()) {if (addNow(session)) {// A new session has been createdaddedSessions++;}}return addedSessions;}

Processor 把newSession队列中的NioSession移除掉。然后IoServiceListenerSupport 执行fireSessionCreated()事件。


  /*** Process a new session :* - initialize it* - create its chain* - fire the CREATED listeners if any** @param session The session to create* @return true if the session has been registered*/private boolean addNow(S session) {boolean registered = false;//处理一个新socket的流程,初始化IoSession,创建一个handler chain 和创建监听器try {init(session);registered = true;// Build the filter chain of this session.IoFilterChainBuilder chainBuilder = session.getService().getFilterChainBuilder();chainBuilder.buildFilterChain(session.getFilterChain());// DefaultIoFilterChain.CONNECT_FUTURE is cleared inside here// in AbstractIoFilterChain.fireSessionOpened().// Propagate the SESSION_CREATED event up to the chainIoServiceListenerSupport listeners = ((AbstractIoService) session.getService()).getListeners();listeners.fireSessionCreated(session);} catch (Exception e) {ExceptionMonitor.getInstance().exceptionCaught(e);try {destroy(session);} catch (Exception e1) {ExceptionMonitor.getInstance().exceptionCaught(e1);} finally {registered = false;}}return registered;}

  /*** Calls {@link IoServiceListener#sessionCreated(IoSession)} for all registered listeners.* * @param session The session which has been created*/public void fireSessionCreated(IoSession session) {boolean firstSession = false;if (session.getService() instanceof IoConnector) {synchronized (managedSessions) {firstSession = managedSessions.isEmpty();}}// If already registered, ignore.if (managedSessions.putIfAbsent(session.getId(), session) != null) {return;}// If the first connector session, fire a virtual service activation event.if (firstSession) {fireServiceActivated();}// Fire session events.IoFilterChain filterChain = session.getFilterChain();filterChain.fireSessionCreated();filterChain.fireSessionOpened();int managedSessionCount = managedSessions.size();if (managedSessionCount > largestManagedSessionCount) {largestManagedSessionCount = managedSessionCount;}cumulativeManagedSessionCount++;// Fire listener events.for (IoServiceListener l : listeners) {try {l.sessionCreated(session);} catch (Exception e) {ExceptionMonitor.getInstance().exceptionCaught(e);}}}

Processor 线程把NioSession 放到IoServiceListenerSupport 的managedSessions 队列中。在IoServiceListenerSupport 来追踪IoSession.


    public void fireSessionCreated(IoSession session) {boolean firstSession = false;if (session.getService() instanceof IoConnector) {synchronized (managedSessions) {firstSession = managedSessions.isEmpty();}}// If already registered, ignore.if (managedSessions.putIfAbsent(session.getId(), session) != null) {return;}// If the first connector session, fire a virtual service activation event.if (firstSession) {fireServiceActivated();}// Fire session events.IoFilterChain filterChain = session.getFilterChain();filterChain.fireSessionCreated();filterChain.fireSessionOpened();int managedSessionCount = managedSessions.size();if (managedSessionCount > largestManagedSessionCount) {largestManagedSessionCount = managedSessionCount;}cumulativeManagedSessionCount++;// Fire listener events.for (IoServiceListener l : listeners) {try {l.sessionCreated(session);} catch (Exception e) {ExceptionMonitor.getInstance().exceptionCaught(e);}}}


在IoServiceListenerSupport 分别触发了fireSessionCreated(),fireSessionOpened()事件。



这篇关于Apache mina 源码再读3 I/O Service 源码剖析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/720392

相关文章

MybatisPlus service接口功能介绍

《MybatisPlusservice接口功能介绍》:本文主要介绍MybatisPlusservice接口功能介绍,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友... 目录Service接口基本用法进阶用法总结:Lambda方法Service接口基本用法MyBATisP

SpringBoot整合Apache Flink的详细指南

《SpringBoot整合ApacheFlink的详细指南》这篇文章主要为大家详细介绍了SpringBoot整合ApacheFlink的详细过程,涵盖环境准备,依赖配置,代码实现及运行步骤,感兴趣的... 目录1. 背景与目标2. 环境准备2.1 开发工具2.2 技术版本3. 创建 Spring Boot

Linux中修改Apache HTTP Server(httpd)默认端口的完整指南

《Linux中修改ApacheHTTPServer(httpd)默认端口的完整指南》ApacheHTTPServer(简称httpd)是Linux系统中最常用的Web服务器之一,本文将详细介绍如何... 目录一、修改 httpd 默认端口的步骤1. 查找 httpd 配置文件路径2. 编辑配置文件3. 保存

Spring Boot 整合 Apache Flink 的详细过程

《SpringBoot整合ApacheFlink的详细过程》ApacheFlink是一个高性能的分布式流处理框架,而SpringBoot提供了快速构建企业级应用的能力,下面给大家介绍Spri... 目录Spring Boot 整合 Apache Flink 教程一、背景与目标二、环境准备三、创建项目 & 添

Apache 高级配置实战之从连接保持到日志分析的完整指南

《Apache高级配置实战之从连接保持到日志分析的完整指南》本文带你从连接保持优化开始,一路走到访问控制和日志管理,最后用AWStats来分析网站数据,对Apache配置日志分析相关知识感兴趣的朋友... 目录Apache 高级配置实战:从连接保持到日志分析的完整指南前言 一、Apache 连接保持 - 性

apache的commons-pool2原理与使用实践记录

《apache的commons-pool2原理与使用实践记录》ApacheCommonsPool2是一个高效的对象池化框架,通过复用昂贵资源(如数据库连接、线程、网络连接)优化系统性能,这篇文章主... 目录一、核心原理与组件二、使用步骤详解(以数据库连接池为例)三、高级配置与优化四、典型应用场景五、注意事

8种快速易用的Python Matplotlib数据可视化方法汇总(附源码)

《8种快速易用的PythonMatplotlib数据可视化方法汇总(附源码)》你是否曾经面对一堆复杂的数据,却不知道如何让它们变得直观易懂?别慌,Python的Matplotlib库是你数据可视化的... 目录引言1. 折线图(Line Plot)——趋势分析2. 柱状图(Bar Chart)——对比分析3

解决Maven项目报错:failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.13.0的问题

《解决Maven项目报错:failedtoexecutegoalorg.apache.maven.plugins:maven-compiler-plugin:3.13.0的问题》这篇文章主要介... 目录Maven项目报错:failed to execute goal org.apache.maven.pl

Android实现一键录屏功能(附源码)

《Android实现一键录屏功能(附源码)》在Android5.0及以上版本,系统提供了MediaProjectionAPI,允许应用在用户授权下录制屏幕内容并输出到视频文件,所以本文将基于此实现一个... 目录一、项目介绍二、相关技术与原理三、系统权限与用户授权四、项目架构与流程五、环境配置与依赖六、完整

Android实现定时任务的几种方式汇总(附源码)

《Android实现定时任务的几种方式汇总(附源码)》在Android应用中,定时任务(ScheduledTask)的需求几乎无处不在:从定时刷新数据、定时备份、定时推送通知,到夜间静默下载、循环执行... 目录一、项目介绍1. 背景与意义二、相关基础知识与系统约束三、方案一:Handler.postDel