ZeroMQ(java)中连接建立与重连机制(四)

2014-11-24 07:32:15 · 作者: · 浏览: 1
session对象,让其进行一些处理,session的处理就包括重连接了,那么来看看他做了啥:
    //相当于是要移除底层的Engine的关联
    public void detach()  {
        //  Engine is dead. Let's forget about it.
        engine = null;  //这里相当于就会释放当前的engine对象

        //  Remove any half-done messages from the pipes.
        clean_pipes ();  //清除那些没有接受完的msg
 
        //  Send the event to the derived class.
        detached ();   //取消pipe,然后重连接

        //  Just in case there's only a delimiter in the pipe.
        if (pipe != null)
            pipe.check_read ();
    }

这里还看不到进行重连接的代码,接下来继续看detached方法:
    private void detached() {
        //  Transient session self-destructs after peer disconnects.
        if (!connect) {  //如果不是主动建立连接的话,那么就直接终止就好了否则的话还进行重连接的尝试
            terminate ();
            return;
        }

        //  For delayed connect situations, terminate the pipe
        //  and reestablish later on
        if (pipe != null && options.delay_attach_on_connect == 1
            && addr.protocol () != pgm && addr.protocol () != epgm) {
            pipe.hiccup ();
            pipe.terminate (false);
            terminating_pipes.add (pipe);
            pipe = null;
        }
        
        reset ();  // 复位标志位

        //这里主动进行重连接的尝试
        if (options.reconnect_ivl != -1) {
            start_connecting (true);   //进行重连接尝试,这里也就是需要进行一些延迟
        }

        //  For subscriber sockets we hiccup the inbound pipe, which will cause
        //  the socket object to resend all the subscriptions.
        if (pipe != null && (options.type == ZMQ.ZMQ_SUB || options.type == ZMQ.ZMQ_XSUB))
            pipe.hiccup ();

    }

这里可以看到调用了start_conneting方法,不过这里传进去的参数是true,具体的执行流程与上面建立连接差不多,只不过这里是延迟进行连接的。。。

也就是会在IO线程上面设置定时,当超时之后才会进行连接。。。这样也就使得重连接在一定的频率内进行。。。

具体的定时就不细讲了,蛮简单的。。。