package com.elab.core.componts;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * @Module 加载工具类
 * @Description 判断应用是否加载完毕
 * @Author liukaixiong
 * @Date 2020/12/24 14:30
 */
public class LoadContextTool {
    private Logger logger = LoggerFactory.getLogger(getClass());
    private volatile boolean isLoadFinish = false;

    private CountDownLatch countDownLatch;

    public LoadContextTool(Integer countLatch) {
        this.countDownLatch = new CountDownLatch(countLatch);
    }

    public void awaitFinish() {
        if (!isLoadFinish) {
            try {
                logger.warn("等待应用程序唤醒...");
                countDownLatch.await(30, TimeUnit.SECONDS);
                logger.debug("唤醒成功...");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void loadFinish() {
        this.isLoadFinish = true;
        countDownLatch.countDown();
    }

}
