quartz和spring整合
添加spring依赖和quartz依赖
pom文件
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
创建HelloJob 为任务类
@Service
public class HelloJob{
private static final SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public void task() {
Date now = new Date();
String currentTime = sdf.format(now);
System.out.println("执行时间为:"+currentTime);
}
}
配置类
<?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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 开启注解支持,并扫描指定包下的注解 -->
<context:annotation-config />
<context:component-scan base-package="com.xuxu"/>
<!-- 配置需要定时执行的任务类以及方法 -->
<bean id="doJob"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 指定任务类 -->
<property name="targetObject" ref="helloJob" />
<!-- 指定任务执行的方法 -->
<property name="targetMethod" value="task" />
</bean>
<!-- 配置触发器 -->
<bean id="jobTrigger"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="doJob" />
<!-- 每10秒运行一次 -->
<property name="cronExpression" value="0/10 * * * * ?" /><!-- 每隔10秒钟触发一次 -->
</bean>
<!-- 触发定时任务 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="jobTrigger" /><!-- 此处可以配置多个触发器 -->
</list>
</property>
</bean>
</beans>
启动类
public class TestSpringQuartz {
public static void main(String[] args) {
ClassPathXmlApplicationContext app =
new ClassPathXmlApplicationContext("/spring/applicationContext.xml");
}
}
运行情况
执行时间为:2019-04-16 14:52:50
执行时间为:2019-04-16 14:53:00
执行时间为:2019-04-16 14:53:10
SpringBoot
1springboot Spring Task定时任务
1.我们使用spring-boot作为基础框架,其理念为零配置文件,所有的配置都是基于注解和暴露bean的方式。
2.使用spring的定时器:
spring自带支持定时器的任务实现。其可通过简单配置来使用到简单的定时任务。
@Component
@Configurable
@EnableScheduling
public class TestSpringQuartz {
@Scheduled(fixedRate = 1000 * 30)
public void reportCurrentTime(){
System.out.println ("Scheduling Tasks Examples: The time is now " + dateFormat ().format (new Date ()));
}
//每10秒执行一次
@Scheduled(cron = "0/10 * * * * ?")
public void reportCurrentByCron(){
System.out.println ("Scheduling Tasks Examples By Cron: The time is now " + dateFormat ().format (new Date ()));
}
private SimpleDateFormat dateFormat(){
return new SimpleDateFormat ("HH:mm:ss");
}
}
没错,使用spring的定时任务就这么简单,其中有几个比较重要的注解:
@EnableScheduling:标注启动定时任务。
@Scheduled(fixedRate = 1000 * 30) 定义某个定时任务。
2springboot Quartz定时任务
Quartz设计者做了一个设计选择来从调度分离开作业。Quartz中的触发器用来告诉调度程序作业什么时候触发。框架提供了一把触发器类型,但两个最常用的是SimpleTrigger和CronTrigger。
由于我们使用的是spring-boot框架,其目的是做到零配置文件,所以我们不使用xml文件的配置文件来定义一个定时器,而是使用向spring容器暴露bean的方式。
向spring容器暴露所必须的bean
springTimerTask和Quartz区别
精确度和功能
Quartz可以通过cron表达式精确到特定时间执行,而TimerTask不能。
如果指定时间会报错
@Scheduled(cron = "0 0,36 15 16 4 ? *")
public void reportCurrentByCron(){
System.out.println ("Scheduling Tasks Examples By Cron: The time is now " + dateFormat ().format (new Date ()));
}
Caused by: java.lang.IllegalStateException:
Encountered invalid @Scheduled method 'reportCurrentByCron':
Cron expression must consist of 6 fields (found 7 in "0 0,36 15 16 4 ? *")
Quartz拥有TimerTask所有的功能,而TimerTask则没有。
任务类的数量
Quartz每次执行都创建一个新的任务类对象。 TimerTask则每次使用同一个任务类对象。
对异常的处理 Quartz的某次执行任务过程中抛出异常,不影响下一次任务的执行,当下一次执行时间到来时,定时器会再次执行任务。 TimerTask不同,一旦某个任务在执行过程中抛出异常,则整个定时器生命周期就结束,以后永远不会再执行定时器任务。