关闭

知识库--StandardService + Lifecycle(start/stop)+initialize(72)

标签: service
109人阅读 评论(0) 收藏 举报
分类:

StandardService + Lifecycle(start/stop)+initialize

核心:
//Server提供了一个优雅的启动和停止方法
//Service 封装了container和多个connectors

The lifecycle methods are the start and the stop methods inherited from the Lifecycle interface plus the initialize method.

//initialize method calls the initialize method of each connector

public void initialize() throws LifecycleException{
    if(initialized){
        throw new LifecycleException();
    }
    initialized = true;

    synchronized(connectors){
        for(int i =0;i<connectors.length;i++){
            connectors[i].initialize();
        }
    }   
}

//start method starts the associated container as well as the connector added to this service

public void start() throws LifecycleException{
    if(started)
        throw new LifecycleException();
    //notify our interested LifecycleListeners
    lifecyle.fireLifecycleEvent(BEFORE_START_EVENT,null);
    //...sout
    lifecycle.fireLifecycleEvent(START_EVENT,null);
    started = true;
    //start our defined container first
    if(container != null){
        synchronized(container){
            if(container instanceof Lifecycle){
                ((Lifecycle)container).start();
            }
        }
    }
    //start our defined connectors 
    synchronized(connectors){
        for(int i =0;i<connectors.length;i++){
            if(connectors[i] instanceof Lifecycle){
                ((Lifecycle)connectors[i]).start();
            }
        }
    }
    //notify
    lifecycle.fireLifecycleEvent(AFTER_START_EVENT,null);
}

//stop method stops the associated container and all the connectors

public void stop()throws LifecycleExcetption{
    //validate and update our current component state
    if(!started){
        throw new LifecycleException();
    }
    //notify our interested lifecycleListeners
    lifecycle.fireLifecycleEvent(BEFORE_STOP_EVENT,null);
    lifecycle.fireLifecycleEvent(STOP_EVENT,null);
    //sout...
    started = false;
    //stop connectors first
    synchronized(connectors){
        for(int i =0;i<connectors.length;i++){
            if(connectors[i] instanceof Lifecycle)
                ((Lifecycle)connectors[i]).stop();
        }
    }
    //stop container second
    if(container !=null){
        synchronized(container){
            if(container instanceof Lifecycle){
                ((Lifecycle)container).stop();
            }
        }
    }
    //notify
    lifecycle.fireLifecycleEvent(AFTER_STOP_EVENT,null);
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:42483次
    • 积分:2588
    • 等级:
    • 排名:第14220名
    • 原创:117篇
    • 转载:8篇
    • 译文:109篇
    • 评论:5条
    文章分类