Electricity supplier project combat (five) - SpringTask achieve regular tasks

I. Introduction

  Regular tasks reflected everywhere in life, for example, get up in the morning, you need to set an alarm clock to call yourself, then the scene in the program which will use regular task? After an order is generated to have a process to pay, when to pay, one can see the payment amount, suddenly felt too much, do not pay, I do not cancel the order, just hanging, so commodity stocks have been bought in the locked state, a small amount of good, and if it is a lock tens of thousands of people still buying, and this time, the timing to cancel unpaid orders, a good solution to this problem.

Second, the framework

  1, Spring Task

    SpringTask Spring is lightweight self-developed tool for regular tasks, no need to introduce additional dependencies

  2, Cron Expressions

    Cron expression is a string, its syntax is: Seconds Minutes Hours DayofMonth Month DayofWeek

    Cron format special characters are:

 

character

effect For example
,(comma) Enumeration values ​​listed

Minutes domain 5, 10, 5 represents a respective trigger points and 10 points in the time

- (dash) Indicates that the trigger range

In the domain Minutes 5-10, represents 5 to 10 since the beginning, is triggered once per minute

*(Asterisk) Means match any value

In the Minutes domain using * indicates triggered once every minute

/ (Left slash)

It indicates the start trigger time

And each fixed time trigger

In use 5/10 Minutes domain, a rear start trigger from 5 minutes once every

10 minutes to trigger a

? (question mark)

And only in DayofMonth

DayofWeek match arbitrary value

In DayofMonth domain? Shows are triggered once a day
# (Well No.)

Used only in DayofMonth

It represents the first few days of the week

Use 1 # 3 in DayofMonth domain, the third Sunday of every month

Trigger a

L (uppercase L) It represents the last

In DayofWeek domain use 5L, shows the trigger on the last Thursday

 

 

 

  Cron Syntax Element Description

Time element Characters may appear

The valid range

Seconds , - * /

0-59

Minutes

, - * / 0-59

Hours

, - * / 0-59

DayofMonth

, - * / ? L W 0-31

Month

, - * / 1-12

DayofWeek

, - * / ? L # 1-7 或 SUN-SAT

三、业务场景

  · 用户对某商品进行下单操作

  · 系统需要根据用户购买的商品信息生成订单并锁定商品的库存

  · 系统设定用户60分钟不支付,自动取消订单

  · 开启一个定时任务,每隔10分钟进行一次扫描,如果有超时未支付订单,就将订单取消并释放库存商品

四、整合SpringTask

  1、在config包下新建启动SpringTask启动类SpringTaskConfig

package com.zzb.test.admin.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
 * 定时任务配置类
 * Created by zzb on 2019/11/26 12:17
 */
@Configuration
@EnableScheduling
public class SpringTaskConfig {
}

  2、在component包下新建SpringTask配置类OrderTimeOutCancelTask

package com.zzb.test.admin.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;


/**
 * 定时任务  订单超时自动取消并释放库存商品
 * Created by zzb on 2019/11/26 11:07
 */
@Component
public class OrderTimeOutCancelTask {

    private static final Logger logger = LoggerFactory.getLogger(OrderTimeOutCancelTask.class);

    // TODO: 2019/11/26 Seconds Minutes Hours DayofMonth Month DayofWeek
    @Scheduled(cron = "0 0/10 * ? * ?")
    public void cancelTimeOutOrder(){

        // TODO: 2019/11/26 从零点开始,每隔10分钟扫描一次,查询未支付订单
        logger.info("取消订单,并释放库存商品");
    }
}

五、测试

  启动项目,等待10分钟查看日志

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/zzb-yp/p/11929925.html