魔改xxl-job,彻底告别手动配置任务!( 二 )

1、接口调用在调用调度中心的接口前,先把xxl-job-admin模块中的XxlJobInfoXxlJobGroup这两个类拿到我们的starter项目中 , 用于接收接口调用的结果 。
登录接口创建一个JobLoginService,在调用业务接口前 , 需要通过登录接口获取cookie,并在获取到cookie后,缓存到本地的Map中 。
private final Map<String,String> loginCookie=new HashMap<>();public void login() {String url=adminAddresses+"/login";HttpResponse response = HttpRequest.post(url).form("userName",username).form("password",password).execute();List<HttpCookie> cookies = response.getCookies();Optional<HttpCookie> cookieOpt = cookies.stream().filter(cookie -> cookie.getName().equals("XXL_JOB_LOGIN_IDENTITY")).findFirst();if (!cookieOpt.isPresent())throw new RuntimeException("get xxl-job cookie error!");String value = https://www.huyubaike.com/biancheng/cookieOpt.get().getValue();loginCookie.put("XXL_JOB_LOGIN_IDENTITY",value);}其他接口在调用时,直接从缓存中获取cookie,如果缓存中不存在则调用/login接口 , 为了避免这一过程失败,允许最多重试3次 。
public String getCookie() {for (int i = 0; i < 3; i++) {String cookieStr = loginCookie.get("XXL_JOB_LOGIN_IDENTITY");if (cookieStr !=null) {return "XXL_JOB_LOGIN_IDENTITY="+cookieStr;}login();}throw new RuntimeException("get xxl-job cookie error!");}执行器接口创建一个JobGroupService,根据appName和执行器名称title查询执行器列表:
public List<XxlJobGroup> getJobGroup() {String url=adminAddresses+"/jobgroup/pageList";HttpResponse response = HttpRequest.post(url).form("appname", appName).form("title", title).cookie(jobLoginService.getCookie()).execute();String body = response.body();JSONArray array = JSONUtil.parse(body).getByPath("data", JSONArray.class);List<XxlJobGroup> list = array.stream().map(o -> JSONUtil.toBean((JSONObject) o, XxlJobGroup.class)).collect(Collectors.toList());return list;}我们在后面要根据配置文件中的appNametitle判断当前执行器是否已经被注册到调度中心过,如果已经注册过那么则跳过 , 而/jobgroup/pageList接口是一个模糊查询接口 , 所以在查询列表的结果列表中,还需要再进行一次精确匹配 。
public boolean preciselyCheck() {List<XxlJobGroup> jobGroup = getJobGroup();Optional<XxlJobGroup> has = jobGroup.stream().filter(xxlJobGroup -> xxlJobGroup.getAppname().equals(appName)&& xxlJobGroup.getTitle().equals(title)).findAny();return has.isPresent();}注册新executor到调度中心:
public boolean autoRegisterGroup() {String url=adminAddresses+"/jobgroup/save";HttpResponse response = HttpRequest.post(url).form("appname", appName).form("title", title).cookie(jobLoginService.getCookie()).execute();Object code = JSONUtil.parse(response.body()).getByPath("code");return code.equals(200);}任务接口创建一个JobInfoService,根据执行器id , jobHandler名称查询任务列表,和上面一样,也是模糊查询:
public List<XxlJobInfo> getJobInfo(Integer jobGroupId,String executorHandler) {String url=adminAddresses+"/jobinfo/pageList";HttpResponse response = HttpRequest.post(url).form("jobGroup", jobGroupId).form("executorHandler", executorHandler).form("triggerStatus", -1).cookie(jobLoginService.getCookie()).execute();String body = response.body();JSONArray array = JSONUtil.parse(body).getByPath("data", JSONArray.class);List<XxlJobInfo> list = array.stream().map(o -> JSONUtil.toBean((JSONObject) o, XxlJobInfo.class)).collect(Collectors.toList());return list;}注册一个新任务 , 最终返回创建的新任务的id
public Integer addJobInfo(XxlJobInfo xxlJobInfo) {String url=adminAddresses+"/jobinfo/add";Map<String, Object> paramMap = BeanUtil.beanToMap(xxlJobInfo);HttpResponse response = HttpRequest.post(url).form(paramMap).cookie(jobLoginService.getCookie()).execute();JSON json = JSONUtil.parse(response.body());Object code = json.getByPath("code");if (code.equals(200)){return Convert.toInt(json.getByPath("content"));}throw new RuntimeException("add jobInfo error!");}2、创建新注解在创建任务时,必填字段除了执行器和jobHandler之外,还有任务描述、负责人、Cron表达式、调度类型、运行模式 。在这里,我们默认调度类型为CRON、运行模式为BEAN , 另外的3个字段的信息需要用户指定 。
因此我们需要创建一个新注解@XxlRegister

推荐阅读