springboot使用SpringTask实现定时任务

 

SpringTask是Spring自主研发的轻量级定时任务工具,相比于Quartz更加简单方便,且不需要引入其他依赖即可使用。

 

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

package com.xc.mall2.config;

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

/**
 * 定时任务配置
 * Created by xc on 190830
 */
@Configuration
@EnableScheduling
public class SpringTaskConfig {
}

 

添加OrderTimeOutCancelTask来执行定时任务

package com.xc.mall2.component;

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

/**
 * Created by xc on 20190830
 * 定时任务
 */
@Component
public class OrderTimeOutCancelTask {
    private Logger LOGGER = LoggerFactory.getLogger(OrderTimeOutCancelTask.class);
    // @Autowired
    // private OmsPortalOrderService portalOrderService;

    /**
     * cron表达式:Seconds Minutes Hours DayofMonth Month DayofWeek [Year]
     */
    @Scheduled(cron = "0/15 * * * * ?")
    private void cancelTimeOutOrder() {
        // CommonResult result = portalOrderService.cancelTimeOutOrder();
        // LOGGER.info("取消订单,并根据sku编号释放锁定库存:{}", result);
        LOGGER.info("定时任务OrderTimeOutCancelTask");
    }
}

 

 

参考文章:https://macrozheng.github.io/mall-learning/#/architect/mall_arch_06?id=%e9%a1%b9%e7%9b%ae%e4%bd%bf%e7%94%a8%e6%a1%86%e6%9e%b6%e4%bb%8b%e7%bb%8d

 

转载于:https://www.cnblogs.com/ooo0/p/11435647.html

Spring Boot中,`SpringTask`是Spring提供的定时任务解决方案,它基于`java.util.concurrent`包下的`ScheduledExecutorService`,简化了定时任务的开发。使用Spring Task,你可以很容易地在应用中安排任务在特定时间执行或周期性执行。 要使用Spring Task,你需要做以下几步: 1. 在Spring Boot应用中添加`spring-boot-starter`依赖。 2. 在需要定时执行的方法上添加`@Scheduled`注解来配置任务执行时间。 3. (可选)使用`@EnableScheduling`注解在应用主类上或者配置类上开启定时任务的调度功能。 以下是一个简单的例子,展示如何在Spring Boot应用中创建一个定时任务: ```java import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class ScheduledTasks { // 每隔5秒执行一次 @Scheduled(fixedRate = 5000) public void reportCurrentTime() { System.out.println("当前时间:" + System.currentTimeMillis()); } } ``` 如果你想配置cron表达式来更精确地控制任务的执行时间,可以这样做: ```java import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class ScheduledTasks { // 每天的凌晨1点执行一次 @Scheduled(cron = "0 0 1 * * ?") public void scheduleTaskUsingCron() { System.out.println("在每天的凌晨1点执行的任务"); } } ``` 确保你的Spring Boot应用已经开启了定时任务的支持: ```java import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.stereotype.Component; @EnableScheduling @Component public class SchedulerConfig { // 这里可以配置其他的Bean } ``` 如果你使用的是Spring Boot 2.3.x或更高版本,需要注意的是`@EnableScheduling`注解已经不再是必需的,因为Spring Boot会在应用上下文中自动寻找并注册`TaskScheduler`和`ScheduledExecutorService`的Bean。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
>