实现任务调度分为三大模块:1.任务调度器Scheduler;
2.触发器cronTrigger;
3.自定义任务JobtTask;
*自定义任务JobtTask*
package com.zjt.quartz;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
/**
* 自定义任务;
*/
public class MyJob implements Job {
// 自定义任务Task;
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("当前任务开始执行: " +
new SimpleDateFormat("yyyy-MM-dd HH-mm-ss")
.format(new Date()));
}
}
*测试类*
package com.zjt.quartz;
import java.text.ParseException;
import org.quartz.JobExecutionException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 测试类
*/
public class Test {
@SuppressWarnings("resource")
public static void main(String[] args) throws ParseException, JobExecutionException {
System.out.println("--------------------------------");
new ClassPathXmlApplicationContext("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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"
default-autowire="byName">
<!-- 调度器 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<!-- 触发器列表 -->
<ref bean="cronTrigger" />
</list>
</property>
<!-- 加载配置文件, 如果不配置, 将会使用quartz默认的配置 -->
<property name="configLocation" value="classpath:quartz.properties" />
</bean>
<!-- 触发器 -->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<!-- 指向我们的任务 -->
<property name="jobDetail" ref="myJobtTask" />
<!-- 每天11点1分到59分,每分钟运行一次 -->
<property name="cronExpression" value="0 0/1 9 * * ?" />
</bean>
<!-- 处理类 -->
<bean name="myJobtTask" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="com.zjt.quartz.MyJob" />
</bean>
</beans>
*quartz.properties*
#============================================================================
# Configure Main Scheduler Properties
#============================================================================
org.quartz.scheduler.instanceName = DefaultQuartzScheduler
org.quartz.scheduler.instanceId = AUTO
org.quartz.scheduler.rmi.export = false
org.quartz.scheduler.rmi.proxy = false
org.quartz.scheduler.wrapJobExecutionInUserTransaction = false
#============================================================================
# Configure ThreadPool
#============================================================================
org.quartz.threadPool.threadCount = 10
org.quartz.threadPool.threadPriority = 5
#============================================================================
# Configure JobStore
#============================================================================
org.quartz.jobStore.misfireThreshold = 60000