第一种方式:使用注解 @EnableScheduling、@Scheduled
cron、fixedRate、fixedDelay三个是用来调度时间的这三种方式都是设置每隔十秒执行一次;
cron:设置定时执行的表达式;
fixedRate:表示一个固定频率执行,隔多长时间调用一次,上个任务开始后,多长时间后开始执行,不管任务是否执行完;
fixedDelay:表示该任务执行完后隔多长时间再调用;
package com.example.demoquartz.scheduled;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
@Component
@EnableScheduling
public class TestJob {
@Scheduled(cron = "0/10 * * * * ?")
public void runfirst(){
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("********first job is ok******" + sdf.format(new Date()));
}
// @Scheduled(fixedRate = 1000 * 10)
// public void runsecend(){
// SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// System.out.println("********second job is ok******" + sdf.format(new Date()));
// }
//
// @Scheduled(fixedDelay=1000)
// public void runThird(){
// SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// System.out.println("********third job is ok******" + sdf.format(new Date()));
// }
}
第二种方式:使用 SchedulingConfigurer
package com.example.demoquartz.scheduled;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import java.text.SimpleDateFormat;
import java.util.Date;
@Configuration
@EnableScheduling
public class ConfigurerTest implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.addFixedRateTask(new Runnable() {
@Override
public void run() {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("********addFixedRateTask new Runnable() 定时任务执行了******" + sdf.format(new Date()));
}
}, 5000);
}
}
第三种方式:使用 Quartz
Scheduler:调度器,所有的调度都是由它控制。
Trigger: 触发器,决定什么时候来执行任务。
JobDetail & Job: JobDetail定义的是任务数据,而真正的执行逻辑是在Job中。
1、pom.xml添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
2、创建定时执行任务类,定义Job
package com.example.demoquartz.scheduled;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
import java.text.SimpleDateFormat;
import java.util.Date;
public class testTask extends QuartzJobBean {
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("TestQuartz01----" + sdf.format(new Date()));
}
}
3、任务调度类
package com.example.demoquartz.scheduled;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class QuartzConfig {
@Bean
public JobDetail testQuartz1() {
return JobBuilder.newJob(testTask.class).withIdentity("testTask1").storeDurably().build();
}
@Bean
public Trigger testQuartzTrigger1() {
//5秒执行一次
SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(5)
.repeatForever();
// return TriggerBuilder.newTrigger().forJob(testQuartz1())
// .withIdentity("testTask1")
// .withSchedule(scheduleBuilder)
// .build();
//cron方式,每隔5秒执行一次
return TriggerBuilder.newTrigger().forJob(testQuartz1())
.withIdentity("testTask1")
.withSchedule(CronScheduleBuilder.cronSchedule("*/5 * * * * ?"))
.build();
}
}
*