前两种java自带方式
1.Timer
(1) Timer().schedule(执行的任务,开始执行时间,间隔执行时间)
下面意思是在启动3秒后开始执行TimerTask中的任务,每隔4秒重复执行一次
public static void testTimer() {
new Timer().schedule(new TimerTask() {
@Override
public void run() {
System.out.println("任务");
}
},
3000,
4000
);
}
(2) Timer().scheduleAtFixedRate(执行的任务,开始执行时间(日期),间隔执行时间)
下面意思是在2022-09-02 10:39:39后开始执行TimerTask中的任务,每隔4秒重复执行一次
public static void testTimer() {
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date parse =null;
try {
parse = format.parse("2022-09-02 10:39:39");
} catch (ParseException e) {
e.printStackTrace();
}
new Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
System.out.println("任务");
}
},
parse,
4000
);
}
2.Executors
通过单线程定时执行器执行,需要上一步任务执行完成才能执行下一个任务,如果有一个出现异常则全部停止
(1) executor.scheduleAtFixedRate(执行的任务,开始执行时间,间隔执行时间,日期格式[年、天、小时、分钟等])
下面意思是在启动1秒后开始执行TimerTask中的任务,每隔4秒重复执行一次,日期格式是秒
public static void testScheduledExecutorService() {
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
executor.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
System.out.println("任务");
}
},
1,
4,
TimeUnit.SECONDS
);
}
(2) executor.scheduleWithFixedDelay(执行的任务,开始执行时间,间隔执行时间,日期格式[年、天、小时、分钟等])
下面意思是在启动1秒后开始执行TimerTask中的任务,延迟4秒重复执行一次,日期格式是秒
public static void testScheduledExecutorService() {
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
executor.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
System.out.println("任务");
}
},
1,
4,
TimeUnit.SECONDS
);
}
3.Spring Scheduled
spring定时任务执行比较简单,需要了解cron表达式
需要在配置定时任务的配置类上加上@EnableScheduling注解开启定时器
@Scheduled(cron = "0/4 * * * * ?")
public static void testSpring() {
System.out.println("任务");
}
4.Quertz
需要导入的包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.2.12.RELEASE</version>
</dependency>
这里我写了两种方式,一种是xml方式配置,一种是配置类方式,两者区别不大
创建任务
@Service
public class ScheduleConfig {
public void testQuertz() {
System.out.println("任务");
}
}
下面是配置
首先看xml方式的
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 扫描指定package下所有组件注解并注册为Spring Beans -->
<context:component-scan base-package="com.example.demo.config" />
<bean id="ScheduleConfig" class="com.example.demo.config.ScheduleConfig"/>
<!-- 配置执行任务的调度对象 -->
<bean id="ExpireJobTaskJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="ScheduleConfig" />
<property name="targetMethod" value="testQuertz" />
</bean>
<!-- 配置任务的执行日历 cron编码 -->
<bean id="ExpireJobTaskJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="ExpireJobTaskJobDetail" />
<property name="cronExpression" value="0/4 * * * * ?" />
</bean>
<!-- 设置调度 -->
<bean name="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="ExpireJobTaskJobTrigger" />
</list>
</property>
</bean>
</beans>
但是要注意如果是springboot默认是不加载xml格式的所以要配置扫描xml
package com.example.demo.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
@ImportResource(locations = "classpath:application-context.xml")
public class WebMVCConfig implements WebMvcConfigurer {
}
下面是配置类方式的
package com.example.demo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import java.util.Date;
@Configuration
public class QuertzConfig {
/**
* 配置执行任务的调度对象
* @return
*/
@Bean(name = "ExpireJobTaskJobDetail")
public MethodInvokingJobDetailFactoryBean getExpireJobTaskJobDetail(){
MethodInvokingJobDetailFactoryBean bean = new MethodInvokingJobDetailFactoryBean();
bean.setTargetObject(new ScheduleConfig());
bean.setTargetMethod("testQuertz");
return bean;
}
/**
* 配置任务的执行日历 cron编码
* @param jobDetail
* @return
*/
@Bean(name = "ExpireJobTaskJobTrigger")
public CronTriggerFactoryBean getExpireJobTaskJobTrigger(MethodInvokingJobDetailFactoryBean jobDetail){
CronTriggerFactoryBean bean = new CronTriggerFactoryBean();
bean.setJobDetail(jobDetail.getObject());
bean.setCronExpression("0/4 * * * * ?");
return bean;
}
/**
* 设置调度
* @param cronTriggerFactoryBean
* @return
*/
@Bean
public SchedulerFactoryBean startQuertz(CronTriggerFactoryBean cronTriggerFactoryBean){
SchedulerFactoryBean bean = new SchedulerFactoryBean( );
bean.setTriggers(cronTriggerFactoryBean.getObject());
return bean;
}
}