dubbo源码分析10——服务暴露1_export()方法分析

ServiceConfig类中的export()方法,是dubbo服务暴露的入口方法,被触发的时机有两个:

    1. spring容器初始化完成所有的bean实例后,通过事件机制触发

    2. 实现InitializingBean的方法中进行触发

export()方法源码如下:

public synchronized void export() {
        if (provider != null) {
            if (export == null) {
                export = provider.getExport();
            }
            if (delay == null) {
                delay = provider.getDelay();
            }
        }
        if (export != null && ! export.booleanValue()) {
            return;
        }
        if (delay != null && delay > 0) {    //如果要进行延迟暴露,则开启一个子线程,在子线程中进行服务暴露的工作
            Thread thread = new Thread(new Runnable() {
                public void run() {
                    try {
                        Thread.sleep(delay);
                    } catch (Throwable e) {
                    }
                    doExport();
                }
            });
            thread.setDaemon(true);  //由于是后台线程,则如果当前只有后台线程存在的情况下,JAVA虚拟机将退出,这样当主线程结束,以及主线程的其他user线程都结束的情况下,daemon线程也将结束
            thread.setName("DelayExportServiceThread");
            thread.start();
        } else {
            doExport(); 
        }
    }
    

通过查看源码可知,export方法处理了如何延时暴露,然后调用doExport()方法进行暴露

原文地址:https://www.cnblogs.com/hzhuxin/p/7675796.html