springBoot定时任务

30 篇文章 1 订阅

一、通过实现SchedulingConfigurer接口的方法实现定时任务

就这一个类…

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;

import java.time.LocalDateTime;

@Configuration
@EnableScheduling   // 开启定时任务
public class MyScheduledTask implements SchedulingConfigurer {
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        /**
         * Runnable task要执行的任务, Trigger trigger执行周期
         */
        taskRegistrar.addTriggerTask(
                () -> System.out.println("任务执行中" + LocalDateTime.now().toLocalTime()),
                (a)->{
                    // 此处可以书写数据库访问逻辑
                    // String cron = tempMapper.selectById(1);
                    // return new CronTrigger(cron).nextExecutionTime(a);
                    return new CronTrigger("0/3 * * * * ?").nextExecutionTime(a);
                }
        );
    }
}

二、@Scheduled注解(springboot定时任务默认使用线程池)

2.1单线程

import com.jule.springtask.service.SelectService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class TestController {
    @Resource
    private SelectService selectService;

    @RequestMapping("/")
    public void send(){
        selectService.send();
    }
}
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Slf4j
@EnableScheduling
@Service
public class SelectService {

    @Scheduled(cron = "0/3 * * * * ?")
    public void send(){
        log.info("定时任务:" + LocalDateTime.now().toLocalTime());
    }
}

2.2线程池(不是定时任务的多线程)

import com.jule.springtask.service.SelectService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class TestController {
    @Resource
    private SelectService selectService;

    @RequestMapping("/")
    public void send(){
        selectService.send();
    }
}
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Slf4j
@EnableScheduling
@Service
public class SelectService {

    @Scheduled(cron = "0/3 * * * * ?")
    @Async("poolTaskSMSExecutor")
    public void send(){
        log.info("定时任务:" + LocalDateTime.now().toLocalTime());
    }
}
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 线程池
 */
@EnableAsync
@Configuration
public class TaskPoolConfig {
    /**
     * 多线程线程池配置
     */
    @Bean("poolTaskSMSExecutor")
    public Executor mailNumPoolSingleTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);//核心线程数:线程池创建时候初始化的线程数
        executor.setMaxPoolSize(100);//最大线程数:线程池最大的线程数,只有在缓冲队列满了之后才会申请超过核心线程数的线程
        executor.setQueueCapacity(20);//缓冲队列200:缓冲执行任务的队列
        executor.setKeepAliveSeconds(60);//允许线程的空闲时间60秒:当超过了核心线程出之外的线程在空闲时间到达之后会被销毁
        executor.setThreadNamePrefix("预约短信发送-");//线程池名的前缀:设置好了之后可以方便我们定位处理任务所在的线程池
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());// 线程池对拒绝任务的处理策略
        return executor;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jule_zhou

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值