package com.xxl.job.admin.controller; import com.xxl.job.admin.core.model.XxlJobGroup; import com.xxl.job.admin.core.model.XxlJobRegistry; import com.xxl.job.admin.core.util.I18nUtil; import com.xxl.job.admin.service.IXxlJobGroupService; import com.xxl.job.admin.service.IXxlJobInfoService; import com.xxl.job.admin.service.IXxlJobRegistryService; import com.xxl.job.core.biz.model.ReturnT; import com.xxl.job.core.enums.RegistryConfig; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; import java.util.*; /** * job group controller * * @author xuxueli 2016-10-02 20:52:56 */ @Controller @RequestMapping("/jobgroup") public class JobGroupController { @Resource public IXxlJobInfoService xxlJobInfoService; @Resource public IXxlJobGroupService xxlJobGroupService; @Resource private IXxlJobRegistryService xxlJobRegistryService; @RequestMapping public String index(Model model) { // job group (executor) List list = xxlJobGroupService.list(); model.addAttribute("list", list); return "jobgroup/jobgroup.index"; } @RequestMapping("/save") @ResponseBody public ReturnT save(XxlJobGroup xxlJobGroup) { // valid if (xxlJobGroup.getAppName() == null || xxlJobGroup.getAppName().trim().length() == 0) { return new ReturnT(500, (I18nUtil.getString("system_please_input") + "AppName")); } if (xxlJobGroup.getAppName().length() < 4 || xxlJobGroup.getAppName().length() > 64) { return new ReturnT(500, I18nUtil.getString("jobgroup_field_appName_length")); } if (xxlJobGroup.getTitle() == null || xxlJobGroup.getTitle().trim().length() == 0) { return new ReturnT(500, (I18nUtil.getString("system_please_input") + I18nUtil.getString("jobgroup_field_title"))); } if (xxlJobGroup.getAddressType() != 0) { if (xxlJobGroup.getAddressList() == null || xxlJobGroup.getAddressList().trim().length() == 0) { return new ReturnT(500, I18nUtil.getString("jobgroup_field_addressType_limit")); } String[] addresss = xxlJobGroup.getAddressList().split(","); for (String item : addresss) { if (item == null || item.trim().length() == 0) { return new ReturnT(500, I18nUtil.getString("jobgroup_field_registryList_unvalid")); } } } boolean ret = xxlJobGroupService.save(xxlJobGroup); return (ret) ? ReturnT.SUCCESS : ReturnT.FAIL; } @RequestMapping("/update") @ResponseBody public ReturnT update(XxlJobGroup xxlJobGroup) { // valid if (xxlJobGroup.getAppName() == null || xxlJobGroup.getAppName().trim().length() == 0) { return new ReturnT(500, (I18nUtil.getString("system_please_input") + "AppName")); } if (xxlJobGroup.getAppName().length() < 4 || xxlJobGroup.getAppName().length() > 64) { return new ReturnT(500, I18nUtil.getString("jobgroup_field_appName_length")); } if (xxlJobGroup.getTitle() == null || xxlJobGroup.getTitle().trim().length() == 0) { return new ReturnT(500, (I18nUtil.getString("system_please_input") + I18nUtil.getString("jobgroup_field_title"))); } if (xxlJobGroup.getAddressType() == 0) { // 0=自动注册 List registryList = findRegistryByAppName(xxlJobGroup.getAppName()); String addressListStr = null; if (registryList != null && !registryList.isEmpty()) { Collections.sort(registryList); addressListStr = ""; for (String item : registryList) { addressListStr += item + ","; } addressListStr = addressListStr.substring(0, addressListStr.length() - 1); } xxlJobGroup.setAddressList(addressListStr); } else { // 1=手动录入 if (xxlJobGroup.getAddressList() == null || xxlJobGroup.getAddressList().trim().length() == 0) { return new ReturnT(500, I18nUtil.getString("jobgroup_field_addressType_limit")); } String[] addresss = xxlJobGroup.getAddressList().split(","); for (String item : addresss) { if (item == null || item.trim().length() == 0) { return new ReturnT(500, I18nUtil.getString("jobgroup_field_registryList_unvalid")); } } } boolean ret = xxlJobGroupService.updateById(xxlJobGroup); return (ret) ? ReturnT.SUCCESS : ReturnT.FAIL; } private List findRegistryByAppName(String appNameParam) { HashMap> appAddressMap = new HashMap>(); List list = xxlJobRegistryService.findAll(RegistryConfig.DEAD_TIMEOUT, new Date()); 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); } } } return appAddressMap.get(appNameParam); } @RequestMapping("/remove") @ResponseBody public ReturnT remove(int id) { // valid int count = xxlJobInfoService.pageListCount(0, 10, id, -1, null, null, null); if (count > 0) { return new ReturnT(500, I18nUtil.getString("jobgroup_del_limit_0")); } List allList = xxlJobGroupService.list(); if (allList.size() == 1) { return new ReturnT(500, I18nUtil.getString("jobgroup_del_limit_1")); } boolean ret = xxlJobGroupService.removeById(id); return (ret) ? ReturnT.SUCCESS : ReturnT.FAIL; } @RequestMapping("/loadById") @ResponseBody public ReturnT loadById(int id) { XxlJobGroup jobGroup = xxlJobGroupService.getById(id); return jobGroup != null ? new ReturnT<>(jobGroup) : new ReturnT<>(ReturnT.FAIL_CODE, null); } }