当前位置:网站首页>Make the tasks in the scheduled task XXL job flexible
Make the tasks in the scheduled task XXL job flexible
2022-06-13 00:21:00 【carl-zhao】
It is a common requirement to use scheduled tasks for business compensation in projects . For a mission , We often fix the start time or end time in the code . If a scheduled task misses data . such as :
- Timing task : Inquire about 1 Days ago to the current system time
- Missing data : stay 3 A few days ago, some data has not been executed yet
If you do not set the start time to a dynamic variable, this problem cannot be solved . You can configure the start time in the registry , The query time is :3 Days ago, ~ Current system time . This SQL The query system is relatively low .
Another way is to set the start time and end time as variables in the registry , In this way, you can dynamically customize the start and end times of your task query .
But the task parameter is configured in the registry ? It's better to configure it in the task ? I personally prefer the latter . The reasons are as follows :
- Use the configuration center to configure , Each task is configured with different variables , The amount of code added is not very elegant
- Use the dynamic parameter configuration in the task , In this way, modifying the execution logic of a task is the closest to the task , Nearby principle .
So for scheduled tasks , We need to customize the following parameters :
- Parameters 1: Business number : The highest priority , It is possible that a piece of data was missed during the execution of the previous scheduled task . This is the time , how xxl job The task parameter of passed the business number . Then only execute this business number
- Parameters 2: Number of queries per page : When performing scheduled tasks , If the time range of our inquiry is too wide , There may also be a lot of data , So we introduce the concept of paging to query data , Only query 1 Page and the number of queries can be configured .
- Parameters 3: Query start time : A scheduled task is the data that needs to be operated on for a period of time , So there is a start time .
- Parameters 4: Query end time : A scheduled task is the data that needs to be operated on for a period of time , So there is an end time .
When performing scheduled tasks , Or only Parameters 1 Otherwise, it would be Parameters 2、3、4. That is, either execute a specific piece of data or execute for a certain period of time, and the maximum number of pieces is Parameters 2 Value .
For the above 4 Parameters can be customized , Then request parameter settings :
JobParamModel.java
public class JobParamModel {
/** Reference number , The order field can be understood as the order number */
private String referenceNo;
/** Base time ( End time ) yyyy-MM-dd HH:mm:ss */
private String basicTime;
/** Forward offset */
private Integer forwardOffset;
/** Offset units forward */
private String forwardOffsetUnit;
/** Backward offset */
private Integer backwardOffset;
/** Shift back units */
private String backwardOffsetUnit;
/** Whether it is a fully closed interval time period */
private boolean wholeClosedRange;
/** paged */
private int pageSize = 500;
}
Let's analyze the function of each parameter .
referenceNo: Reference number , The order field can be understood as the order number : Like your scheduled tasks , Only one order number is required for execution . Then you only need to configure this parameter . It should have the highest priority .basicTime: Base time , That is our end time , This time can be set by yourself , If the reference time is not set , By default, the current system time is used as the base timeforwardOffset: Forward offset , Obtain the start time of scheduled task execution based on the reference time , And forwardOffsetUnit In combination with . It means that the reference time is shifted forward by multiple time units ( At present, we support : second (SECONDS)、 branch (MINUTES)、 when (HOURS)、 God (DAYS)). For example, the benchmark time is :2021-05-19 13:00:01 , The offset is 10 , Then the forward offset unit is hour (HOURS), The start time of the query is : 2021-05-19 03:00:01. If not configured , The default start time is the base time minus 24 Hours , That is to query the data of the latest day .forwardOffsetUnit: Offset units forward , At present, we support : second (SECONDS)、 branch (MINUTES)、 when (HOURS)、 God (DAYS)backwardOffset: Backward offset , Obtain the end time of scheduled task execution based on the reference time , And backwardOffsetUnit In combination with . It means that the reference time is shifted back by multiple time units ( At present, we support : second (SECONDS)、 branch (MINUTES)、 when (HOURS)、 God (DAYS)). For example, the benchmark time is :2021-05-19 13:00:01, The offset is -5, Then shift back in days (DAYS), The end time of the query is :2021-05-14 13:00:01. If it is not configured, the default end time is the base time .backwardOffsetUnit: Shift back units , At present, we support : second (SECONDS)、 branch (MINUTES)、 when (HOURS)、 God (DAYS)wholeClosedRange: Whether it is a fully closed interval time period , It means to set the start time to the current day 00:00:00, Set the end time to 23:59:59. such as wholeClosedRange by false The time period is : 2021-05-18 13:00:01 ~ 2021-05-19 13:00:01 . When the value is set to true when , The query period is :2021-05-18 00:00:00 ~ 2021-05-19 23:59:59pageSize: Number of queries , Default query 500 strip , You can pass in the parameter of the scheduled task or the customized query number in the query criteria .
Be careful : If the parameters passed by the scheduled task are illegal, the latest will be queried by default 1 Days of 500 Data .
Illustrate with examples :
situation 1:{“basicTime” : “2021-05-19 13:00:00”, “forwardOffset” : 5, “forwardOffsetUnit” : “HOURS”}
Query criteria : 2021-05-19 08:00:00 ~ 2021-05-19 13:00:00( The system time does not affect the query criteria )
situation 2:{“forwardOffset” : 5, “forwardOffsetUnit” : “HOURS”} system time :2021-06-30 20:59:11
Query criteria :2021-06-30 15:59:11 ~ 2021-06-30 20:59:11( System time affects query criteria )
situation 3:{“forwardOffset” : 5, “forwardOffsetUnit” : “DAYS”, “backwardOffset”: -2, “backwardOffsetUnit”:“HOURS”} system time :2021-06-30 20:59:11
Query criteria :2021-06-25 20:59:11 ~ 2021-06-30 18:59:11( System time affects query criteria )
situation 4:{“forwardOffset” : 5, “forwardOffsetUnit” : “DAYS”, “backwardOffset”: -2, “backwardOffsetUnit”:“HOURS”, “wholeClosedRange”} system time :2021-06-30 20:59:11
Query criteria :2021-06-25 00:00:00 ~ 2021-06-30 23:59:59( System time affects query criteria )
This paper only provides a way of thinking , The implementation method is actually quite simple .
边栏推荐
猜你喜欢
![[matlab] two dimensional curve](/img/5a/bd69c78513fe4b59b2c63dc3679841.png)
[matlab] two dimensional curve

测试平台系列(97) 完善执行case部分

浏览器缓存的执行流程

Is the newly graduated college student taking BEC or PMP? PM who wants to transfer to another job in the future

PMP registration conditions, time, cost, new version related information

【HCIE论述】组播IGMP-A

63. 不同路径 II

What are the PMP scores?

How to make maputnik, a vector tile matching artifact, support GeoServer

MAYA建模六
随机推荐
Basics of network security (1)
Converting Chinese numbers to Arabic numbers in Delphi
Will the salary increase after obtaining PMP certification?
How to pass the PMP review?
[matlab] basic knowledge
[matlab] 3D curve and 3D surface
How to control the display and hiding of layergroup through transparency in leaflet
Is the PMP training organization an actual training?
Test platform series (97) perfect the case part
Using STM32 DMA to drive 4-wire SPI interface OLED high-speed display
[LeetCode]9. Palindromes thirty-two
Go custom collation
Delphi Chinese digit to Arabic digit
The difference between philosophy and Literature
New blog address
KAUST:Deyao Zhu | 价值记忆图:基于离线强化学习的图结构世界模型
6.824 Lab 3B: Fault-tolerant Key/Value Service
[LeetCode]13. Roman numerals to integers thirty
ucore lab2
Do you really use the buffer buffer in PostGIS?