SpringBoot项目中@Scheduled定时任务配置线程池


使用spring的定时器 @Scheduled 的话,因为 @Scheduled 默认是单线程执行的,所以在需要的时候,我们可以设置一个线程池去执行定时任务。

1 在启动类上加入@EnableScheduling注解

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import java.net.InetAddress;
import lombok.extern.slf4j.Slf4j;
@EnableScheduling
@SpringBootApplication
@Slf4j
public class SynchronizationApplication {

    public static void main(String[] args) throws UnknownHostException {
        SpringApplication app = new SpringApplication(SynchronizationApplication.class);
        //同名Bean允许覆盖,不设置时默认为true
        app.setAllowBeanDefinitionOverriding(true);
        Environment env = app.run(args).getEnvironment();
        //服务名称
        String appName = env.getProperty("spring.application.name");
        //端口号
        String port = env.getProperty("server.port");
        //服务对外ip地址
        String ip = InetAddress.getLocalHost().getHostAddress();
    }

}

2 通过实现SchedulingConfigurer接口来将定时线程池放入



import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

@Configuration
public class TaskConfig implements SchedulingConfigurer {

    @Bean
    public TaskScheduler taskScheduler() {
        ThreadPoolTaskScheduler executor = new ThreadPoolTaskScheduler();
        executor.setPoolSize(10);
        executor.setThreadNamePrefix("task-thread");
        //设置饱和策略
        //CallerRunsPolicy:线程池的饱和策略之一,当线程池使用饱和后,直接使用调用者所在的线程来执行任务;如果执行程序已关闭,则会丢弃该任务
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        return executor;
    }

	//配置@Scheduled 定时器所使用的线程池
	//配置任务注册器:ScheduledTaskRegistrar 的任务调度器
    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
    	//可配置两种类型:TaskScheduler、ScheduledExecutorService
        //scheduledTaskRegistrar.setScheduler(taskScheduler());
        //只可配置一种类型:taskScheduler
        scheduledTaskRegistrar.setTaskScheduler(taskScheduler());
    }

}

线程池的饱和策略:

如果当前同时运行的线程数量达到最大线程数量并且队列也已经被放满了,线程池会执行饱和策略。

  • ThreadPoolExecutor.AbortPolicy:抛出 RejectedExecutionException来拒绝新任务的处理。
  • ThreadPoolExecutor.CallerRunsPolicy:当线程池使用饱和后,直接使用调用者所在的线程来执行任务;如果执行程序已关闭,则会丢弃该任务。
  • ThreadPoolExecutor.DiscardPolicy:不处理新任务,直接丢弃掉。
  • ThreadPoolExecutor.DiscardOldestPolicy: 此策略将丢弃最早的未处理的任务请求。

3 编写定时任务

cron在线生成网址

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

//注册为spring容器的组件
@Component
@Slf4j
public class SchedulerTask {

	//定时任务
	// 5 * * * * ? 在每分钟的5秒执行
    @Scheduled(cron = " 5 * * * * ? ")
    public void scheduleTask() {
        try {
            log.info("定时任务: 开始执行");
            //todo:执行业务
            log.info("定时任务: 执行完毕");
        } catch (Exception e) {
            log.error("定时任务执行出错", e);
        }
    }

  • 9
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
@Scheduled是Spring框架提供的一个注解,用于定时执行任务。它可以与线程池一起使用,以便更好地管理任务的执行。 在使用@Scheduled注解时,可以通过配置ThreadPoolTaskScheduler来指定线程池。ThreadPoolTaskScheduler是Spring提供的一个实现了TaskScheduler接口的线程池调度器。 使用线程池可以提供以下好处: 1. 资源管理:线程池可以限制同时执行的任务数量,避免资源过度占用。 2. 提高性能:线程池可以重用线程,避免频繁创建和销毁线程的开销。 3. 控制并发:线程池可以控制任务的并发度,避免系统负载过高。 下面是使用@Scheduled注解和线程池的示例代码: ```java @Configuration @EnableScheduling public class ScheduledConfig implements SchedulingConfigurer { @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler(); taskScheduler.setPoolSize(10); // 设置线程池大小 taskScheduler.initialize(); taskRegistrar.setTaskScheduler(taskScheduler); } } @Component public class ScheduledTasks { @Scheduled(fixedRate = 1000) // 每隔1秒执行一次任务 public void task() { // 执行任务逻辑 } } ``` 在上述示例,通过@Configuration和@EnableScheduling注解启用了定时任务配置。在ScheduledConfig类,通过实现SchedulingConfigurer接口并重写configureTasks方法,创建了一个ThreadPoolTaskScheduler实例,并设置了线程池大小为10。然后将该线程池设置到ScheduledTaskRegistrar。 在ScheduledTasks类,使用@Scheduled注解标注了一个定时任务方法task(),并设置了fixedRate属性为1000,表示每隔1秒执行一次任务。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值