SpringBoot enables timer tasks (annotation-based, easy to use)

Article directory

 

foreword

1. What is a scheduled task?

2. Code implementation

1. Relevant code annotations

2. Annotation usage

Summarize


foreword

       In almost all projects, the use of timed tasks is indispensable. For example, in the daily life of online shopping, if the user receives the product without clicking Received, the order needs to be automatically converted to completed after a week. In the receiving state, this kind of real-time situation is not high, and the timer can be used to realize it. Therefore, it is very necessary to systematically learn about timing tasks. This article will take you to sort out and learn about the implementation of timers in the Java field.


1. What is a scheduled task?

        The timing task can be regarded as an alarm clock. After the project is running, according to the set timing interval, it can be set how often to process once to process the designed timing business

2. Code implementation

1. Relevant code annotations

        To use @Scheduledannotations, you first need to add them in the startup class @EnableSchedulingto enable Spring's scheduled task execution function, so that annotations can be detected on any Spring-managed bean in the container @Scheduled, and scheduled tasks can be executed

             Create a business processing class, inject the class into the Spring container ( emphasis ), create the business method that needs to be processed in the class, and use the @Scheduled annotation on the method, which can set the timing

package com.lyj.web.task;

import com.github.pagehelper.PageInfo;
import com.ruoyi.biz.domain.UserOrder;
import com.ruoyi.biz.dto.OrderDTO;
import com.ruoyi.biz.param.query.OrderQueryParam;
import com.ruoyi.biz.service.IUserOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

@Component
@Slf4j
public class OrderTask {

    @Autowired
    private IUserOrderService orderService;

    /**
     * 定时设置订单自动收货
     */
    @Scheduled(cron = "0 0 */1 * * *")
    public void autoReceipt(){
        log.info("----订单自动收货定时器----");
        final Date currentDate = new Date();
        //将已经付款,待收货的订单七天自动调整为已收货
        List<UserOrder> userOrderList = orderService.selectAll(OrderQueryParam.builder().status(2).build());
        List<UserOrder> collect = userOrderList.stream().filter(userOrder -> currentDate.getTime() - userOrder.getSendGoodsTime().getTime() > 1000 * 60 * 60 * 24 * 7 ).collect(Collectors.toList());
        collect.stream().forEach(userOrder -> orderService.updateUserOrder(UserOrder.builder().id(userOrder.getId()).status(4).build()));
    }
}

2. Annotation usage

Set the value of corn when using @Scheduled to set the interval of business processing

The six positional parameters of corn represent:

  • Seconds (0~59) For example, 0/5 means every 5 seconds
  • points (0~59)
  • hour (0~23)
  • A certain day of the day (0~31), needs to be calculated
  • month (0~11)
  • Day of the week (can fill in 1-7 or SUN/MON/TUE/WED/THU/FRI/SAT)

Common corn expressions:


Summarize

        The above is the implementation of timing tasks based on annotations. Timing tasks can be used for low real-time requirements, and message queues can be used for high real-time requirements, which can be realized by RabbitMq or Kafka.

Guess you like

Origin blog.csdn.net/liyingjie2001/article/details/126639128