定时任务是企业开发中最常见的功能之一,比如定时统计订单数、数据库备份、定时发送短信和邮件,简单的定时任务都可以通过Spring中的 @Scheduled注解来实现。复杂的定时任务可以通过集成Quartz实现。
开启定时任务:
package com.song.songvue;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class SongVueApplication {
public static void main(String[] args) {
SpringApplication.run(SongVueApplication.class, args);
}
}
package com.song.songvue.config.task;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Date;
@Component
public class MySchedule {
/**
* fixedDelay = 1000 每一秒执行一次
*/
@Scheduled(fixedDelay = 1000)
public void fixedDelay() {
System.out.println("fixedDelay: " + DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
}
/**
* fixedRate表示当前任务开始执行两秒后,开启另一个定时任务
*/
/*@Scheduled(fixedRate = 2000)
public void fixedRate() {
System.out.println("fixedRate: " + DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
}*/
/**
* initialDelay: 表示首次执行的延迟时间
*/
@Scheduled(initialDelay = 1000, fixedRate = 2000)
public void initialDelay() {
System.out.println("initialDelay: " + DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
}
@Scheduled(cron = "0 * * * * ?")
public void cron() {
System.out.println("cron, 每分钟执行一次: " + DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
}
/**
* 每天早上五点
*/
@Scheduled(cron = "0 0 5 * * ?")
public void task() {
System.out.println("cron, 每天早上五点执行一次: " + DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
}
}
SpringBoot整合Quartz :
pom.xml添加以下内容:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
package com.song.songvue.config.task;
import org.quartz.CronTrigger;
import org.quartz.JobDataMap;
import org.quartz.SimpleTrigger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.*;
@Configuration
public class QuartzConfig {
/**
* 任务详情
*/
@Bean
MethodInvokingJobDetailFactoryBean jobDetail1() {
MethodInvokingJobDetailFactoryBean bean = new MethodInvokingJobDetailFactoryBean();
bean.setTargetBeanName("myJob");
bean.setTargetMethod("sayHello");
return bean;
}
/**
* 任务详情
*/
@Bean
JobDetailFactoryBean jobDetail2() {
JobDetailFactoryBean bean = new JobDetailFactoryBean();
bean.setJobClass(MySecondJob.class);
JobDataMap jobDataMap = new JobDataMap();
jobDataMap.put("name", "song");
bean.setJobDataMap(jobDataMap);
bean.setDurability(true);
return bean;
}
/**
* 定义触发器,只需要指定Job的实例名和调用的方法即可, 注册这种方法无法在创建JobDetail时传递参数
*/
@Bean
SimpleTriggerFactoryBean simpleTrigger() {
// 简单触发器
SimpleTriggerFactoryBean bean = new SimpleTriggerFactoryBean();
bean.setJobDetail(jobDetail1().getObject());
bean.setRepeatCount(3);
// 任务延迟启动时间
bean.setStartDelay(1000);
// 任务时间间隔
bean.setRepeatInterval(2000);
return bean;
}
/**
* CronTrigger 触发
*/
@Bean
CronTriggerFactoryBean cronTrigger() {
CronTriggerFactoryBean bean = new CronTriggerFactoryBean();
bean.setJobDetail(jobDetail2().getObject());
bean.setCronExpression("* * * * * ?");
return bean;
}
/**
* Trigger有两种实现方式, SimpleTrigger 和 CronTrigger
*/
@Bean
SchedulerFactoryBean schedulerFactory() {
SchedulerFactoryBean bean = new SchedulerFactoryBean();
SimpleTrigger simpleTrigger = simpleTrigger().getObject();
CronTrigger cronTrigger = cronTrigger().getObject();
bean.setTriggers(simpleTrigger,cronTrigger);
return bean;
}
}