apache mina 与 SEDA


在apache  mina创建之初作者就开始讨论SEDA在mina中的应用。在mina官网中也配置mina的线程模型时,也提及SEDA。那让我们看看SEDA在apache mina中的应用吧。



1 -通过队列queue把服务分成多个stages.

     1.1 每一个阶段执行请求过程中的一个部分。1.2 阶段内部通过事件驱动,特别是非阻塞模型 。1.3 用队列来区分边界

2 每一个阶段包含一个线程池来驱动阶段的执行.

    2.1 每一个阶段中的线程池不会暴露给应用程序  2.2 根据需要动态控制线程池

3  最好的区分事件和线程   

    3.1 根据事件流来明确来明确编程逻辑


接下来,我们以apache mina的服务器端来分析SEDA架构在mina中是如何应用的?

在apache  mina服务器端通过把整个应该程序划分为4个阶段。

Acceptor阶段  ----->newSessions 队列---->Processor 阶段--->IoFilterChain 队列-->ExecutorFilter阶段-->--->waitingSessions 队列--->业务处理阶段


紧接着我们把每一阶段具体实现来分析一下:


1 -Acceptor阶段。

    /**
     * This class is called by the startupAcceptor() method and is
     * placed into a NamePreservingRunnable class.
     * It's a thread accepting incoming connections from clients.
     * The loop is stopped when all the bound handlers are unbound.
     */
    private class Acceptor implements Runnable {
        public void run() {}
}

    /** A Session queue containing the newly created sessions */
    private final Queue<S> newSessions = new ConcurrentLinkedQueue<S>();

    public final void add(S session) {
        if (disposed || disposing) {
            throw new IllegalStateException("Already disposed.");
        }
        // Adds the session to the newSession queue and starts the worker
        newSessions.add(session);
        startupProcessor();
    }

Acceptor阶段是接收新socket进来,然后把IoSession加入到newSessions队列中。通过startupProcessor()方法来启动下一个阶段线程。

在Acceptor阶段是通过Acceptor 线程来驱动事件的。当int selected = select(); 事件发生时,表示新socket的来临。


2 -Processor 阶段。

    /**
     * The main loop. This is the place in charge to poll the Selector, and to
     * process the active sessions. It's done in
     * - handle the newly created sessions
     * -
     */
    private class Processor implements Runnable {
        public void run() {}
}
    /** A queue used to store the sessions to be removed */
    private final Queue<S> removingSessions = new ConcurrentLinkedQueue<S>();

    /** A queue used to store the sessions to be flushed */
    private final Queue<S> flushingSessions = new ConcurrentLinkedQueue<S>();

Processor 阶段主要处理Socket的输入输出流。在这个阶段涉及到removingSessions队列和flushingSessions 队列。

已读取数据为例,在IoFilterChain过滤链中,触发messageReceived事件来驱动。

3 -ExecutorFilter 阶段。

当ExecutorFilter接收到messageReceived 事件时,会把消息分发到OrderedThreadPoolExecutor线程池中。

    private class Worker implements Runnable {

        private volatile long completedTaskCount;

        private Thread thread;

        public void run() {}
}
    /** A queue used to store the available sessions */
    private final BlockingQueue<IoSession> waitingSessions = new LinkedBlockingQueue<IoSession>();


该阶段由Worker线程来驱动事件。在该阶段中waitingSessions 队列来区分边界。

4-业务处理阶段。

在最后阶段,为应用程序处理阶段。该阶段可以定义业务处理的线程。在Fix协议中定义一个IoSession一个线程来处理的。




1 队列主要负责进入控制策略  

     

 2-队列分割执行边界 


 3-明确事件分发,支持监控





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值