当前位置:网站首页>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("关单");
}
}边栏推荐
- List of stringutils and string methods
- A problem of MySQL database
- Is it safe for qiniu to open an account?
- 2022 Niuke summer multi school K - link with bracket sequence I (linear DP)
- TCP protocol debugging tool tcpengine v1.3.0 tutorial
- Baidu PaddlePaddle easydl x wesken: see how to install the "eye of AI" in bearing quality inspection
- Fast power writing
- Link editing tips of solo blog posts illegal links
- 2022 Yangtze River Delta industrial automation exhibition will be held in Nanjing International Exhibition Center in October
- 《STL源码剖析》应该怎样读?
猜你喜欢

ROC and AUC details of the recommended system

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

Baidu PaddlePaddle easydl x wesken: see how to install the "eye of AI" in bearing quality inspection

Tensorflow introductory tutorial (38) -- V2 net

2022 Niuke summer multi school K - link with bracket sequence I (linear DP)

Shardingsphere database read / write separation

Internship report 1 - face 3D reconstruction method

Pay close attention! List of the latest agenda of 2022 open atom open source Summit

Step by step introduction to the development framework based on sqlsugar (12) -- split the content of the page module into components to realize the division and rule processing

Get the data of Tongcheng (elong) Hotel
随机推荐
Hcip fourth day notes
wallys/IPQ8074A 4x4 2.4G 8x8 5G 802.11ax
Step by step introduction to the development framework based on sqlsugar (12) -- split the content of the page module into components to realize the division and rule processing
C language custom type explanation - structure
2022 Niuke summer multi school K - link with bracket sequence I (linear DP)
近30所高校,获教育部点名表扬!
数论整除分块讲解 例题:2021陕西省赛C
Openlayers: point aggregation effect
Array double pointer - deliberate practice
ShardingSphere数据库读写分离
分家后印象笔记过日子依然不好过,骚操作却不少
C语言自定义类型讲解 — 联合体
Portfwd port forwarding
启发式合并(含一般式、树上启发式合并 例题)
Colleges and universities have introduced 23 Philippine doctors with heavy funds, and the relevant departments have launched an investigation!
Huawei machine test - topic core test point
Shardingsphere database read / write separation
2.3.1 view drawing process
Review and analysis of noodle dishes
In the morning, Tencent took out 38K, which let me see the ceiling of the foundation