当前位置:网站首页>0612~quartz timer frame
0612~quartz timer frame
2022-07-24 17:47:00 【Life is so hard】
Today, the teacher talked about the timer frame ,quartz~ Easy to learn But it is not suitable for high concurrency ;
quartz Mainly by :1.trigger trigger ,
2.job detail Business details ,
Two parts scheduler Containers ;
Applicable scenario : 1. Email regularly ;
2. Send reports regularly ;
3. Close payment orders regularly ;
4. A series of timing operations can be completed , Powerful ;
The function of closing orders in practice ;
1. Configuration dynamics jobDetai~ dynamic trigg trigger ;
2. Configure time into cron Class of expressions ;
3. Configure the class of shutdown timer ;
4. Call this method directly at the business layer , Configuration parameters ;
5.scheduler Realize business timeout shutdown
1. Configuration dynamics jobDetai~ dynamic trigg trigger ;
*/
public class QuartzUtils {
private static String JOB_GROUP_NAME = "JOB_GROUP_SYSTEM";
private static String TRIGGER_GROUP_NAME = "TRIGGER_GROUP_SYSTEM";
/**
* @Description: Add a scheduled task , Use the default task group name , Trigger Name , Trigger group name
*
* @param sched
* Scheduler
*
* @param jobName
* Task name
* @param cls
* Mission
* @param params
* Mission parameters
* @param time
* Set the time , Reference resources quartz documentation
*
* @Title: QuartzManager.java
*/
public static void addJob(Scheduler sched, String jobName, Class cls, Object params,
String time) {
try {
//-------------------------- establish JobDetail Job details ---------------------------------
JobKey jobKey = new JobKey(jobName, JOB_GROUP_NAME);// Task name , Task force , Task execution class
//Job The data of map
JobDataMap jobDataMap = new JobDataMap();
jobDataMap.put("params", params);
// Based on the incoming job Class , Create a JobDetail, And designate JobKey, And put the data map Set to job
JobDetail jobDetail = newJob(cls).withIdentity(jobKey).setJobData(jobDataMap).build();
//-------------------------- establish Trigger trigger ---------------------------------
// To create a trigger key, Equivalent to trigger ID
TriggerKey triggerKey = new TriggerKey(jobName, TRIGGER_GROUP_NAME);
// Create trigger , Set trigger key, And associated with a cronSchedule( Expression based time rules )
Trigger trigger = newTrigger().withIdentity(triggerKey).withSchedule(cronSchedule(time)).build();
// Set the job details and triggers to schedule Scheduling container
sched.scheduleJob(jobDetail, trigger);
// start-up
if (!sched.isShutdown()) {
sched.start();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}2. Configure time into cron Class of expressions
// Turn time into a time expression , stay fireDate When the time comes
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. Configure the class of timer off
public static void removeJob(Scheduler sched, String jobName) {
try {
TriggerKey triggerKey = new TriggerKey(jobName, TRIGGER_GROUP_NAME);
sched.pauseTrigger(triggerKey);// Stop trigger
sched.unscheduleJob(triggerKey);// Remove trigger
JobKey jobKey = new JobKey(jobName, JOB_GROUP_NAME);
boolean b = sched.deleteJob(jobKey);// Delete task
System.out.println(b);
} catch (Exception e) {
throw new RuntimeException(e);
}
}4. Call this method directly at the business layer , Configuration parameters
Map map = new HashMap();
map.put("orderAdoptId",orderinfo.getId());
// Timeout order closed
QuartzUtils.addJob(scheduler," Overtime customs clearance ",PrintTimeJob.class,map, QuartzJobInfo.setFireDate(date));5.scheduler Realize business timeout shutdown
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
// adopt key obtain value
Map map = (HashMap)jobDataMap.get("params");
// Through value Of key obtain value
Long orderAdoptId = (Long)map.get("orderAdoptId");
System.out.println(" Order id"+orderAdoptId);
// Query the current order , Get current status
//Example example = new Example(OrderAdopt.class);
//example.and().andEqualTo("id",orderAdoptId);
OrderAdopt orderAdopt = orderAdoptMapper.selectByPrimaryKey(orderAdoptId);
if(orderAdopt==null){
System.out.println(" The order does not exist ");
}
// Judge whether the current status is to be paid state (0: To be paid ;1: Successful payment ;2: Failure to pay )
if(orderAdopt.getState()==0){
// Change the status to payment failed
orderAdopt.setState(2);
orderAdoptMapper.updateByPrimaryKeySelective(orderAdopt);
System.out.println(" Order modification completed ");
// Stock return +1
Long petId = orderAdopt.getPetId();// Get pets id
Pet pet = petMapper.selectByPrimaryKey(petId);// By pet id Get pet object
pet.setCount(pet.getCount()+1);
petMapper.updateByPrimaryKeySelective(pet);
System.out.println(" Inventory reduction success ");
}
// Turn off timer
QuartzUtils.removeJob(scheduler," Overtime customs clearance ");
System.out.println(" Customs clearance ");
}
}边栏推荐
- 213. Looting II - Dynamic Planning
- 20 -- validate palindrome string
- C语言自定义类型 — 枚举
- Supervisor common commands
- Ipaylinks, a cross-border payment integration service, won the 3A Asia Award of treasury
- 快速完成intelij idea的单元测试JUnit4设置
- 还在用命令行看日志?快用Kibana吧,可视化日志分析YYDS!
- How the computer accesses the Internet (IV) LAN and server response
- C语言实现静态版本的通讯录
- Atcoder beginer 202 e - count descendants (heuristic merge on heavy chain split tree for offline query)
猜你喜欢

实习报告1——人脸三维重建方法

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

Tensorflow introductory tutorial (38) -- V2 net

Nearly 30 colleges and universities were named and praised by the Ministry of education!

Openlayers: point aggregation effect

The use and Simulation of character and string library functions in C language

使用Prometheus+Grafana监控MySQL性能指标

The results of the second quarter online moving people selection of "China Internet · moving 2022" were announced

700. Search DFS method in binary search tree

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
随机推荐
213. 打家劫舍 II-动态规划
《STL源码剖析》应该怎样读?
How does win11 enhance the microphone? Win11 enhanced microphone settings
Tensorflow introductory tutorial (40) -- acunet
Colleges and universities have introduced 23 Philippine doctors with heavy funds, and the relevant departments have launched an investigation!
邻接表的定义和存储以及有向图无向图的邻接存储
头文件是必须的吗?跟一跟编译过程~~~
Codeforces Round #794 (Div. 2)(A.B.C)
Scept: consistent and strategy based trajectory prediction for planned scenarios
Review and analysis of noodle dishes
0623~放假自习
C language programming training topics: K characters in left-handed string, little Lele and Euclidean, printing arrow pattern, civil servant interview, poplar matrix
C language custom type explanation - structure
Wrote a few small pieces of code, broke the system, and was blasted by the boss
After separation, the impression notes are still difficult to live, but there are many coquettish operations
Df2net 3D model deployment
es(1)
0701~放假总结
实习报告1——人脸三维重建方法
ROC and AUC details of the recommended system