淘宝diamond学习笔记五:定时任务

服务端定时任务:

文件:/diamond-server/src/main/java/com/taobao/diamond/server/service/DumpConfigInfoTask.java

public final class DumpConfigInfoTask implements Runnable {
    private static final Log log = LogFactory.getLog(DumpConfigInfoTask.class);
    private static final int PAGE_SIZE = 1000;
    private final TimerTaskService timerTaskService;
    public DumpConfigInfoTask(TimerTaskService timerTaskService) {
        this.timerTaskService = timerTaskService;
    }
    public void run() {
        try {
            Page<ConfigInfo> page = this.timerTaskService.getPersistService().findAllConfigInfo(1, PAGE_SIZE);
            if (page != null) {
                // 总页数
                int totalPages = page.getPagesAvailable();
                updateConfigInfo(page);
                if (totalPages > 1) {
                    for (int pageNo = 2; pageNo <= totalPages; pageNo++) {
                        page = this.timerTaskService.getPersistService().findAllConfigInfo(pageNo, PAGE_SIZE);
                        if (page != null) {
                            updateConfigInfo(page);
                        }
                    }
                }
            }
        }
        catch (Throwable t) {
            log.error("dump task run error", t);
        }
    }

    private void updateConfigInfo(Page<ConfigInfo> page) throws IOException {
        for (ConfigInfo configInfo : page.getPageItems()) {
            if (configInfo == null) {
                continue;
            }
            try {
                // 写入磁盘,更新缓存
                this.timerTaskService.getConfigService().updateMD5Cache(configInfo);
                this.timerTaskService.getDiskService().saveToDisk(configInfo);
            }
            catch (Throwable t) {
                log.error(
                    "dump config info error, dataId=" + configInfo.getDataId() + ", group=" + configInfo.getGroup(), t);
            }
        }
    }

}

客户端定时任务:

文件:/diamond-client/src/main/java/com/taobao/diamond/client/impl/DefaultDiamondSubscriber.java

     /**
     * 循环探测配置信息是否变化,如果变化,则再次向DiamondServer请求获取对应的配置信息
     */
    private void rotateCheckConfigInfo() {
        scheduledExecutor.schedule(new Runnable() {
            public void run() {
                if (!isRun) {
                    log.warn("DiamondSubscriber不在运行状态中,退出查询循环");
                    return;
                }
                try {
                    checkLocalConfigInfo();
                    checkDiamondServerConfigInfo();
                    checkSnapshot();
                }
                catch (Exception e) {
                    e.printStackTrace();
                    log.error("循环探测发生异常", e);
                }
                finally {
                    rotateCheckConfigInfo();
                }
            }

        }, bFirstCheck ? 20 : diamondConfigure.getPollingIntervalTime(), TimeUnit.SECONDS);
        bFirstCheck = false;
    }


你可能感兴趣的:(定时任务,淘宝diamond)