赞
踩
定时任务就是给程序设定一个时间,到了时间程序就会根据你设定的参数去执行程序。
定时任务有很多的应用场景,越来越多的公司开始依靠定时任务去处理很多事务,如在夜里刷数据,定时发送邮件或者每隔一段时间对部分数据进行一次拉取以及实现一些补偿机制等等。灵活的使用定时任务可以处理很多事情,事半功倍。
XXL-JOB是一个基于SpringBoot框架的开源的分布式的任务调度平台,在GitHub上持续保持着很高的热度。部署即用。
他可以通过添加定时任务并进行一些简单配置的方式,让程序在指定的时间单次或循环调用指定的接口,并传入相应的参数,在执行结束后,会将程序的执行结果以指定的格式展示出来,方便对执行的结果进行了解。
话不多说,开始使用!
从GitHub上clone源码
在mysql中创建数据库,并运行在/doc/db目录下的sql语句,创建表结构。
修改xxl-job-admin/src/main/resources下的application.properties文件,将其中的数据库信息修改为自己的。
### xxl-job, datasource
spring.datasource.url=jdbc:mysql://127..0.0.1:3306/xxl_job?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
直接启动xxl-job-admin/src/main/java下的XxlJobAdminApplication.java。启动成功后访问:
默认的登陆账号密码为:admin/123456
进入如下页面便登陆成功了
在任务管理处点击新增,添加必要信息,即可创建。
cron表达式不会写可以使用cron表达式生成器来生成。图中所示的"0/30 * * * * ?"为每隔30秒执行一次。
参数部分写成json的格式
编写xxl-job的方式有很多,这里采用给方法添加@XxlJob注解的方式,这种方式最为便捷。
新建一个springboot项目,加入xxl-job和fastJson的依赖
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>
修改application.properties的配置如下:
# web port server.port=8081 # no web #spring.main.web-environment=false ### xxl-job admin address list, such as "http://address" or "http://address01,http://address02" xxl.job.admin.addresses=http://127.0.0.1:8080/xxl-job-admin ### xxl-job, access token xxl.job.accessToken=default_token ### xxl-job executor appname xxl.job.executor.appname=xxl-job-executor-sample ### xxl-job executor registry-address: default use address to registry , otherwise use ip:port if address is null xxl.job.executor.address= ### xxl-job executor server-info xxl.job.executor.ip= xxl.job.executor.port=9999 ### xxl-job executor log-path xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler ### xxl-job executor log-retention-days xxl.job.executor.logretentiondays=30
添加一个xxl-job的配置类:
package com.config; import com.xxl.job.core.executor.impl.XxlJobSpringExecutor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * xxl-job config * * @author xuxueli 2017-04-28 */ @Configuration public class XxlJobConfig { private Logger logger = LoggerFactory.getLogger(XxlJobConfig.class); @Value("${xxl.job.admin.addresses}") private String adminAddresses; @Value("${xxl.job.accessToken}") private String accessToken; @Value("${xxl.job.executor.appname}") private String appname; @Value("${xxl.job.executor.address}") private String address; @Value("${xxl.job.executor.ip}") private String ip; @Value("${xxl.job.executor.port}") private int port; @Value("${xxl.job.executor.logpath}") private String logPath; @Value("${xxl.job.executor.logretentiondays}") private int logRetentionDays; @Bean public XxlJobSpringExecutor xxlJobExecutor() { logger.info(">>>>>>>>>>> xxl-job config init."); XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor(); xxlJobSpringExecutor.setAdminAddresses(adminAddresses); xxlJobSpringExecutor.setAppname(appname); xxlJobSpringExecutor.setAddress(address); xxlJobSpringExecutor.setIp(ip); xxlJobSpringExecutor.setPort(port); xxlJobSpringExecutor.setAccessToken(accessToken); xxlJobSpringExecutor.setLogPath(logPath); xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays); return xxlJobSpringExecutor; } /** * 针对多网卡、容器内部署等情况,可借助 "spring-cloud-commons" 提供的 "InetUtils" 组件灵活定制注册IP; * * 1、引入依赖: * <dependency> * <groupId>org.springframework.cloud</groupId> * <artifactId>spring-cloud-commons</artifactId> * <version>${version}</version> * </dependency> * * 2、配置文件,或者容器启动变量 * spring.cloud.inetutils.preferred-networks: 'xxx.xxx.xxx.' * * 3、获取IP * String ip_ = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress(); */ }
编写一个真正的定时任务,@XxlJob()中填写刚刚上面加入的定时任务JobHandler
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.TypeReference; import com.xxl.job.core.biz.model.ReturnT; import com.xxl.job.core.context.XxlJobHelper; import com.xxl.job.core.handler.annotation.XxlJob; import org.springframework.stereotype.Component; import java.util.Map; @Component public class Test { @XxlJob("test1") public ReturnT<String> test1()throws Exception{ String jobParam = XxlJobHelper.getJobParam(); XxlJobHelper.log("获取的参数是:"+jobParam); Map stringStringMap = JSON.parseObject(jobParam,new TypeReference<Map<String, String>>(){}); ReturnT<String> returnT = ReturnT.SUCCESS; returnT.setMsg("执行完毕,获取的参数是:"+stringStringMap.toString()); return returnT; } }
启动项目,项目启动成功后,该定时任务就可以被执行器调用了。
启动任务
因为设置的是每30秒执行一次,通过查看操作中的查询日志可以查看任务的执行结果。
点击调度备注,即可看到我们编辑的返回值已经展示在了备注中。
在实际应用中也可以手动执行,并且可以修改执行的参数
在不需要定时任务执行时只需要将该任务停止即可
在项目实际的应用中,结合业务会发现很多情况需要在代码中添加或者修改任务,这就需要我们在代码中取调用XXL-JOB的api接口来实现。
在xxl-job-admin中,JobInfoController.java这个类中的接口是对定时任务的正删改查接口。
/** * index controller * @author xuxueli 2015-12-19 16:13:16 */ @Controller @RequestMapping("/jobinfo") public class JobInfoController { private static Logger logger = LoggerFactory.getLogger(JobInfoController.class); @Resource private XxlJobGroupDao xxlJobGroupDao; @Resource private XxlJobService xxlJobService; @RequestMapping public String index(HttpServletRequest request, Model model, @RequestParam(required = false, defaultValue = "-1") int jobGroup) { // 枚举-字典 model.addAttribute("ExecutorRouteStrategyEnum", ExecutorRouteStrategyEnum.values()); // 路由策略-列表 model.addAttribute("GlueTypeEnum", GlueTypeEnum.values()); // Glue类型-字典 model.addAttribute("ExecutorBlockStrategyEnum", ExecutorBlockStrategyEnum.values()); // 阻塞处理策略-字典 model.addAttribute("ScheduleTypeEnum", ScheduleTypeEnum.values()); // 调度类型 model.addAttribute("MisfireStrategyEnum", MisfireStrategyEnum.values()); // 调度过期策略 // 执行器列表 List<XxlJobGroup> jobGroupList_all = xxlJobGroupDao.findAll(); // filter group List<XxlJobGroup> jobGroupList = filterJobGroupByRole(request, jobGroupList_all); if (jobGroupList==null || jobGroupList.size()==0) { throw new XxlJobException(I18nUtil.getString("jobgroup_empty")); } model.addAttribute("JobGroupList", jobGroupList); model.addAttribute("jobGroup", jobGroup); return "jobinfo/jobinfo.index"; } public static List<XxlJobGroup> filterJobGroupByRole(HttpServletRequest request, List<XxlJobGroup> jobGroupList_all){ List<XxlJobGroup> jobGroupList = new ArrayList<>(); if (jobGroupList_all!=null && jobGroupList_all.size()>0) { XxlJobUser loginUser = (XxlJobUser) request.getAttribute(LoginService.LOGIN_IDENTITY_KEY); if (loginUser.getRole() == 1) { jobGroupList = jobGroupList_all; } else { List<String> groupIdStrs = new ArrayList<>(); if (loginUser.getPermission()!=null && loginUser.getPermission().trim().length()>0) { groupIdStrs = Arrays.asList(loginUser.getPermission().trim().split(",")); } for (XxlJobGroup groupItem:jobGroupList_all) { if (groupIdStrs.contains(String.valueOf(groupItem.getId()))) { jobGroupList.add(groupItem); } } } } return jobGroupList; } public static void validPermission(HttpServletRequest request, int jobGroup) { XxlJobUser loginUser = (XxlJobUser) request.getAttribute(LoginService.LOGIN_IDENTITY_KEY); if (!loginUser.validPermission(jobGroup)) { throw new RuntimeException(I18nUtil.getString("system_permission_limit") + "[username="+ loginUser.getUsername() +"]"); } } @RequestMapping("/pageList") @ResponseBody public Map<String, Object> pageList(@RequestParam(required = false, defaultValue = "0") int start, @RequestParam(required = false, defaultValue = "10") int length, int jobGroup, int triggerStatus, String jobDesc, String executorHandler, String author) { return xxlJobService.pageList(start, length, jobGroup, triggerStatus, jobDesc, executorHandler, author); } @RequestMapping("/add") @ResponseBody public ReturnT<String> add(XxlJobInfo jobInfo) { return xxlJobService.add(jobInfo); } @RequestMapping("/update") @ResponseBody public ReturnT<String> update(XxlJobInfo jobInfo) { return xxlJobService.update(jobInfo); } @RequestMapping("/remove") @ResponseBody public ReturnT<String> remove(int id) { return xxlJobService.remove(id); } @RequestMapping("/stop") @ResponseBody public ReturnT<String> pause(int id) { return xxlJobService.stop(id); } @RequestMapping("/start") @ResponseBody public ReturnT<String> start(int id) { return xxlJobService.start(id); } @RequestMapping("/trigger") @ResponseBody //@PermissionLimit(limit = false) public ReturnT<String> triggerJob(int id, String executorParam, String addressList) { // force cover job param if (executorParam == null) { executorParam = ""; } JobTriggerPoolHelper.trigger(id, TriggerTypeEnum.MANUAL, -1, null, executorParam, addressList); return ReturnT.SUCCESS; } @RequestMapping("/nextTriggerTime") @ResponseBody public ReturnT<List<String>> nextTriggerTime(String scheduleType, String scheduleConf) { XxlJobInfo paramXxlJobInfo = new XxlJobInfo(); paramXxlJobInfo.setScheduleType(scheduleType); paramXxlJobInfo.setScheduleConf(scheduleConf); List<String> result = new ArrayList<>(); try { Date lastTime = new Date(); for (int i = 0; i < 5; i++) { lastTime = JobScheduleHelper.generateNextValidTime(paramXxlJobInfo, lastTime); if (lastTime != null) { result.add(DateUtil.formatDateTime(lastTime)); } else { break; } } } catch (Exception e) { logger.error(e.getMessage(), e); return new ReturnT<List<String>>(ReturnT.FAIL_CODE, (I18nUtil.getString("schedule_type")+I18nUtil.getString("system_unvalid")) + e.getMessage()); } return new ReturnT<List<String>>(result); } }
但是在该类中,所有的接口都必须经过权限校验,也就是必须登陆才可使用。若想在调用时不经过权限验证,可以将该类复制一份,对接口的访问路径做一些修改,然后在每个接口上添加注解@PermissionLimit(limit = false)即可。
其他详细的使用方法请参考XXL-JOB官方文档:https://www.xuxueli.com/xxl-job/#2.3%20%E9%85%8D%E7%BD%AE%E9%83%A8%E7%BD%B2%E2%80%9C%E8%B0%83%E5%BA%A6%E4%B8%AD%E5%BF%83%E2%80%9D
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。