1、Quartz是一个任务调度框架。在某一个有规律的时间点干某件事。并且时间的触发的条件可以非常复杂(比如每月最后一个工作日的17:50),复杂到需要一个专门的框架来干这个事。你给它一个触发条件的定义,它负责到了时间点,触发相应的Job起来干活。
2、整合ssm的一个简单的例子
pom.xml加入依赖包
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.2.2</version>
</dependency>
配置spring:spring-quartz.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.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">
<!-- >>>>>>>>>>>>>>>>>>>>>>>>>>>>>任务调度配置<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< -->
<!-- 实例化bean -->
<bean id="demo" class="你的包路径.QuartzDemo" />
<!-- 配置MethodInvokingJobDetailFactoryBean -->
<bean id="testTaskMethod"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="demo" />
<property name="targetMethod" value="execute" />
<property name="concurrent" value="false" />
</bean>
<!-- ============= 调度触发器 ============= -->
<bean id="articleBatchRunTrigger"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="testTaskMethod" />
<!-- 每隔1分钟执行一次 -->
<property name="cronExpression" value="0 0/1 * * * ?" />
</bean>
<!--其他执行执行规则可以自行查找-->
<!-- ============= 调度工厂 ============= -->
<bean id="schedulerBean"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean"
lazy-init="false">
<property name="triggers">
<list>
<ref bean="articleBatchRunTrigger" />
</list>
</property>
</bean>
</beans>
demo.java
public class QuartzDemo {
public void execute() throws Exception {
System.out.println("定时demo:"+new Date() );
}
}