5 xxl-job原理-- 执行器注册问题

xxl-job: v2.0.2 原理 目录学习

  • 0. xxl-job原理
  • 1. xxl-job原理---定时任务架构
  • 2. xxl-job原理-- 调度中心
  • 3. xxl-job原理-- 执行器注册
  • 4. xxl-job原理-- 执行器注册问题
  • 5 xxl-job原理-- 执行器注册问题
  • 6. xxl-job 原理-- 调度中心注册
  • 7. xxl-job 原理-- 任务管理
  • 8. xxl-job 原理-- 任务执行或触发
  • 9. xxl-job原理-- jobthread的作用
  • 10. xxl-job原理---回调

xxl-job-admin 调度中心, 每隔30s, 会检测 注册表, 删除90s之前的注册信息

1. XxlJobScheduler 调度中心的代码

// init i18n
// admin registry monitor run
//  admin  registry 开始工作,将注册表信息注入到执行器表中, 每隔30s检测,删除90s之前的数据
//  1.  auto registry group   
//  2. remove dead address (admin/executor)
//  3. fresh online address (admin/executor)
//  4. fresh group address
JobRegistryMonitorHelper.getInstance().start();
// admin monitor run
// 1、fail retry monitor
// 2、fail alarm monitor
JobFailMonitorHelper.getInstance().start();
 // init   admin-server  , 做一些准备工作
 initRpcProvider();
// start-schedule      
//           schedule thread 
//           ringThread
 JobScheduleHelper.getInstance().start();
// 1、预读10s内调度任务
// 2、推送时间轮
     // 时间轮刻度计算
    过期超10s:本地忽略,当前时间开始计算下次触发时间
    过期10s内:立即触发一次,当前时间开始计算下次触发时间
   未过期:正常触发,递增计算下次触发时间
    push async ring
3、更新trigger信息



2.触发任务

JobTriggerPoolHelper.trigger(jobId, TriggerTypeEnum.CRON, -1, null, null);

3.修正执行器注册 ,dashboard能够看到其注册信息

JobRegistryMonitorHelper

public void start() {
        registryThread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (!toStop) {
                    try {
                        // auto registry group
                        List groupList = XxlJobAdminConfig.getAdminConfig().getXxlJobGroupDao().findByAddressType(0);
//                      if (groupList!=null && !groupList.isEmpty()) {

                        // remove dead address (admin/executor)
                        XxlJobAdminConfig.getAdminConfig().getXxlJobRegistryDao().removeDead(RegistryConfig.DEAD_TIMEOUT);

                        // fresh online address (admin/executor)
                        Map> appAddressMap = new ConcurrentHashMap>();
                        appAddressMap = XxlJobScheduler.jobGroupCache;
                        List list = XxlJobAdminConfig.getAdminConfig().getXxlJobRegistryDao().findAll(RegistryConfig.DEAD_TIMEOUT);
                        if (list != null) {
                            for (XxlJobRegistry item : list) {
                                if (RegistryConfig.RegistType.EXECUTOR.name().equals(item.getRegistryGroup())) {
                                    String appName = item.getRegistryKey();
                                    List registryList = appAddressMap.get(appName);
                                    if (registryList == null) {
                                        registryList = new ArrayList();
                                    }

                                    if (!registryList.contains(item.getRegistryValue())) {
                                        registryList.add(item.getRegistryValue());
                                    }
                                    appAddressMap.put(appName, registryList);
                                }
                            }
                        }

                        // fresh group address
                        for (Map.Entry> entry : appAddressMap.entrySet()) {
                            for (XxlJobGroup group : groupList) {
                                if (entry.getKey().equals(group.getAppName())) {
                                    List registryList = appAddressMap.get(group.getAppName());
                                    String addressListStr = null;
                                    if (registryList != null && !registryList.isEmpty()) {
                                        Collections.sort(registryList);
                                        addressListStr = Joiner.on(",").join(registryList);
                                    }
                                    group.setAddressList(addressListStr);
                                    XxlJobAdminConfig.getAdminConfig().getXxlJobGroupDao().update(group);
                                } else {
                                    if ("xxl-job-executor-sample".equals(group.getAppName())) {
                                        continue;
                                    }
                                    XxlJobAdminConfig.getAdminConfig().getXxlJobGroupDao().remove(group.getId());
                                }
                            }
                            String appName = entry.getKey();
                            String addressListStr = Joiner.on(",").join(entry.getValue());
                            XxlJobGroup group = new XxlJobGroup();
                            group.setAppName(appName);
                            group.setAddressType(0);
                            group.setAddressList(addressListStr);
                            group.setTitle("自动注入的执行器");
                            int order = XxlJobAdminConfig.getAdminConfig().getXxlJobGroupDao().findMaxOrder();
                            group.setOrder(order + 1);
                            List oldjobGroup = XxlJobAdminConfig.getAdminConfig().getXxlJobGroupDao().findByAppName(group);
                            if (oldjobGroup == null || oldjobGroup.isEmpty()) {
                                XxlJobAdminConfig.getAdminConfig().getXxlJobGroupDao().save(group);
                            }
                        }
                    } catch (Exception e) {
                        if (!toStop) {
                            logger.error(">>>>>>>>>>> xxl-job, job registry monitor thread error:{}", e);
                        }
                    }
                    try {
                        TimeUnit.SECONDS.sleep(RegistryConfig.BEAT_TIMEOUT);
                    } catch (InterruptedException e) {
                        if (!toStop) {
                            logger.error(">>>>>>>>>>> xxl-job, job registry monitor thread error:{}", e);
                        }
                    }
                }
                logger.info(">>>>>>>>>>> xxl-job, job registry monitor thread stop");
            }
        });
        registryThread.setDaemon(true);
        registryThread.setName("xxl-job, admin JobRegistryMonitorHelper");
        registryThread.start();
    }

总结

  1. 开启国际化i18n
  2. 开启admin registry 注册器
  3. 开启jobFail 失败执行器
  4. 开启admin server, 用于构建xxlRpcProviderFactory和servletServerHandler
  5. 开启定时任务触发器, job scheudle

PS: 若你觉得可以、还行、过得去、甚至不太差的话,可以“关注”一下,就此谢过!

你可能感兴趣的:(5 xxl-job原理-- 执行器注册问题)