Before because of
Warning: It takes 6 minutes to read this article
In Hanzo Store, there are some users who submit orders but do not pay. Before, I scanned unpaid orders at 5 o ‘clock every day through quartz timed task, then read users’ email addresses and sent emails to remind users to pay as soon as possible. This time, I used the Spring built-in SpringTask for scheduled tasks.
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
Seconds, - * / 0-59 Minutes, - * / 0-59 Hours, - * / 0-23 DayofMonth, - * /? L W 0-31 Month , - * / 1-12 DayofWeek , - * / ? L # 1-7 or Sun-satCopy the code
Special characters in Cron format
Enumeration values are listed in the Minutes field using 5,10, to trigger at 5 and 10. - To trigger ranges from 5 to 10. Every minute from 5 to 10 Minutes * Matches any value * is used in the Minutes field * is used every minute/Start time is used at fixed intervals 5/10 is used in the Minutes field is used once at 5 Minutes and again every 10 Minutes? In DayofMonth and DayofWeek, used to match any value used in the DayofMonth field? # in DayofMonth, the day of the week is set. 1#3 is set for the third Sunday. L is set for the last Dayof the week. If the 5th falls on a Saturday, it will be triggered on the 4th of the nearest working dayCopy the code
Integrated SpringTask
Since SpringTask already exists in the Spring framework, there is no need to add dependencies.
Configuration SpringTaskConfig class
Simply add an @enablesCheduling annotation to the configuration class to enable SpringTask’s scheduled task capability.
@Configuration
@EnableScheduling
public class SpringTaskConfig {}Copy the code
Add the CallPaySpringTask class to perform scheduled tasks
package ltd.hanzo.mall.task;
import lombok.extern.slf4j.Slf4j;
import ltd.hanzo.mall.service.TaskService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/ * * *@AuthorHao yu QAQ *@email [email protected]
* @Date2020/5/19 "*@link https://github.com/Tianhaoy/hanzomall
* @Description: Scan the unpaid orders regularly every day and send email reminders to pay */
@Slf4j
@Component
public class CallPaySpringTask {
@Resource
private TaskService taskService;
/** * cron expression: Seconds Minutes Hours DayOfMonth Month DayOfWeek [Year] */
@Scheduled(cron = "0, 0, 5 * *?)
private void callPay(a) {
log.info("Start batch sending email reminders of pending orders via SpringTask");
// Call your own scheduled task interface -- I'm calling the interface that sends an order to be paidtaskService.callPayOrders(); }}Copy the code
There are some search order information of the service layer mapper layer code is not posted, according to their own business development can be. The main thing is to share the process, the code implementation is not difficult.
summary
This concludes the entire process of sending email messages via the SpringTask timed task, and knowledge is valuable only if shared. If you have any questions, please feel free to contact me through my email on my page.