Mall integrates SpringTask to implement timing tasks

Introduction to the project framework

SpringTask

SpringTask is a lightweight timing task tool independently developed by Spring. Compared with Quartz, it is simpler and more convenient, and can be used without introducing other dependencies.

Cron expression

Cron expression is a string, including 6~7 time elements, which can be used to specify the execution time of the task in SpringTask.

Cron syntax format

Seconds Minutes Hours DayofMonth Month DayofWeek

Description of each time element in cron format

Time element Characters that can appear Valid value range
Seconds , - * / 0-59
Minutes , - * / 0-59
Hours , - * / 0-23
DayofMonth , - * / ? L W 0-31
Month , - * / 1-12
DayofWeek , - * / ? L # 1-7 or SUN-SAT

Description of special characters in cron format

character effect For example
, List enum values Use 5 and 10 in the Minutes field, which means it will trigger once at 5 and 10 points
- Indicates the trigger range Use 5-10 in the Minutes field, which means it will be triggered every minute from 5 to 10 minutes
* Match any value Use * in the Minutes field, which means it will be triggered every minute
/ Start trigger at the start time, trigger once every fixed time Use 5/10 in the Minutes field, which means that it will be triggered once at 5 minutes and will be triggered again every 10 minutes
? In DayofMonth and DayofWeek, used to match any value Use? In the DayofMonth domain to indicate that it is triggered once a day
# In DayofMonth, determine the day of the week 1#3 means the third Sunday
L Means last Use 5L in DayofWeek, which means to trigger on the last Thursday
W Indicates effective working days (Monday to Friday) Use 5W in DayofMonth, if the 5th is a Saturday, it will be triggered once on the 4th of the nearest working day

Business scenario description

  • The user places an order for a product;

  • The system needs to generate an order based on the product information purchased by the user and lock the inventory of the product;

  • The system is set to cancel the order if the user does not pay for 60 minutes;

  • Start a timed task and check every 10 minutes. If there is an order that has not been paid for overtime, cancel the order and cancel the locked inventory.

Integrate SpringTask

由于SpringTask已经存在于Spring框架中,所以无需添加依赖。

添加SpringTask的配置

只需要在配置类中添加一个@EnableScheduling注解即可开启SpringTask的定时任务能力。

 
 
  1. package com.macro.mall.tiny.config;


  2. import org.springframework.context.annotation.Configuration;

  3. import org.springframework.scheduling.annotation.EnableScheduling;


  4. /**

  5. * 定时任务配置

  6. * Created by macro on 2019/4/8.

  7. */

  8. @Configuration

  9. @EnableScheduling

  10. public class SpringTaskConfig {

  11. }

添加OrderTimeOutCancelTask来执行定时任务

 
 
  1. package com.macro.mall.tiny.component;


  2. import org.slf4j.Logger;

  3. import org.slf4j.LoggerFactory;

  4. import org.springframework.scheduling.annotation.Scheduled;

  5. import org.springframework.stereotype.Component;


  6. /**

  7. * Created by macro on 2018/8/24.

  8. * 订单超时取消并解锁库存的定时器

  9. */

  10. @Component

  11. public class OrderTimeOutCancelTask {

  12.    private Logger LOGGER = LoggerFactory.getLogger(OrderTimeOutCancelTask.class);


  13.    /**

  14.     * cron表达式:Seconds Minutes Hours DayofMonth Month DayofWeek [Year]

  15.     * 每10分钟扫描一次,扫描设定超时时间之前下的订单,如果没支付则取消该订单

  16.     */

  17.    @Scheduled(cron = "0 0/10 * ? * ?")

  18.    private void cancelTimeOutOrder() {

  19.        // TODO: 2019/5/3 此处应调用取消订单的方法,具体查看mall项目源码

  20.        LOGGER.info("取消订单,并根据sku编号释放锁定库存");

  21.    }

  22. }


Guess you like

Origin blog.51cto.com/15082397/2591211