From 7b052d7331c856674d906a925c1f42d5d5b5efb8 Mon Sep 17 00:00:00 2001 From: xuxueli <931591021@qq.com> Date: Fri, 14 Jul 2017 15:49:12 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E5=BA=A6=E4=B8=AD=E5=BF=83=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E6=B3=A8=E5=86=8C=E6=A3=80=E6=B5=8B=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E4=BC=98=E5=8C=96=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admin/controller/JobGroupController.java | 18 ------ .../xxl/job/admin/core/model/XxlJobGroup.java | 19 +++--- .../core/thread/JobRegistryMonitorHelper.java | 64 +++++++++++-------- .../job/admin/core/trigger/XxlJobTrigger.java | 17 +---- .../xxl/job/admin/dao/IXxlJobGroupDao.java | 2 + .../admin/dao/impl/XxlJobGroupDaoImpl.java | 5 ++ .../admin/service/impl/XxlJobServiceImpl.java | 16 ++--- .../mybatis-mapper/XxlJobGroupMapper.xml | 9 ++- 8 files changed, 69 insertions(+), 81 deletions(-) diff --git a/xxl-job-admin/src/main/java/com/xxl/job/admin/controller/JobGroupController.java b/xxl-job-admin/src/main/java/com/xxl/job/admin/controller/JobGroupController.java index 7b6ec968..bd9e8f7b 100644 --- a/xxl-job-admin/src/main/java/com/xxl/job/admin/controller/JobGroupController.java +++ b/xxl-job-admin/src/main/java/com/xxl/job/admin/controller/JobGroupController.java @@ -1,12 +1,9 @@ package com.xxl.job.admin.controller; import com.xxl.job.admin.core.model.XxlJobGroup; -import com.xxl.job.admin.core.thread.JobRegistryMonitorHelper; import com.xxl.job.admin.dao.IXxlJobGroupDao; import com.xxl.job.admin.dao.IXxlJobInfoDao; import com.xxl.job.core.biz.model.ReturnT; -import com.xxl.job.core.enums.RegistryConfig; -import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang.StringUtils; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; @@ -14,7 +11,6 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; -import java.util.Arrays; import java.util.List; /** @@ -36,20 +32,6 @@ public class JobGroupController { // job group (executor) List list = xxlJobGroupDao.findAll(); - if (CollectionUtils.isNotEmpty(list)) { - for (XxlJobGroup group: list) { - List registryList = null; - if (group.getAddressType() == 0) { - registryList = JobRegistryMonitorHelper.discover(RegistryConfig.RegistType.EXECUTOR.name(), group.getAppName()); - } else { - if (StringUtils.isNotBlank(group.getAddressList())) { - registryList = Arrays.asList(group.getAddressList().split(",")); - } - } - group.setRegistryList(registryList); - } - } - model.addAttribute("list", list); return "jobgroup/jobgroup.index"; } diff --git a/xxl-job-admin/src/main/java/com/xxl/job/admin/core/model/XxlJobGroup.java b/xxl-job-admin/src/main/java/com/xxl/job/admin/core/model/XxlJobGroup.java index 6e6b7add..85fb4a10 100644 --- a/xxl-job-admin/src/main/java/com/xxl/job/admin/core/model/XxlJobGroup.java +++ b/xxl-job-admin/src/main/java/com/xxl/job/admin/core/model/XxlJobGroup.java @@ -1,5 +1,9 @@ package com.xxl.job.admin.core.model; +import org.apache.commons.lang.StringUtils; + +import java.util.ArrayList; +import java.util.Arrays; import java.util.List; /** @@ -16,6 +20,12 @@ public class XxlJobGroup { // registry list private List registryList; // 执行器地址列表(系统注册) + public List getRegistryList() { + if (StringUtils.isNotBlank(addressList)) { + registryList = new ArrayList(Arrays.asList(addressList.split(","))); + } + return registryList; + } public int getId() { return id; @@ -49,14 +59,6 @@ public class XxlJobGroup { this.order = order; } - public List getRegistryList() { - return registryList; - } - - public void setRegistryList(List registryList) { - this.registryList = registryList; - } - public int getAddressType() { return addressType; } @@ -72,4 +74,5 @@ public class XxlJobGroup { public void setAddressList(String addressList) { this.addressList = addressList; } + } diff --git a/xxl-job-admin/src/main/java/com/xxl/job/admin/core/thread/JobRegistryMonitorHelper.java b/xxl-job-admin/src/main/java/com/xxl/job/admin/core/thread/JobRegistryMonitorHelper.java index 881df981..bb092291 100644 --- a/xxl-job-admin/src/main/java/com/xxl/job/admin/core/thread/JobRegistryMonitorHelper.java +++ b/xxl-job-admin/src/main/java/com/xxl/job/admin/core/thread/JobRegistryMonitorHelper.java @@ -1,14 +1,17 @@ package com.xxl.job.admin.core.thread; +import com.xxl.job.admin.core.model.XxlJobGroup; import com.xxl.job.admin.core.model.XxlJobRegistry; import com.xxl.job.admin.core.schedule.XxlJobDynamicScheduler; import com.xxl.job.core.enums.RegistryConfig; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; -import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; /** @@ -23,8 +26,6 @@ public class JobRegistryMonitorHelper { return instance; } - private ConcurrentHashMap> registMap = new ConcurrentHashMap>(); - private Thread registryThread; private boolean toStop = false; public void start(){ @@ -33,26 +34,42 @@ public class JobRegistryMonitorHelper { public void run() { while (!toStop) { try { - // remove dead admin/executor - XxlJobDynamicScheduler.xxlJobRegistryDao.removeDead(RegistryConfig.DEAD_TIMEOUT); + // auto registry group + List groupList = XxlJobDynamicScheduler.xxlJobGroupDao.findByAddressType(0); + if (CollectionUtils.isNotEmpty(groupList)) { - // fresh registry map - ConcurrentHashMap> temp = new ConcurrentHashMap>(); - List list = XxlJobDynamicScheduler.xxlJobRegistryDao.findAll(RegistryConfig.DEAD_TIMEOUT); - if (list != null) { - for (XxlJobRegistry item: list) { - String groupKey = makeGroupKey(item.getRegistryGroup(), item.getRegistryKey()); - List registryList = temp.get(groupKey); - if (registryList == null) { - registryList = new ArrayList(); - } - if (!registryList.contains(item.getRegistryValue())) { - registryList.add(item.getRegistryValue()); + // remove dead address (admin/executor) + XxlJobDynamicScheduler.xxlJobRegistryDao.removeDead(RegistryConfig.DEAD_TIMEOUT); + + // fresh online address (admin/executor) + HashMap> appAddressMap = new HashMap>(); + List list = XxlJobDynamicScheduler.xxlJobRegistryDao.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); + } } - temp.put(groupKey, registryList); + } + + // fresh group address + for (XxlJobGroup group: groupList) { + List registryList = appAddressMap.get(group.getAppName()); + String addressListStr = StringUtils.join(registryList, ","); + + group.setAddressList(addressListStr); + XxlJobDynamicScheduler.xxlJobGroupDao.update(group); } } - registMap = temp; } catch (Exception e) { logger.error("job registry instance error:{}", e); } @@ -73,13 +90,4 @@ public class JobRegistryMonitorHelper { //registryThread.interrupt(); } - private static String makeGroupKey(String registryGroup, String registryKey){ - return registryGroup.concat("_").concat(registryKey); - } - - public static List discover(String registryGroup, String registryKey){ - String groupKey = makeGroupKey(registryGroup, registryKey); - return instance.registMap.get(groupKey); - } - } diff --git a/xxl-job-admin/src/main/java/com/xxl/job/admin/core/trigger/XxlJobTrigger.java b/xxl-job-admin/src/main/java/com/xxl/job/admin/core/trigger/XxlJobTrigger.java index f50dc5e3..bd142fdf 100644 --- a/xxl-job-admin/src/main/java/com/xxl/job/admin/core/trigger/XxlJobTrigger.java +++ b/xxl-job-admin/src/main/java/com/xxl/job/admin/core/trigger/XxlJobTrigger.java @@ -7,18 +7,14 @@ import com.xxl.job.admin.core.model.XxlJobLog; import com.xxl.job.admin.core.route.ExecutorRouteStrategyEnum; import com.xxl.job.admin.core.schedule.XxlJobDynamicScheduler; import com.xxl.job.admin.core.thread.JobFailMonitorHelper; -import com.xxl.job.admin.core.thread.JobRegistryMonitorHelper; import com.xxl.job.core.biz.model.ReturnT; import com.xxl.job.core.biz.model.TriggerParam; import com.xxl.job.core.enums.ExecutorBlockStrategyEnum; -import com.xxl.job.core.enums.RegistryConfig; import org.apache.commons.collections.CollectionUtils; -import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.ArrayList; -import java.util.Arrays; import java.util.Date; /** @@ -90,17 +86,10 @@ public class XxlJobTrigger { StringBuffer triggerSb = new StringBuffer(); // exerutor address list - ArrayList addressList = null; XxlJobGroup group = XxlJobDynamicScheduler.xxlJobGroupDao.load(jobInfo.getJobGroup()); - if (group.getAddressType() == 0) { - triggerSb.append("注册方式:自动注册"); - addressList = (ArrayList) JobRegistryMonitorHelper.discover(RegistryConfig.RegistType.EXECUTOR.name(), group.getAppName()); - } else { - triggerSb.append("注册方式:手动录入"); - if (StringUtils.isNotBlank(group.getAddressList())) { - addressList = new ArrayList(Arrays.asList(group.getAddressList().split(","))); - } - } + triggerSb.append( (group.getAddressType() == 0)?"注册方式:自动注册":"注册方式:手动录入" ); + ArrayList addressList = (ArrayList) group.getRegistryList(); + triggerSb.append("
阻塞处理策略:").append(ExecutorBlockStrategyEnum.match(jobInfo.getExecutorBlockStrategy(), ExecutorBlockStrategyEnum.SERIAL_EXECUTION).getTitle()); triggerSb.append("
失败处理策略:").append(ExecutorFailStrategyEnum.match(jobInfo.getExecutorBlockStrategy(), ExecutorFailStrategyEnum.FAIL_ALARM).getTitle()); triggerSb.append("
地址列表:").append(addressList!=null?addressList.toString():""); diff --git a/xxl-job-admin/src/main/java/com/xxl/job/admin/dao/IXxlJobGroupDao.java b/xxl-job-admin/src/main/java/com/xxl/job/admin/dao/IXxlJobGroupDao.java index 94957e03..5ecb9efe 100644 --- a/xxl-job-admin/src/main/java/com/xxl/job/admin/dao/IXxlJobGroupDao.java +++ b/xxl-job-admin/src/main/java/com/xxl/job/admin/dao/IXxlJobGroupDao.java @@ -11,6 +11,8 @@ public interface IXxlJobGroupDao { public List findAll(); + public List findByAddressType(int addressType); + public int save(XxlJobGroup xxlJobGroup); public int update(XxlJobGroup xxlJobGroup); diff --git a/xxl-job-admin/src/main/java/com/xxl/job/admin/dao/impl/XxlJobGroupDaoImpl.java b/xxl-job-admin/src/main/java/com/xxl/job/admin/dao/impl/XxlJobGroupDaoImpl.java index b9e11340..de1e3224 100644 --- a/xxl-job-admin/src/main/java/com/xxl/job/admin/dao/impl/XxlJobGroupDaoImpl.java +++ b/xxl-job-admin/src/main/java/com/xxl/job/admin/dao/impl/XxlJobGroupDaoImpl.java @@ -22,6 +22,11 @@ public class XxlJobGroupDaoImpl implements IXxlJobGroupDao { return sqlSessionTemplate.selectList("XxlJobGroupMapper.findAll"); } + @Override + public List findByAddressType(int addressType) { + return sqlSessionTemplate.selectList("XxlJobGroupMapper.findByAddressType", addressType); + } + @Override public int save(XxlJobGroup xxlJobGroup) { return sqlSessionTemplate.update("XxlJobGroupMapper.save", xxlJobGroup); diff --git a/xxl-job-admin/src/main/java/com/xxl/job/admin/service/impl/XxlJobServiceImpl.java b/xxl-job-admin/src/main/java/com/xxl/job/admin/service/impl/XxlJobServiceImpl.java index b36c2835..67ab1345 100644 --- a/xxl-job-admin/src/main/java/com/xxl/job/admin/service/impl/XxlJobServiceImpl.java +++ b/xxl-job-admin/src/main/java/com/xxl/job/admin/service/impl/XxlJobServiceImpl.java @@ -5,7 +5,6 @@ import com.xxl.job.admin.core.model.XxlJobGroup; import com.xxl.job.admin.core.model.XxlJobInfo; import com.xxl.job.admin.core.route.ExecutorRouteStrategyEnum; import com.xxl.job.admin.core.schedule.XxlJobDynamicScheduler; -import com.xxl.job.admin.core.thread.JobRegistryMonitorHelper; import com.xxl.job.admin.dao.IXxlJobGroupDao; import com.xxl.job.admin.dao.IXxlJobInfoDao; import com.xxl.job.admin.dao.IXxlJobLogDao; @@ -13,7 +12,6 @@ import com.xxl.job.admin.dao.IXxlJobLogGlueDao; import com.xxl.job.admin.service.IXxlJobService; import com.xxl.job.core.biz.model.ReturnT; import com.xxl.job.core.enums.ExecutorBlockStrategyEnum; -import com.xxl.job.core.enums.RegistryConfig; import com.xxl.job.core.glue.GlueTypeEnum; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang.StringUtils; @@ -288,21 +286,15 @@ public class XxlJobServiceImpl implements IXxlJobService { // executor count Set executerAddressSet = new HashSet(); List groupList = xxlJobGroupDao.findAll(); + if (CollectionUtils.isNotEmpty(groupList)) { for (XxlJobGroup group: groupList) { - List registryList = null; - if (group.getAddressType() == 0) { - registryList = JobRegistryMonitorHelper.discover(RegistryConfig.RegistType.EXECUTOR.name(), group.getAppName()); - } else { - if (StringUtils.isNotBlank(group.getAddressList())) { - registryList = Arrays.asList(group.getAddressList().split(",")); - } - } - if (CollectionUtils.isNotEmpty(registryList)) { - executerAddressSet.addAll(registryList); + if (CollectionUtils.isNotEmpty(group.getRegistryList())) { + executerAddressSet.addAll(group.getRegistryList()); } } } + int executorCount = executerAddressSet.size(); Map dashboardMap = new HashMap(); diff --git a/xxl-job-admin/src/main/resources/mybatis-mapper/XxlJobGroupMapper.xml b/xxl-job-admin/src/main/resources/mybatis-mapper/XxlJobGroupMapper.xml index 0cb1efbc..a21a4366 100644 --- a/xxl-job-admin/src/main/resources/mybatis-mapper/XxlJobGroupMapper.xml +++ b/xxl-job-admin/src/main/resources/mybatis-mapper/XxlJobGroupMapper.xml @@ -21,12 +21,19 @@ t.address_list - SELECT FROM XXL_JOB_QRTZ_TRIGGER_GROUP AS t ORDER BY t.order ASC + + INSERT INTO XXL_JOB_QRTZ_TRIGGER_GROUP ( `app_name`, `title`, `order`, `address_type`, `address_list`) values ( #{appName}, #{title}, #{order}, #{addressType}, #{addressList});