1. 对比Quartz的优缺点
优点:
- 配置非常简单
缺点:springtask
- 不支持分布式部署
- 不支持动态配置定时任务
- 不支持持久化
其实这几个缺点归根结底都是因为不支持持久化,所以如果项目需要持久化定时任务,还是要选择Quartz比较好。
下面上下用springtask实现的定时功能代码如下:
package org.llyf.test.service;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Component;
//一般Component用在类上面,指明我是一个bean,你spring要管理我
//而@Configurantion跟@Bean一般一起连用,@Bean一般用在方法上面,指明创建实体bean
@Configuration
@EnableScheduling
public class SpringTaskScheduler {
@Bean
public TaskScheduler taskScheduler() {
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.setPoolSize(10);
scheduler.setThreadNamePrefix("wangxiaoer---thread");
return scheduler;
}
}
@Component
class SpringTask2 {
private int count=0;
@Scheduled(cron="*/6 * * * * ?")
public void process() {
System.out.println("["+Thread.currentThread().getName()+"]"+"This is scheduler task running:"+(count++));
}
}
@Component
class SpringTask3 {
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
@Scheduled(fixedRate =6000)
public void reportCurrentTime() {
System.out.println("["+Thread.currentThread().getName()+"]"+"-现在时间:"+dateFormat.format(new Date()));
}
}
在传统的Spring项目中,我们可以在xml配置文件添加task的配置,而在SpringBoot项目中一般使用config配置类的方式添加配置,所以新建一个AsyncConfig类
@Configuration
:表明该类是一个配置类 @EnableAsync
:开启异步事件的支持
然后在定时任务的类或者方法上添加@Async
。最后重启项目,每一个任务都是在不同的线程中
下面来实现下在springboot里用quarzt实现定时任务,
package org.llyf.test.service;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;
@Component
@EnableScheduling
public class Myjob {
private int count=0;
public void task() {
System.out.println("草泥马"+(count++));
}
}
package org.llyf.test.service;
import org.quartz.JobDetail;
import org.quartz.Trigger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
/**
* 创建配置类
* @author 123456
*
*/
@Configuration
public class QuartzConfiguration {
/**
* 定义jobDetail
* @param job
* @return
*/
@Bean(name="firstJobDetail")
public MethodInvokingJobDetailFactoryBean firstJobDetail(Myjob job) {
MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();
//设置是否并发执行
jobDetail.setConcurrent(false);
//设置需要执行的目标对象
jobDetail.setTargetObject(job);
//设置需要执行的的目标对象的 方法
jobDetail.setTargetMethod("task");
return jobDetail;
}
/**
* 配置trigger
* @param firstJobDetail
* @return
*/
@Bean(name="firstTrigger")
public CronTriggerFactoryBean firstTrigger(JobDetail firstJobDetail) {
CronTriggerFactoryBean trigger = new CronTriggerFactoryBean();
trigger.setJobDetail(firstJobDetail);
trigger.setCronExpression("*/4 * * * * ?");
return trigger;
}
@Bean(name="scheduler")
public SchedulerFactoryBean schedulerFactory(Trigger firstTrigger) {
SchedulerFactoryBean bean = new SchedulerFactoryBean();
//设置延迟启动,
bean.setStartupDelay(10);
bean.setTriggers(firstTrigger);
return bean;
}
}