quartz学习笔记
1,quick start
第一步:创建maven项目
管理依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.simple.projects</groupId> <artifactId>quartztest</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.3</version> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz-jobs</artifactId> <version>2.2.3</version> </dependency> </dependencies> <build/> </project> |
第二步:利用官网的quickstart先跑起来
http://www.quartz-scheduler.org/documentation/quartz-2.1.x/quick-start.html |
package cn.simple.quartz.test;
import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.impl.StdSchedulerFactory;
public class QuartzQuickStart { public static void main(String[] args) {
try { // Grab the Scheduler instance from the Factory Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); // and start it off scheduler.start(); scheduler.shutdown(); } catch (SchedulerException se) { se.printStackTrace(); } } } |
运行结果:
2,简易例子
详情参考:http://www.quartz-scheduler.org/documentation/quartz-2.1.x/tutorials/tutorial-lesson-01.html
目标:每五秒在控制台上打印语句
第一步:编写job任务,实现了job接口的任务类
HelloJob.java
package cn.simple.quartz.sample;
import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException;
public class HelloJob implements Job{
@Override public void execute(JobExecutionContext arg0) throws JobExecutionException { System.out.println("我来证明任务在执行..."); }
} |
第二步:编写任务执行类
QuartzSampleTest.
package cn.simple.quartz.sample;
import org.quartz.JobBuilder; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.SchedulerFactory; import org.quartz.SimpleScheduleBuilder; import org.quartz.Trigger; import org.quartz.TriggerBuilder; import org.quartz.impl.StdSchedulerFactory;
public class QuartzSampleTest { public static void main(String[] args) { //拿到调度工厂 SchedulerFactory schedulerFactory = new StdSchedulerFactory(); //拿到scheduler try { Scheduler scheduler = schedulerFactory.getScheduler(); //开启调度 scheduler.start(); //包装job JobDetail job = JobBuilder.newJob(HelloJob.class) //用来分组命名 .withIdentity("job1", "group1") //创建 .build(); //创建触发,定义何时,如何运行 Trigger trigger = TriggerBuilder.newTrigger() .withIdentity("trigger1", "group1") .startNow() .withSchedule( SimpleScheduleBuilder.simpleSchedule() .repeatForever() .withIntervalInSeconds(5) ) .build(); //添加到scheduler scheduler.scheduleJob(job, trigger); } catch (SchedulerException e) { e.printStackTrace(); } } } |
运行结果
3,同spring整合,并使用计划任务CronTrigger
查阅spring-framework-reference.pdfspring整合文档中的Using the Quartz Scheduler
第一步:导入依赖
<!-- spring context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.9.RELEASE</version> </dependency> <!-- spring context support --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.2.9.RELEASE</version> </dependency> <!-- spring web --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.2.9.RELEASE</version> </dependency> <!-- spring tx --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.2.9.RELEASE</version> </dependency> <!-- quartz核心 --> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.3</version> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz-jobs</artifactId> <version>2.2.3</version> </dependency> <!-- slf4j log4j --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.7</version> </dependency>
|
第二步:配置web.xml,设置监听器
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>storemanager</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 整合spring容器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- spring容器位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param>
</web-app> |
创建applicationContext.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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置扫描 --> <context:component-scan base-package="cn.simple.quartz.service"/> <!-- 创建包装job --> <bean id="myJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"> <property name="jobClass" value="cn.simple.quartz.job.MyJob"></property> </bean> <!-- 配置crontrigger --> <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="myJobDetail"/> <!-- run every morning at 6 AM --> <property name="cronExpression" value="0/5 * * * * ?"/> </bean> <!-- 配置调度器 --> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="cronTrigger"/> </list> </property> </bean> </beans> |
Tomcat:run调试:
结果如下