文章目录
分类
- Java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务
- 使用Quartz,这是一个功能比较强大的的调度器,可以让你的程序在指定时间执行,也可以按照某一个频度执行
- Spring3.0以后自带的task,可以将它看成一个轻量级的Quartz
- xxl-job,是一个分布式任务调度平台
- OhMyScheduler,是基于Akka架构的新一代分布式调度与计算框架
任务调度触发时机都可分为两种(2和3都具备):
- 每隔指定时间则触发一次
- 每到指定时间则触发一次
spring3.0以后自带task
xmlns中添加task定义
xmlns:task=“http://www.springframework.org/schema/task”
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd
注解配置
spring
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd">
<!-- 引入任务调度器 -->
<task:annotation-driven />
<!-- 扫描注解 -->
<context:component-scan base-package="com.thunisoft.shop.service.impl" />
</beans>
SpringTaskServiceImpl
@Component
public class SpringTaskServiceImpl {
@Scheduled(cron = "0/10 * * * * ?")
//需要注意@Scheduled这个注解,它可配置多个属性:cron\fixedDelay\fixedRate
public void doJob() {
LocalDateTime time = LocalDateTime.now();
System.out.println("每10秒执行一次:" +time);
}
}
xml配置
spring
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd">
<!-- 进行定时任务的类,将其定义为一个bean -->
<bean id="springTaskServiceImpl" class="com.thunisoft.shop.service.impl.SpringTaskServiceImpl"></bean>
<!-- 通过task标签,定义定时功能 -->
<task:scheduled-tasks>
<task:scheduled ref="springTaskServiceImpl" method="doJob" cron="0/10 * * * * ?" />
<task:scheduled ref="springTaskServiceImpl" method="doJob1" fixed-rate="5000" />
</task:scheduled-tasks>
</beans>
SpringTaskServiceImpl
@Component
public class SpringTaskServiceImpl {
public void doJob() {
LocalDateTime time = LocalDateTime.now();
System.out.println("每10秒执行一次job:" +time);
}
public void doJob1() {
LocalDateTime time = LocalDateTime.now();
System.out.println("每50秒执行一次job1:" +time);
}
}
Quartz定时器
有两种存储类型
内存
持久化(存到数据库)
内存
maven引入jar包
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.0</version>
</dependency>
spring配置
org.springframework.scheduling.quartz.SimpleTriggerFactoryBean
按照一定频率调用任务,如:每隔30分钟运行一次
<bean id="doTime" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
<property name="jobDetail" ref="jobTask" />
<property name="startDelay" value="0" /><!-- 调度工厂实例化后,经过0秒开始执行调度 -->
<property name="repeatInterval" value="2000" /><!-- 每2秒调度一次 -->
</bean>
org.springframework.scheduling.quartz.CronTriggerFactoryBean
指定时间运行一次,如:每天12:00运行一次
<!-- 实现定时任务接口启动类 -->
<bean id="cleanImageTask" class="com.thunisoft.shop.service.impl.CleanImageTaskServiceImpl"></bean>
<!-- 定义调用对象和调用对象的方法 -->
<bean id="jobTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类 -->
<property name="targetObject">
<ref bean="cleanImageTask"/>
</property>
<!-- 调用类中的方法 -->
<property name="targetMethod">
<value>doJob</value>
</property>
</bean>
<!-- 定义触发时间 -->
<bean id="doTime" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail">
<ref bean="jobTask"/>
</property>
<!-- cron表达式 -->
<property name="cronExpression">
<!-- 每天凌晨12点触发执行一次 -->
<value>0 0 0 * * ?</value>
<!-- 每5秒触发执行一次 -->
<!--<value>*/5 * * * * ?</value>-->
</property>
</bean>
<!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
<bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="doTime"/>
</list>
</property>
</bean>
CleanImageTaskServiceImpl
public class CleanImageTaskServiceImpl {
/**
* 定时任务入口方法
* @throws SchedulerException 任务异常
*/
public void doJob() throws SchedulerException {
//要执行的任务...
}
}
持久化
参考:https://blog.youkuaiyun.com/bicheng4769/article/details/85263602
XXL-JOB
参看 https://www.xuxueli.com
SchedulerX 2.0
百度
PowerJob
源码:https://github.com/KFCFans/OhMyScheduler
说明文档:https://www.yuque.com/powerjob/guidence/ztn4i5
文档:https://kfcfans.github.io/