0
点赞
收藏
分享

微信扫一扫

xxl_job系列---【Glue(java)模式如何通过动态参数传参?】

Go_Viola 01-13 12:00 阅读 34

1.编辑GLUE(Java)模式的定时任务

这里以传递json参数为例:
修改任务参数:{"startDate": "","endDate": "","desc": "入参日期格式:yyyyMMdd"}
保存。

2.编辑此定时任务的GLUE脚本

import添加:
import com.xxl.job.core.context.XxlJobHelper;
import cn.hutool.core.util.StrUtil;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

excute()方法里接收参数:

String jobParam = XxlJobHelper.getJobParam();
XxlJobHelper.log("自定义入参:"+jobParam);

String startDateStr = null;
String endDayStr = null;
if(!StrUtil.isEmptyIfStr(jobParam)){
  JSONObject jsonObject = new JSONObject(jobParam);
  startDayStr = jsonObject.getStr("startDate");
  endDayStr = jsonObject.getStr("endDate");
}

if(StrUtil.isEmptyIfStr(startDateStr) || StrUtil.isEmptyIfStr(endDateStr)){
  LocalDate todayDate = LocalDate.now();
  DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMMdd");
  startDateStr = todayDate.minusDays(2).format(formatter);
  endDateStr = todayDate.format(formatter);
}
...省略

举报

相关推荐

0 条评论