springboot定时任务 多线程/周期自定义

package cn.com.rivercloud.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * author 
 * date 2020/1/13 0013 16:36
 */

@Configuration
@EnableScheduling
public class ScheduledConfig implements SchedulingConfigurer {

    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {

     //线程池的配置 scheduledTaskRegistrar.setScheduler(Executors.newScheduledThreadPool(
3));
     //并行的三个方法 scheduledTaskRegistrar.addTriggerTask(
          //执行的任务 ()
-> System.out.println(Thread.currentThread().getName() +"bbbbbbbb"),
          //自定义周期
          triggerContext
-> { String cron = "0/1 * * * * ?"; return new CronTrigger(cron).nextExecutionTime(triggerContext); } ); scheduledTaskRegistrar.addTriggerTask( ()-> System.out.println(Thread.currentThread().getName() +"aaaaaaaa"), triggerContext -> { String cron = "0/1 * * * * ?"; return new CronTrigger(cron).nextExecutionTime(triggerContext); } ); scheduledTaskRegistrar.addTriggerTask( ()-> System.out.println(Thread.currentThread().getName() +"dddddddd"), triggerContext -> { String cron = "0/1 * * * * ?"; return new CronTrigger(cron).nextExecutionTime(triggerContext); } ); } }

猜你喜欢

转载自www.cnblogs.com/notchangeworld/p/12188256.html