1.Quartz是什么
Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用。Quartz可以用来创建简单或为运行十个,百个,甚至是好几万个Jobs这样复杂的程序。Jobs可以做成标准的Java组件或 EJBs。
1.1 Quartz框架的使用思路
1)job - 任务 - 你要做什么事?
2)Trigger - 触发器 - 你什么时候去做?
3)Scheduler - 任务调度 - 你什么时候需要去做什么事?
1.2 Quartz框架的使用
1.2.1 添加依赖
pom.xml添加依赖(父工程)
<properties>
... ...
<quartz.version>2.2.2</quartz.version>
</properties>
<dependencyManagement>
.......
<!-- quartz任务调度框架 -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>${quartz.version}</version>
</dependency>
</dependencyManagement>
pom.xml添加依赖(订单web工程)
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
</dependency>
1.2.2 job类
public class JobDemo implements Job{
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println(new Date()+"------------------------------------");
}
}
1.2.3 测试类
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
public class QuartzTest {
public static void main(String[] args) throws SchedulerException {
/**
* job:任务类 做什么事
* trigger:触发器 什么时候做
* scheduled:任务调度容器 什么时候做什么事
*/
//创建job对象
JobDetail job = JobBuilder.newJob(JobDemo.class).build();
//创建trigger对象
CronTrigger trigger = TriggerBuilder.newTrigger().withSchedule(
CronScheduleBuilder.cronSchedule("*/1****?")).build();
//创建scheduled对象
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
scheduler.scheduleJob(job, trigger);
//启动
scheduler.start();
}
}
2.Spring整合Quartz框架
2.1 job类
public class JobDemo2{
public void execute(){
System.out.println(new Date()+"------------------------------------");
}
}
2.2 applicationContext-task.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:task="http://www.springframework.org/schema/task"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd">
<!--1、job:任务类 -->
<bean id="job" class="com.ego.quartz.JobDemo2"></bean>
<bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="job"></property>
<property name="targetMethod" value="execute"></property>
</bean>
<!--2、trigger:触发器 -->
<bean id="trigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="jobDetail"></property>
<property name="cronExpression" value="*/1 * * * * ?"></property>
</bean>
<!-- 3、scheduled:调度容器(job,triiger) -->
<bean id="scheduled" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="trigger"/>
</list>
</property>
</bean>
</beans>
2.3 测试类
public class QuartzTest2 {
public static void main(String[] args) throws IOException {
new ClassPathXmlApplicationContext("classpath:applicationContext-task.xml");
System.in.read();
}
}