springboot integration timed tasks using @Scheduled

1. Start the class which add annotations @EnableScheduling, for example:

@SpringBootApplication
@EnableScheduling
@MapperScan("com.example.liuyi.mapper")
public class LiuyiApplication {

public static void main(String[] args) {
SpringApplication.run(LiuyiApplication.class, args);
}

}

2. The method of adding annotations @Scheduled, and class have achieved the annotation @Component assembly,

For example cron usage scenarios:

@Component 
public class TaskTest {

/ **
* timing of execution of a task definition time, performed once every 5 seconds,
    the cron expression arranged to perform tasks in which moment, configures the task start time determines whether the task can be performed in If you can perform, will not skip this execution;
* /
@Scheduled (cron = "? * * * * 0/5")
public void doEat () throws InterruptedException {
System.out.println ( "began to eat it "new new + a Date ());
the Thread.sleep (* 1000. 7);
System.out.println (" end eat it "new new + a Date ());
}

 

fixedDelay usage scenarios
/ ** 
* fixedDelay is set on the end of a long task to the next task, which is fixedDelay only care about the start time and end time of the next task on a task.
* /
@Scheduled (. 5 * 1000 = FIXEDDELAY)
public void doPlay () throws InterruptedException {
System.out.println ( "Start Wanla" + new new a Date ());
the Thread.sleep (*. 7 1000);
the System.out. println ( "end Wanla" + new new a Date ());
}


fixedRate usage scenarios
/ ** 
* Define a task performed by the frequency
* Start time two tasks interval is 5s, when the time arrives begin the task, but the task has not been completed,
* the Spring will wait on a task executed, and immediately began to perform this task.
* /
@Scheduled (fixedRate = 1000 *. 5)
public void doJob () throws InterruptedException {
System.out.println ( "Start Work Rights" new new + a Date ());
the Thread.sleep (*. 7 1000);
the System.out. println ( "end of the work it" new new + a Date ());
}


Precautions:

1.SpringBoot default is the time to perform the task synchronization, as long as the task to add @Scheduled method needs to be configured on the next task will be to start the implementation of this task is finished before the start
asynchronous need to perform the same task in the method upper body plus @Async comment


/ ** 
* synchronous execution
* @throws InterruptedException
* /
@Scheduled ( the cron = "? * / * * * * 20 is" )
public void ipWriter () throws InterruptedException {
for ( int I = 0 ; I < 20 is ; I ++) {
. the System OUT .println ( "I ---- synchronization executing" + new new a Date ()) ;
. the Thread SLEEP ( 5000 ) ;
}
}

/ **
* asynchronous execution
* @throwsInterruptedException
*/
@Async
@Scheduled(cron = "*/20 * * * * ?")
public void ipWriterSync() throws InterruptedException {
for(int i=0;i<20;i++){
System.out.println("i----异步执行 "+new Date());
Thread.sleep(5000);
}
}

 

 

 2. Multi-task concurrent execution, scheduled tasks using SpringBoot configuration process, use @Scheduled configured with multiple regular tasks, but each task will start a timer when the project started,

Because the number of threads ThreadPoolTaskScheduler source 1 is turned on by default, so you can only perform a timed task, the following is part of the source code
 
public class ThreadPoolTaskScheduler extends ExecutorConfigurationSupport implements AsyncListenableTaskExecutor, SchedulingTaskExecutor, TaskScheduler {
private volatile int poolSize = 1;

public void setPoolSize(int poolSize) {
Assert.isTrue(poolSize > 0, "'poolSize' must be 1 or higher");
this.poolSize = poolSize;
if (this.scheduledExecutor instanceof ScheduledThreadPoolExecutor) {
((ScheduledThreadPoolExecutor)this.scheduledExecutor).setCorePoolSize(poolSize);
}

}
}

 

在启动的时候重新配置,创建BeanConfig类,注意,需要在类上添加@Component注解,项目启动的时候类中的@Bean注解才会被扫描到,使配置生效

@Component
public class ThreadPoolTaskSchedulerConfig {
@Bean
public TaskScheduler taskScheduler() {
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.setPoolSize(2);//我这里设置的线程数是2,可以根据需求调整
return taskScheduler;
}

}


 

public class ThreadPoolTaskScheduler extends ExecutorConfigurationSupport implements AsyncListenableTaskExecutor, SchedulingTaskExecutor, TaskScheduler {    private volatile int poolSize = 1;        public void setPoolSize(int poolSize) {        Assert.isTrue(poolSize > 0, "'poolSize' must be 1 or higher");        this.poolSize = poolSize;        if (this.scheduledExecutor instanceof ScheduledThreadPoolExecutor) {            ((ScheduledThreadPoolExecutor)this.scheduledExecutor).setCorePoolSize(poolSize);        }     }}————————————————版权声明:本文为CSDN博主「Demo_Liu」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。原文链接:https://blog.csdn.net/Demo_Liu/article/details/85335414

Guess you like

Origin www.cnblogs.com/liuyi13535496566/p/12052358.html