Quartz
=========
1
官网:
http://quartz-scheduler.org/
下载地址:
http://d2zwv9pap9ylyd.cloudfront.net/quartz-2.1.0.tar.gz
2
简介:
开源免费, Apache 2.0 license.
创建简单或复杂的作业调度服务, 整合或单独使用于JAVA应用.支持JTA事务和集群.
3
使用场景:
传递工作流
数据库系统维护
提醒服务
...
4
Quartz JAR 文件
quartz-all-xxx.jar 全特性包
依赖包:
lib/*.jar
5
Properties 文件 quartz.properties
http://quartz-scheduler.org/documentation/quartz-2.x/configuration/
6
示例:
Quartz.java
package cn.bisoft.component.quartz;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import cn.bisoft.component.quartz.job.HelloJob;
/**
* Scheduler - 调度器, 用于调度Job.
* Job - 调度任务.
* JobDetail - 创建Job.
* Trigger - 触发运行规则.
* JobBuilder - 创建JobDetail
* TriggerBuilder - 创建Trigger.
* ScheduleBuilder - 创建Trigger规则.
*
*/
public class Quartz
{
public static void main(String[] args)
{
usage();
}
public static void usage()
{
try
{
/*
* 1. 创建Scheduler, 通过 JNDI或 StdSchedulerFactory, 不推荐使用DirectSchedulerFactory.
* Scheduler 四种状态: stand-by, start, pause, shutdown.
*/
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
// 2. Start Scheduler
scheduler.start();
// 3. 定义Job
JobDetail job = JobBuilder.newJob(HelloJob.class).withIdentity("job1", "group1").build();
// 4. 定义Trigger, 每3秒执行一次
Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1").startNow()
.withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(3).repeatForever())
.build();
// 5. 绑定Job 和 Trigger
scheduler.scheduleJob(job, trigger);
// 6. Shutdown Scheduler
// scheduler.shutdown();
}
catch (SchedulerException se)
{
se.printStackTrace();
}
}
}
HelloJob.java
package cn.bisoft.component.quartz.job;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.PersistJobDataAfterExecution;
@DisallowConcurrentExecution
@PersistJobDataAfterExecution
public class HelloJob implements Job
{
public HelloJob()
{
}
public void execute(JobExecutionContext context) throws JobExecutionException
{
System.err.println("Hello! HelloJob is executing.");
}
}
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{yyyy-MM-dd hh:mm:ss.SSS} %t [%c]%n%m%n%n"/>
</layout>
</appender>
<logger name="org.quartz">
<level value="debug" />
</logger>
<root>
<level value="debug" />
<appender-ref ref="default" />
</root>
</log4j:configuration>
quartz.properties
org.quartz.scheduler.instanceName = MyScheduler
org.quartz.threadPool.threadCount = 3
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore