转载于:https://blog.csdn.net/hao7030187/article/details/79077464
1. 对比Quartz的优缺点
优点:
配置非常简单
缺点:
不支持分布式部署
不支持动态配置定时任务
不支持持久化
其实这几个缺点归根结底都是因为不支持持久化,所以如果项目需要持久化定时任务,还是要选择Quartz比较好。
(我用的是spring boot2.1 可以支持jpa操作呀?这不是持久化吗...文末展示)
2. SpringBoot整合及使用
在我们的项目开发过程中,经常需要定时任务来帮助我们来做一些内容,springboot默认已经帮我们实现了,只需要添加相应的注解就可以实现
2.1. pom配置
pom文件里面并不需要引入什么依赖,Spring默认提供。
2.2. 启动类启用定时
在启动类上面加上@EnableScheduling即可开启定时
@Configuration
@EnableScheduling
public class SchedulerConfig {
}
2.3. 创建定时任务实现类
定时任务1:
@Component
public class TestTask1 {
private int count=0;
@Scheduled(cron="*/6 * * * * ?")
private void process(){
System.out.println("[" + Thread.currentThread().getName() + "]" + "this is scheduler task runing "+(count++));
}
}
定时任务2:
@Component
public class TestTask2 {
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()));
}
}
结果如下:
[pool-4-thread-1]this is scheduler task runing 0
[pool-4-thread-1]现在时间:17:07:10
[pool-4-thread-1]this is scheduler task runing 1
[pool-4-thread-1]现在时间:17:07:16
[pool-4-thread-1]this is scheduler task runing 2
[pool-4-thread-1]现在时间:17:07:22
[pool-4-thread-1]this is scheduler task runing 3
[pool-4-thread-1]现在时间:17:07:28
2.3. 参数说明
@Scheduled所支持的参数:
1. cron:cron表达式,指定任务在特定时间执行;
2. fixedDelay:表示上一次任务执行完成后多久再次执行,参数类型为long,单位ms;
3. fixedDelayString:与fixedDelay含义一样,只是参数类型变为String;
4. fixedRate:表示按一定的频率执行任务,参数类型为long,单位ms;
5. fixedRateString: 与fixedRate的含义一样,只是将参数类型变为String;
6. initialDelay:表示延迟多久再第一次执行任务,参数类型为long,单位ms;
7. initialDelayString:与initialDelay的含义一样,只是将参数类型变为String;
8. zone:时区,默认为当前时区,一般没有用到。
3. 设置多线程
从上图的输出结果可以看到,两个定时任务使用的是同一个线程,这是因为SpringTask默认是单线程的。
在实际开发过程中,我们当然不希望所有的任务都运行在一个线程中。
想要改成多线程,给SpringTask提供一个多线程的TaskScheduler即可,Spring已经有默认实现。
@Configuration
@EnableScheduling
public class SchedulerConfig {
@Bean
public TaskScheduler taskScheduler() {
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
//线程池大小
scheduler.setPoolSize(10);
//线程名字前缀
scheduler.setThreadNamePrefix("spring-task-thread");
return scheduler;
}
}
改造后的输出结果:
[spring-task-thread1]this is scheduler task runing 0
[spring-task-thread2]现在时间:17:15:50
[spring-task-thread3]this is scheduler task runing 1
[spring-task-thread1]现在时间:17:15:56
[spring-task-thread4]this is scheduler task runing 2
[spring-task-thread2]现在时间:17:16:02
[spring-task-thread5]this is scheduler task runing 3
[spring-task-thread3]现在时间:17:16:08
------------------------------------下面是用jpa查询然后修改保存,想用来做定时更新的----------------------------------------
@Component
public class TestTask {
@Autowired
private ADao aDao;
private int i = 10000;
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
@Scheduled(cron = "0/1 * * * * ?")
private void task1() {
i++;
A a = aDao.getById(i);
a.setText(a.getText()+"----"+i);
a = aDao.save(a);
String string = i + (a==null?"null":a.toString());
System.out.println("[" + Thread.currentThread().getName() + "][" + dateFormat.format(new Date()) + "]" + string);
}
}
运行结果:
2018-12-25 11:23:35.050 INFO 93128 --- [ng-task-thread1] o.h.h.i.QueryTranslatorFactoryInitiator : HHH000397: Using ASTQueryTranslatorFactory
Hibernate: select a0_.id as id1_0_, a0_.href as href2_0_, a0_.text as text3_0_ from a a0_ where a0_.id=?
Hibernate: select a0_.id as id1_0_0_, a0_.href as href2_0_0_, a0_.text as text3_0_0_ from a a0_ where a0_.id=?
Hibernate: update a set href=?, text=? where id=?
[spring-task-thread1][11:23:35]10001A [id=10001, href=//v.qq.com/x/cover/4q4rst615ir544d.html, text=太空救援----10001]
Hibernate: select a0_.id as id1_0_, a0_.href as href2_0_, a0_.text as text3_0_ from a a0_ where a0_.id=?
Hibernate: select a0_.id as id1_0_0_, a0_.href as href2_0_0_, a0_.text as text3_0_0_ from a a0_ where a0_.id=?
Hibernate: update a set href=?, text=? where id=?
[spring-task-thread2][11:23:36]10002A [id=10002, href=https://v.qq.com/x/cover/gjxf5ibuuljjvav.html, text=远古外星人----10002]
Hibernate: select a0_.id as id1_0_, a0_.href as href2_0_, a0_.text as text3_0_ from a a0_ where a0_.id=?
Hibernate: select a0_.id as id1_0_0_, a0_.href as href2_0_0_, a0_.text as text3_0_0_ from a a0_ where a0_.id=?
Hibernate: update a set href=?, text=? where id=?
[spring-task-thread3][11:23:37]10003A [id=10003, href=javascript: void 0;, text= <i class="icon icon_ellipsis"><svg class="svg_icon" viewBox="0 0 20 20" width="20" height="20"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#svg_icon_ellipsis"></use></svg></i> <div class="figure_action_menu"> <span class="menu_text" data-feedback="true" _stat="rec-shortvideo:watched-23" data-action="watched">我已看过</span> <span class="menu_text" data-feedback="true" _stat="rec-shortvideo:del-23" data-action="unlike">不感兴趣</span> </div>----10003]
2018-12-25 11:23:37.688 INFO 93128 --- [on(2)-127.0.0.1] inMXBeanRegistrar$SpringApplicationAdmin : Application shutdown requested.