《quartz插件的使用》由会员分享,可在线阅读,更多相关《quartz插件的使用(7页珍藏版)》请在金锄头文库上搜索。
1、 Quartz 插件的使用Quartz 框架提供了几种用于扩展平台能力的方式。通过使用各种 钩子 (通常指的就是扩展点),Quartz 变得很容易被扩展和定制化来适应你的需要。其中一个最简单的扩展框架的方法就是使用 Quartz 插件。本章就来看看如何使用插件机制让 Quartz 进入到之前 Quartz 用户没去过的领域。一、使用 Quartz 自带插件范例下面将使用插件实现从 XML 得到 JOB,不用手工的把 job 和 trigger 加到 Scheduler 中去再来运行,这样就很方便。如果要更改任务,直接在 xml 中配置就可以了。不用再写 Java代码。1、首先是 job 类:j
2、ava view plain copypackage com.mucfc; import java.util.Date; import java.util.Set; import org.quartz.DisallowConcurrentExecution; import org.quartz.InterruptableJob; import org.quartz.Job; import org.quartz.JobDataMap; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; i
3、mport org.quartz.JobKey; import org.quartz.PersistJobDataAfterExecution; import org.quartz.UnableToInterruptJobException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; PersistJobDataAfterExecution DisallowConcurrentExecution public class SimpleJob implements Job private static Logger log
4、= LoggerFactory.getLogger(SimpleJob.class); private JobKey jobkey=null; Override public void execute(JobExecutionContext context) throws JobExecutionException jobkey = context.getJobDetail().getKey(); log.info(Excuting job: + jobkey + executing at + new Date()+ fire by: +context.getTrigger().getKey(
5、); if(context.getMergedJobDataMap().size()0) Set keys= context.getMergedJobDataMap().keySet(); for (String key : keys) String value= context.getMergedJobDataMap().getString(key); log.info( jobdatamap entry: +key+ = +value); context.setResult(hello); 2、 job 触发时间配置,使用 quartz_data.xmlhtml view plain co
6、py* * true false TestJob1 com.mucfc.SimpleJob TestDurableJob com.mucfc.SimpleJob true false TestSimpleTrigger1AtFiveSecondInterval TestJob1 -1 5000 TestJob2 GroupOfTestJob2 This is the description of TestJob2 com.mucfc.SimpleJob false true someKey someValue someOtherKey someOtherValue TestSimpleTrig
7、ger2AtTenSecondIntervalAndFiveRepeats GroupOfTestJob2Triggers TestJob2 GroupOfTestJob2 2010-02-09T10:15:00 MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_EXISTING_REPEAT_COUNT 5 10000 TestCronTrigger2AtEveryMinute GroupOfTestJob2Triggers TestJob2 GroupOfTestJob2 someKey overriddenValue someOtherKey someOth
8、erOverriddenValue 0 * * ? * * TestCronTrigger2AtEveryMinuteOnThe45thSecond GroupOfTestJob2Triggers TestJob2 GroupOfTestJob2 2015-05-12T12:26:00.0 2015-05-16T12:26:00.0 MISFIRE_INSTRUCTION_SMART_POLICY 45 * * ? * * America/Los_Angeles 3、配置文件plain view plain copy#= # Configure Main Scheduler Propertie
9、s #= org.quartz.scheduler.instanceName: TestScheduler org.quartz.scheduler.instanceId: AUTO org.quartz.scheduler.skipUpdateCheck: true #= # Configure ThreadPool #= org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool org.quartz.threadPool.threadCount: 3 org.quartz.threadPool.threadPriority
10、: 5 #= # Configure JobStore #= org.quartz.jobStore.misfireThreshold: 60000 org.quartz.jobStore.class: org.quartz.simpl.RAMJobStore #org.quartz.jobStore.class: org.quartz.impl.jdbcjobstore.JobStoreTX #org.quartz.jobStore.driverDelegateClass: org.quartz.impl.jdbcjobstore.PostgreSQLDelegate #org.quartz
11、.jobStore.useProperties: false #org.quartz.jobStore.dataSource: myDS #org.quartz.jobStore.tablePrefix: QRTZ_ #org.quartz.jobStore.isClustered: false #= # Configure Datasources #= #org.quartz.dataSource.myDS.driver: org.postgresql.Driver #org.quartz.dataSource.myDS.URL: jdbc:postgresql:/localhost/dev
12、 #org.quartz.dataSource.myDS.user: jhouse #org.quartz.dataSource.myDS.password: #org.quartz.dataSource.myDS.maxConnections: 5 #= # Configure Plugins #= org.quartz.plugin.triggHistory.class: org.quartz.plugins.history.LoggingJobHistoryPlugin org.quartz.plugin.jobInitializer.class: org.quartz.plugins.
13、xml.XMLSchedulingDataProcessorPlugin org.quartz.plugin.jobInitializer.fileNames: quartz_data.xml org.quartz.plugin.jobInitializer.failOnFileNotFound: true org.quartz.plugin.jobIscanInterval: 120 org.quartz.plugin.jobInitializer.wrapInUserTransaction: false 其中 org.quartz.plugin.jobInitializer.class: org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin 会自动加载 quartz_data.xml 中的trigger 和 jo