quartz-2.1.5示例:
1、下载quartz
2、解压后将lib中的jar包、jta.jar和quartz-all-2.1.5.jar导入
3、编写job
package com.test; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class DemoJob implements Job{ static Logger logger = LoggerFactory.getLogger(DemoJob.class); public DemoJob() { } public void execute(JobExecutionContext context) throws JobExecutionException { logger.info("=========执行=================="); } }
4、导入log4j.xml和编写配置文件
导入log4j.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <appender name="default" class="org.apache.log4j.ConsoleAppender"> <param name="target" value="System.out"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="[%p] %d{dd MMM hh:mm:ss.SSS aa} %t [%c]%n%m%n%n"/> </layout> </appender> <logger name="org.quartz"> <level value="info" /> </logger> <root> <level value="info" /> <appender-ref ref="default" /> </root> </log4j:configuration>
编写quartz.properties
#===============================================================
#Configure Main Scheduler Properties
#===============================================================
org.quartz.scheduler.instanceName = QuartzScheduler
org.quartz.scheduler.instanceId = AUTO
#===============================================================
#Configure ThreadPool
#===============================================================
org.quartz.threadPool.threadCount = 5
org.quartz.threadPool.threadPriority = 5
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
#===============================================================
#Configure JobStore
#===============================================================
#===============================================================
#Configure Plugins
#===============================================================
org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin
org.quartz.plugin.jobInitializer.fileNames =jobs.xml
org.quartz.plugin.jobInitializer.failOnFileNotFound =true
5、编写jobs.xml
<?xml version="1.0" encoding="UTF-8"?> <job-scheduling-data xmlns="http://www.quartz-scheduler.org/xml/JobSchedulingData" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.quartz-scheduler.org/xml/JobSchedulingData http://www.quartz-scheduler.org/xml/job_scheduling_data_1_8.xsd" version="1.8"> <schedule> <job> <name>DemoJob</name> <group>DEFAULT</group> <description>A job just for test</description> <job-class>com.test.DemoJob</job-class> </job> <trigger> <!-- <simple> <name>HeiTrigger</name> <group>DEFAULT</group> <job-name>DemoJob</job-name> <job-group>DEFAULT</job-group> <repeat-count>-1</repeat-count> <repeat-interval>10000</repeat-interval> </simple> --> <cron> <name>Trigger</name> <group>DEFAULT</group> <job-name>DemoJob</job-name> <job-group>DEFAULT</job-group> <cron-expression>0 11 17 * * ?</cron-expression> </cron> </trigger> </schedule> </job-scheduling-data>
有两种模式:一种是每个多久执行任务,如上面是10秒,执行次数为无限次。二种是每天在什么时候执行任务,上面代码表示的是每天下午5点11分执行任务。
6、编写DemoSchedular.javapackage com.test;
import java.util.Date; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.SchedulerFactory; import org.quartz.impl.StdSchedulerFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class DemoSchedular { static Logger logger = LoggerFactory.getLogger(DemoSchedular.class); public void test() { try { SchedulerFactory schedulerFactory = new StdSchedulerFactory(); Scheduler scheduler = schedulerFactory.getScheduler(); scheduler.start(); logger.info("==========started at " + new Date() + "=========="); } catch (SchedulerException e) { e.printStackTrace(); } } public static void main(String[] args) { DemoSchedular demoSchedular = new DemoSchedular(); demoSchedular.test(); } }