官网 Github springboot项目地址
需求: 现有多个用户 每个用户的生日当天给该用户发送短息祝福
发送祝福前提
1:知道该用户 id 【即可获取用户信息】
2:延迟任务指定时间 自动发送短信
发送短信可以作为公共服务 只需要动态获取用户id即可
package com.xxl.job.executor.service.myjobhandler;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MyJobHandler extends IJobHandler {
//用户id
private String userid;
public MyJobHandler(String userid) {
this.userid = userid;
}
public ReturnT<String> execute(String s) throws Exception {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String date = simpleDateFormat.format(new Date());
System.out.println("当前时间:"+date);
//1 拿到用户 id 查询用户信息
// 执行业务逻辑
// sendMsg(userid)
System.out.println("给用户【"+this.userid+"】"+"发送祝福");
return ReturnT.SUCCESS;
}
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
}
package com.xxl.job.executor.service.myjobhandler;
import com.xxl.job.core.executor.XxlJobExecutor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class App {
@ResponseBody
@RequestMapping(value = "/register")
public void registerJob() {
//这里 注册了 两个用户的任务
XxlJobExecutor.registJobHandler("myJobHandler_user1", new MyJobHandler("user1"));
XxlJobExecutor.registJobHandler("myJobHandler_user2", new MyJobHandler("user2"));
}
}
可以通过http请求 admin项目 来实现 任务管理器的生成
在admin项目中,有一个JobInfoController, 这个类就是处理各种新增任务,修改任务,触发任务的Controller
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-http</artifactId>
<version>5.4.7</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.75</version>
</dependency>
package com.xxl.job.executor.service.myjobhandler;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpStatus;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xxl.job.core.executor.XxlJobExecutor;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.PostConstruct;
import java.net.HttpCookie;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 程序员 by dell
* time 2020-11-16
**/
@Controller
public class App {
@ResponseBody
@RequestMapping(value = "/register")
public void registerJob() throws ParseException {
//这里 注册了 两个用户的任务
XxlJobExecutor.registJobHandler("myJobHandler_user1", new MyJobHandler("user1"));
XxlJobExecutor.registJobHandler("myJobHandler_user2", new MyJobHandler("user2"));
String strDate1 = "2020-11-16 21:30";
String strDate2 = "2020-11-16 21:31";
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date date1 = simpleDateFormat.parse(strDate1);
Date date2 = simpleDateFormat.parse(strDate2);
//启动任务
addXxlJob(getCron(date1), "myJobHandler_user1");
//启动任务
addXxlJob(getCron(date2), "myJobHandler_user2");
}
/**
* 访问 admin项目 来实现 任务管理器的生成
*
* @return
*/
public int addXxlJob(String cron, String messageJob) {
String path = "http://127.0.0.1:7391/xxl-job-admin/jobinfo/add";
Map<String, Object> paramMap = new HashMap<String, Object>();
paramMap.put("jobGroup", 1); // 你的执行器 id
paramMap.put("jobDesc", "这是测试任务"); //任务描述
paramMap.put("executorRouteStrategy", "FIRST");//路由策略*
paramMap.put("cronGen_display", cron);//Cron*
paramMap.put("jobCron", cron); // 任务执行CRON表达式
paramMap.put("glueType", "BEAN");//运行模式*
paramMap.put("executorHandler", messageJob); // 此处hander需提前在项目中定义
paramMap.put("executorBlockStrategy", "SERIAL_EXECUTION"); //阻塞处理策略
paramMap.put("executorTimeout", 0);//任务超时时间*
paramMap.put("executorFailRetryCount", 1);//失败重试次数*
paramMap.put("author", "admin");//负责人*
paramMap.put("glueRemark", "GLUE代码初始化");// GLUE备注
paramMap.put("triggerStatus", 1);// 调度状态:0-停止,1-运行
String cookie = getCookie();
HttpResponse response = HttpRequest.post(path).form(paramMap).execute();
if (HttpStatus.HTTP_OK != response.getStatus()) {
// TODO
}
JSONObject jsonObject = JSON.parseObject(response.body());
int jobId = jsonObject.getIntValue("content");
return jobId;
}
/**
* 获取admin项目 登录的cookie
*
* @return
*/
public String getCookie() {
String path = "http://127.0.0.1:7391/xxl-job-admin" + "/login";
Map<String, Object> hashMap = new HashMap();
hashMap.put("userName", "admin");
hashMap.put("password", "123456");
HttpResponse response = HttpRequest.post(path).form(hashMap).execute();
List<HttpCookie> cookies = response.getCookies();
StringBuilder sb = new StringBuilder();
for (HttpCookie cookie : cookies) {
sb.append(cookie.toString());
}
String cookie = sb.toString();
return cookie;
}
/***
* 生成 日期对应的 cron表达式
* convert Date to cron ,eg. "0 06 10 15 1 ? 2014"
* @param date : 时间点
* @return
*/
public static String getCron(Date date) {
String dateFormat = "ss mm HH dd MM ? yyyy";
return formatDateByPattern(date, dateFormat);
}
/***
*
* @param date
* @param dateFormat : e.g:yyyy-MM-dd HH:mm:ss
* @return
*/
public static String formatDateByPattern(Date date, String dateFormat) {
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
String formatTimeStr = null;
if (date != null) {
formatTimeStr = sdf.format(date);
}
return formatTimeStr;
}
}
http://localhost:8080/register
解决思路 :
1: 创建定时任务 每小时一次 将数据 数据 将数据放入 List list=new ArrayList();
2: 创建定时任务 没一秒执行一次 遍历 list中的数据 判断时间发送短信
第二篇文章 实现优化