当前位置:网站首页>0612~quartz定时器框架
0612~quartz定时器框架
2022-07-24 17:43:00 【生活可真难啊】
今天老师讲的定时器框架,quartz~简单易学 但不适合高并发;
quartz主要由:1.trigger触发器,
2.job detail 业务详情,
两部分组成scheduler容器;
适用场景: 1.定时发邮件;
2.定时发报表;
3.定时关闭支付订单;
4.一系列定时操作都可以完成,功能强大;
实战关闭订单的功能实现;
1.配置动态jobDetai~动态trigg触发器;
2.配置讲时间转化成cron表达式的类;
3.配置关闭定时器的类;
4.直接在业务层调用此方法,配置参数 ;
5.scheduler实现业务超时关闭
1.配置动态jobDetai~动态trigg触发器;
*/
public class QuartzUtils {
private static String JOB_GROUP_NAME = "JOB_GROUP_SYSTEM";
private static String TRIGGER_GROUP_NAME = "TRIGGER_GROUP_SYSTEM";
/**
* @Description: 添加一个定时任务,使用默认的任务组名,触发器名,触发器组名
*
* @param sched
* 调度器
*
* @param jobName
* 任务名
* @param cls
* 任务
* @param params
* 任务参数
* @param time
* 时间设置,参考quartz说明文档
*
* @Title: QuartzManager.java
*/
public static void addJob(Scheduler sched, String jobName, Class cls, Object params,
String time) {
try {
//--------------------------创建 JobDetail 工作详情---------------------------------
JobKey jobKey = new JobKey(jobName, JOB_GROUP_NAME);// 任务名,任务组,任务执行类
//Job的数据 map
JobDataMap jobDataMap = new JobDataMap();
jobDataMap.put("params", params);
//根据传入的job的类,创建一个 JobDetail,并指定 JobKey,并把数据map设置给job
JobDetail jobDetail = newJob(cls).withIdentity(jobKey).setJobData(jobDataMap).build();
//--------------------------创建 Trigger 触发器---------------------------------
//创建触发器的 key,相当于触发器的ID
TriggerKey triggerKey = new TriggerKey(jobName, TRIGGER_GROUP_NAME);
//创建触发器,设置触发器key,并关联一个 cronSchedule(基于表达式的时间规则)
Trigger trigger = newTrigger().withIdentity(triggerKey).withSchedule(cronSchedule(time)).build();
//把工作详情和触发器设置到 schedule 调度容器中
sched.scheduleJob(jobDetail, trigger);
//启动
if (!sched.isShutdown()) {
sched.start();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}2.配置讲时间转化成cron表达式的类
//把时间变成时间表达式,在 fireDate 时间到的时候执行
public static String setFireDate(Date fireDate) {
//this.fireDate = fireDate;
String[] cronArr = new String[7];
for (int i = 0; i < cronArr.length; i++) {
cronArr[i] = "";
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(fireDate);
int second = calendar.get(Calendar.SECOND);
int minute = calendar.get(Calendar.MINUTE);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int day = calendar.get(Calendar.DAY_OF_MONTH);
int month = calendar.get(Calendar.MONTH) + 1;
int year = calendar.get(Calendar.YEAR);
cronArr[0] = second + "";
cronArr[1] = minute + "";
cronArr[2] = hour + "";
cronArr[3] = day + "";
cronArr[4] = month + "";
cronArr[5] = "?";
cronArr[6] = year + "";
String cron = StringUtils.join(cronArr," ").trim();
System.out.println("cron==========" + cron);
//this.setCronj(cron);
return cron;
}3.配置定时器关闭的类
public static void removeJob(Scheduler sched, String jobName) {
try {
TriggerKey triggerKey = new TriggerKey(jobName, TRIGGER_GROUP_NAME);
sched.pauseTrigger(triggerKey);// 停止触发器
sched.unscheduleJob(triggerKey);// 移除触发器
JobKey jobKey = new JobKey(jobName, JOB_GROUP_NAME);
boolean b = sched.deleteJob(jobKey);// 删除任务
System.out.println(b);
} catch (Exception e) {
throw new RuntimeException(e);
}
}4.直接在业务层调用此方法,配置参数
Map map = new HashMap();
map.put("orderAdoptId",orderinfo.getId());
//超时订单关闭
QuartzUtils.addJob(scheduler,"超时关单",PrintTimeJob.class,map, QuartzJobInfo.setFireDate(date));5.scheduler实现业务超时关闭
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
//通过key获取value
Map map = (HashMap)jobDataMap.get("params");
//在通过value的key获取value
Long orderAdoptId = (Long)map.get("orderAdoptId");
System.out.println("订单id"+orderAdoptId);
//查询当前订单,获取当前状态
//Example example = new Example(OrderAdopt.class);
//example.and().andEqualTo("id",orderAdoptId);
OrderAdopt orderAdopt = orderAdoptMapper.selectByPrimaryKey(orderAdoptId);
if(orderAdopt==null){
System.out.println("订单不存在");
}
//判断当前状态是否为待支付 状态(0:待支付;1:支付成功;2:支付失败)
if(orderAdopt.getState()==0){
//修改状态为支付失败
orderAdopt.setState(2);
orderAdoptMapper.updateByPrimaryKeySelective(orderAdopt);
System.out.println("订单修改完成");
//库存回库+1
Long petId = orderAdopt.getPetId();//获得宠物id
Pet pet = petMapper.selectByPrimaryKey(petId);//通过宠物id获得宠物对象
pet.setCount(pet.getCount()+1);
petMapper.updateByPrimaryKeySelective(pet);
System.out.println("减库存成功");
}
//关定时器
QuartzUtils.removeJob(scheduler,"超时关单");
System.out.println("关单");
}
}边栏推荐
- HCNP Routing&Switching之DHCP中继
- What are the pitfalls from single architecture to distributed architecture?
- 700. Search DFS method in binary search tree
- Array learning navigation
- 详解 Apache Hudi Schema Evolution(模式演进)
- mac数据库管理软件Navicat Premium Essentials Mac
- 2022 ranking list of database audit products - must see!
- C语言中的字符与字符串库函数的使用以及模拟实现
- 二维卷积——torch.nn.conv2d的使用
- Are the top ten securities companies safe and risky to open accounts?
猜你喜欢

Still using xshell? You are out, recommend a more modern terminal connection tool!

Two dimensional convolution -- use of torch.nn.conv2d

Shengxin commonly used analysis graphics drawing 02 -- unlock the essence of volcano map!

数论整除分块讲解 例题:2021陕西省赛C

启发式合并(含一般式、树上启发式合并 例题)

C语言中的字符与字符串库函数的使用以及模拟实现

Tensorflow introductory tutorial (40) -- acunet

分家后印象笔记过日子依然不好过,骚操作却不少

ShardingSphere数据库读写分离

A problem of MySQL database
随机推荐
awk从入门到入土(17)awk多行写法
Six ways for JS to implement inheritance
获取同程(艺龙)酒店数据
Detailed explanation of ansible automatic operation and maintenance (V) the setting and use of variables in ansible, the use of jinja2 template and the encryption control of ansible
DF2NET三维模型部署
再见收费的Navicat!这款开源的数据库管理工具界面更炫酷!
Codeforces Round #794 (Div. 2)(A.B.C)
com.mysql.cj.jdbc.exceptions. MySQLTransactionRollbackException: Deadlock found when trying to get lo
Preliminary study of Oracle pl/sql
Can CSC open an account for domestic futures? Is it safe?
2022 Yangtze River Delta industrial automation exhibition will be held in Nanjing International Exhibition Center in October
Dry goods | three sub domain name collection tools worth collecting
使用matplotlib模拟线性回归
700. 二叉搜索树中的搜索-dfs法
C语言自定义类型讲解 — 结构体
Baidu PaddlePaddle easydl x wesken: see how to install the "eye of AI" in bearing quality inspection
[waiting for insurance] what does waiting for insurance rectification mean? What are the rectification contents?
Wrote a few small pieces of code, broke the system, and was blasted by the boss
【网络安全】网站中间件存在的解析漏洞
Get the data of Tongcheng (elong) Hotel