springboot定时器阻塞

使用springboot开启定时器很简单,代码如下
package com.server;



import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.scheduling.annotation.Scheduled;

import org.springframework.stereotype.Component;



/**

 * 定时器

 */

@Component

public class Scheduler {

    Logger log = LoggerFactory.getLogger(this.getClass());





    @Scheduled(fixedRate = 1000)

    public void execScheduler1() {

        log.info("====execScheduler1");

    }



    @Scheduled(fixedRate = 3000)

    public void execScheduler2() {

        log.info("====execScheduler2");

    }

}

这样简单配置定时器能正常执行,但是会出现堵塞情况,可以加上如下配置解决
package com.server;



import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.scheduling.TaskScheduler;

import org.springframework.scheduling.annotation.EnableScheduling;

import org.springframework.scheduling.annotation.SchedulingConfigurer;

import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

import org.springframework.scheduling.config.ScheduledTaskRegistrar;



@Configuration

@EnableScheduling

public class ScheduleConfig implements SchedulingConfigurer {



   @Override

   public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {

      TaskScheduler taskScheduler = taskScheduler();

      taskRegistrar.setTaskScheduler(taskScheduler);

   }



   @Bean(destroyMethod = "shutdown")

   public ThreadPoolTaskScheduler taskScheduler() {

      ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();

      scheduler.setPoolSize(50);

      scheduler.setThreadNamePrefix("scheduled-");

      scheduler.setAwaitTerminationSeconds(60);

      scheduler.setWaitForTasksToCompleteOnShutdown(false);

      return scheduler;

   }

}

Author:

发表评论

电子邮件地址不会被公开。 必填项已用*标注