Spring boot集成xxl-job以及动态任务配置
拉取项目
从git上拉取xxl-job到本地:
https://gitee.com/xuxueli0323/xxl-job.git
修改配置启动项目
xxl-job的包结构如下:
修改xxl-job的配置信息
2、配置令牌
3、启动xxl-job-admin控制台,通过http://localhost:8080/xxl-job-admin 访问,
默认用户名:admin
密码:123456
在自己的项目中集成xxl-job
1、在自己的项目中导入xxl-job的依赖,比如:
com.xuxueli xxl-job-core 2.3.0
2、将xxl-job中提供的配置复制到自己项目中
这个配置类原封不动复制过来就可以,有需要也可以按需修改,同时需要在自己的项目中配置xxl-job的信息,特别注意,如果你在 xxl-job-admin的配置中修改了 配置的 accessToken 一定要和之前修改到的一致,否则在自己的项目会看到注册失败的日志,同时在xxl-job的控制台也无法调度你的任务。
3、在自己项目中需要定时执行的方法上加上@XxlJob(“任务名”)
括号里面的是自己定义的任务名,在执行的时候需要用到
4、配置执行器
appName就是在自己项目中配置的appname
名称 可以自定义
注册方式可以手动也可以自动,自动注册就是通过配置的信息注册,无特别需求可以使用自动注册
5、注册定时任务
配置好之后就可以 直接启动定时任务,然后在自己的项目中看到效果了
在自己项目中动态注册定时任务
XXL-job 在xxl-job-admin的 JobInfoController 中提供了一系列的新增、修改、删除、启动、停止等方法,但是这些方法是需要登录才能够调用,XXL-job本身提供了越过登录校验的方法。
在接口上加上@PermissionLimt(limt = false) (没记错的话是这个注解)就可以跳过登录,但是这样存在一定安全风险。
建议先调用登录接口,获取cookie,然后缓存下来,在调用XXL-job接口的时候就可以带上。
参考文章xxl-job动态配置任务,彻底解放双手
下面给出部分调度任务的demo
public class XxlJobUtil { // 此处的name 是xxl-job 存入cookie时设置的名字 必须一致 private static final String cookieName = "XXL_JOB_LOGIN_IDENTITY"; private static String getCookie() { return login(); } /** * 登录方法 * 可以将获取到的cookie缓存 * @return cookie */ private static String login() { String url = "http://xxxxx/xxl-job-admin/login"; String userName = "xxx"; String password = "xxx@xx"; try ( HttpResponse response = HttpUtil.createPost(url) .header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8") .form("userName", userName) .form("password", password).execute(); ){ if (response.getStatus() != 200) { throw new RuntimeException("登录xxl-job失败!网络请求异常,状态码:" + response.getStatus()); } String setCookie = response.header("Set-Cookie"); // 提取 XXL_JOB_LOGIN_IDENTITY cookie if (setCookie != null) { String[] cookies = setCookie.split(";"); for (String cookie : cookies) { if (cookie != null && cookie.trim().startsWith(cookieName)) { return cookie.split("=")[1]; } } } throw new RuntimeException("登录xxl-job失败,未获取到cookie"); } catch (Exception e) { throw new RuntimeException("登录xxl-job失败," + e.getMessage()); } } /** * 启动 */ public static boolean start(int id) { String url = "http://xxxxx/xxl-job-admin/jobinfo/start"; try ( HttpResponse response = HttpUtil.createPost(url) .header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8") .header("Cookie",String.format("%s=%s", cookieName, getCookie())) .form("id", id).execute() ) { if (response.getStatus() == 200) { JSONObject body = JSON.parseObject(response.body()); Integer code = body.getInteger("code"); if (code == 200) { // 调度成功 return true; } } } catch (Exception e) { throw new RuntimeException("定时任务调度失败," + e.getMessage()); } return false; } /** * 停止 */ public static boolean stop(int id) { String url = "http://xxxxxx/xxl-job-admin/jobinfo/stop"; try ( HttpResponse response = HttpUtil.createPost(url) .header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8") .header("Cookie",String.format("%s=%s", cookieName, getCookie())) .form("id", id).execute() ) { if (response.getStatus() == 200) { JSONObject body = JSON.parseObject(response.body()); Integer code = body.getInteger("code"); if (code == 200) { // 调度成功 return true; } } } catch (Exception e) { throw new RuntimeException("定时任务调度失败," + e.getMessage()); } return false; } /** * 执行一次 * @param id 任务id * @param executorParam 任务参数 非必填 * @param addressList 机器地址 多个地址用逗号分隔 不填从执行器中获取 */ public static boolean trigger(int id, String executorParam, String addressList) { String url = "http://xxxxx/xxl-job-admin/jobinfo/trigger"; try ( HttpResponse response = HttpUtil.createPost(url) .header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8") .header("Cookie",String.format("%s=%s", cookieName, getCookie())) .form("id", id) .form("executorParam", executorParam) .form("addressList", addressList).execute() ){ if (response.getStatus() == 200) { JSONObject body = JSON.parseObject(response.body()); Integer code = body.getInteger("code"); if (code == 200) { // 调度成功 return true; } } } catch (Exception e) { throw new RuntimeException("定时任务调度失败!" + e.getMessage()); } return false; } }