xxl-job 通过定时任务来实现延迟任务
原理篇:juejin.cn/post/7172171594943430663
xxl-job在项目中,如何动态或者定义实现任务调用呢?
Xxl-job 是目前比较主流的轻量级定时任务框架,该框架以相对简单的使用方式,灵活的配置,和可读性强的源码 等优势,深得广大开发者的喜欢。让我们的定时任务实现起来变得简单。具体的使用方法这里就不介绍了,想了解的可以直接访问官网:分布式任务调度平台XXL-JOB
今天我们主要解决一下使用xxl-job中的一个痛点,就是动态创建任务。使用过xxl-job的朋友们都知道,xxl-job给我们提供一个管理后台,我们可以在管理后台上,配置执行器,配置任务,管理任务。如下图。
添加调度任务
访问接口并查看数据库
查看执行结果
代码如下:
/**
* @author zhangxiao
* @qq 490433117
* @create_date 2023/1/9 13:33
*/
package com.xxl.job.admin.controller;
import com.xxl.job.admin.controller.annotation.PermissionLimit;
import com.xxl.job.admin.core.model.XxlJobInfo;
import com.xxl.job.admin.service.XxlJobService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.glue.GlueTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
*
* @author zhangxiao
* @date 2023/1/9 13:33
*/
@RestController
@RequestMapping("/demo")
public class DemoJobInfoController {
@Autowired
private XxlJobService xxlJobService;
/**
* 添加任务
* @return
*/
@GetMapping("/addJobInfo")
@PermissionLimit(limit = false)
public ReturnT<String> addJobInfo(){
XxlJobInfo xxlJobInfo = new XxlJobInfo();
xxlJobInfo.setJobGroup(1); // 你的执行器 id
xxlJobInfo.setJobDesc("这是zx自定义测试"); // 描述
xxlJobInfo.setExecutorRouteStrategy("FIRST"); // 路由策略*
xxlJobInfo.setScheduleConf("0 0,20 15 9 1 ? 2023-2023");
xxlJobInfo.setScheduleType("CRON");
xxlJobInfo.setAuthor("zxx");
xxlJobInfo.setExecutorHandler("myJobHandler1");
xxlJobInfo.setExecutorBlockStrategy("SERIAL_EXECUTION");
xxlJobInfo.setMisfireStrategy("DO_NOTHING");
xxlJobInfo.setExecutorTimeout(0);
xxlJobInfo.setGlueRemark("beizu");
xxlJobInfo.setTriggerStatus(1);
xxlJobInfo.setGlueType(GlueTypeEnum.BEAN.getDesc());
System.out.println("执行任务开始中...");
return xxlJobService.add(xxlJobInfo);
}
/**
* 更新任务
* @param jobInfo
* @return
*/
@GetMapping("/updateJobInfo")
@PermissionLimit(limit = false)
public ReturnT<String> updateJobInfo(@RequestBody XxlJobInfo jobInfo){
return xxlJobService.update(jobInfo);
}
/**
* 删除任务
* @param jobInfo
* @return
*/
@RequestMapping("/removeJob")
@PermissionLimit(limit = false)
public ReturnT<String> removeJob(@RequestBody XxlJobInfo jobInfo) {
return xxlJobService.remove(jobInfo.getId());
}
/**
* 暂停任务
* @param jobInfo
* @return
*/
@RequestMapping("/pauseJob")
@PermissionLimit(limit = false)
public ReturnT<String> pauseJob(@RequestBody XxlJobInfo jobInfo) {
return xxlJobService.stop(jobInfo.getId());
}
/**
* 开启任务
* @param jobInfo
* @return
*/
@RequestMapping("/startJob")
@PermissionLimit(limit = false)
public ReturnT<String> startJob(@RequestBody XxlJobInfo jobInfo) {
return xxlJobService.start(jobInfo.getId());
}
/**
* 添加并启用任务
* @param jobInfo
* @return
*/
@RequestMapping("/addAndStart")
@PermissionLimit(limit = false)
public ReturnT<String> addAndStart(@RequestBody XxlJobInfo jobInfo) {
ReturnT<String> result = xxlJobService.add(jobInfo);
int id = Integer.valueOf(result.getContent());
xxlJobService.start(id);
return result;
}
}
/**
* @author zhangxiao
* @qq 490433117
* @create_date 2023/1/9 10:26
*/
package com.xxl.job.executor.service.jobhandler;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.stereotype.Component;
/**
*
* @author zhangxiao
* @date 2023/1/9 10:26
*/
@Component
public class MyJobHandler {
@XxlJob("myJobHandler1")
public void execute() throws Exception {
System.out.println("我们不能失去信仰");
}
}
### 原理篇:xxl-job 任务触发流程
https://www.jianshu.com/p/ac32cd07dea5
参考地址:www.cnblogs.com/MrYuChen-Blog/p/14...
本作品采用《CC 协议》,转载必须注明作者和本文链接