Abstract

This article mainly explains the process of integrating SpringTask with Mall, taking batch modification of time-out orders as an example. SpringTask is a lightweight timed task tool developed by Spring. Compared with Quartz, SpringTask is easier to use and does not need to introduce other dependencies. A Cron expression is a string containing 6 to 7 time elements that can be used in SpringTask to specify the execution time of a task.

Project use framework introduction

SpringTask

SpringTask is a lightweight timed task tool developed by Spring. Compared with Quartz, SpringTask is easier to use and does not need to introduce other dependencies.

Cron expression

A Cron expression is a string containing 6 to 7 time elements that can be used in SpringTask to specify the execution time of a task.

Cron syntax format

Seconds Minutes Hours DayofMonth Month DayofWeek

Description of each time element in the Cron format

The time element The character that can appear Effective value range
Seconds , – * / 0-59
Minutes , – * / 0-59
Hours , – * / 0-23
DayofMonth , – * /? L W 0-31
Month , – * / 1-12
DayofWeek , – * /? L # 1-7 – SAT or SUN

Special characters in Cron format

character role For example,
. Listing enumerated values Use 5,10 in the Minutes field to fire at 5 and 10 Minutes each
Indicates trigger range Use 5-10 in the Minutes field to trigger every minute from 5 to 10 Minutes
* Match any value Using * in the Minutes field means that it fires every minute
/ Start time Triggers at fixed intervals Use 5/10 in the Minutes field to fire once every 5 Minutes and again every 10 Minutes
? In DayofMonth and DayofWeek, used to match any value Used in the DayofMonth domain? Is triggered every day
# In DayofMonth, determine the day of the week 1#3 is the third Sunday
L Said the last Use 5L in DayofWeek to trigger on the last Thursday
W Represents valid working days (Monday to Friday) Use 5W in DayofMonth, and if the 5th falls on a Saturday, it will be triggered once on the 4th of the nearest weekday

Service Scenario Description

  • Users place an order for a commodity;
  • The system needs to generate orders according to the information of goods purchased by users and lock the inventory of goods;
  • The system set 60 minutes users do not pay will cancel the order;
  • Start a scheduled task and check every 10 minutes. If there is an order that has not been paid over time, cancel the order and cancel the locked inventory.

Integrated SpringTask

Since SpringTask already exists in the Spring framework, there is no need to add dependencies.

Add the SpringTask configuration

Simply add an @enablesCheduling annotation to the configuration class to enable SpringTask’s scheduled task capability.

package com.macro.mall.tiny.config;

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

/** * Created by macro on 2019/4/8. */
@Configuration
@EnableScheduling
public class SpringTaskConfig {}Copy the code

Add OrderTimeOutCancelTask to execute the scheduled task

package com.macro.mall.tiny.component;

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

/** * Created by macro on 2018/8/24. * Cancel order timeout and unlock inventory timer */
@Component
public class OrderTimeOutCancelTask {
    private Logger LOGGER = LoggerFactory.getLogger(OrderTimeOutCancelTask.class);

    /** * cron expression: Seconds Minutes Hours DayofMonth Month DayofWeek [Year] * Scan the order every 10 Minutes. If the order is not paid, the order will be cancelled */
    @Scheduled(cron = "0 * 0/10? *?")
    private void cancelTimeOutOrder(a) {
        // TODO:Here should call the cancel order method, specifically view the mall project source code
        LOGGER.info("Cancel order and release locked stock according to SKU number"); }}Copy the code

Project source code address

Github.com/macrozheng/…

The public,

Mall project full set of learning tutorials serialized, attention to the public number the first time access.