美文网首页
SchedulingConfigurer demo

SchedulingConfigurer demo

作者: 青丝如梦 | 来源:发表于2019-07-12 10:04 被阅读0次

    @EnableScheduling 注解:

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.scheduling.annotation.EnableScheduling;
    
    @SpringBootApplication
    @EnableScheduling
    public class ExterintellreceiveApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ExterintellreceiveApplication.class, args);
        }
    }
    

    注入ThreadPoolTaskScheduler:

    package com.wilson;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
    
    @Configuration
    public class ThreadPoolTaskSchedulerConfig {
    
        @Bean
        public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
            ThreadPoolTaskScheduler executor = new ThreadPoolTaskScheduler();
            executor.setPoolSize(20);
            executor.setThreadNamePrefix("ThreadPoolTaskScheduler");
            executor.setWaitForTasksToCompleteOnShutdown(true);
            executor.setAwaitTerminationSeconds(60);
            return executor;
        }
    }
    

    定时任务:

    package com.idss.exterintellreceive.scheduled;
    
    import org.springframework.scheduling.annotation.SchedulingConfigurer;
    import org.springframework.scheduling.config.ScheduledTaskRegistrar;
    import org.springframework.stereotype.Component;
    
    /**
     * @author admin
     */
    @Component
    public class TestScheduled implements SchedulingConfigurer {
    
        @Override
        public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
            scheduledTaskRegistrar.addCronTask(() -> {
    //            每隔一分钟打印一次 'hello world'
                System.out.println("hello world");
            }, "0 0/1 * * * ?");
        }
    }
    
    

    相关文章

      网友评论

          本文标题:SchedulingConfigurer demo

          本文链接:https://www.haomeiwen.com/subject/dcvrkctx.html