文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

quartz2.2 官方例子3

2023-01-31 05:58

关注

quartz 官方例子3


所需jar:

quartz-2.2.0.jar

quartz-jobs-2.2.0.jar

slf4j-api-1.7.5.jar

slf4j-log4j12-1.7.5.jar

log4j-1.2.16.jar




quartz.properties 配置:

org.quartz.scheduler.instanceName: DefaultQuartzScheduler

org.quartz.scheduler.rmi.export: false

org.quartz.scheduler.rmi.proxy: false

org.quartz.scheduler.wrapJobExecutionInUserTransaction: false


org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool

org.quartz.threadPool.threadCount: 10

org.quartz.threadPool.threadPriority: 5

org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread: true


org.quartz.jobStore.misfireThreshold: 60000


org.quartz.jobStore.class: org.quartz.simpl.RAMJobStore

#使用RAMJobStore模式把所有数据保存在内存里,执行速度快;

#使用数据库模式:org.quartz.impl.jdbcjobstore.JobStoreTX将所有调度、作业、触发器信息都保存在数据库中。



官方例子:

CronTriggerExample.java :


package org.quartz.examples.example3;


import java.util.Date;

import org.quartz.CronScheduleBuilder;

import org.quartz.CronTrigger;

import org.quartz.JobBuilder;

import org.quartz.JobDetail;

import org.quartz.Scheduler;

import org.quartz.SchedulerFactory;

import org.quartz.SchedulerMetaData;

import org.quartz.TriggerBuilder;

import org.quartz.impl.StdSchedulerFactory;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;


public class CronTriggerExample

{

 public void run()

   throws Exception

 {

   Logger log = LoggerFactory.getLogger(CronTriggerExample.class);


   log.info("------- Initializing -------------------");


   SchedulerFactory sf = new StdSchedulerFactory();

   Scheduler sched = sf.getScheduler(); // 初始化调度器


   log.info("------- Initialization Complete --------");


   log.info("------- Scheduling Jobs ----------------");


   JobDetail job = JobBuilder.newJob(SimpleJob.class).withIdentity("job1", "group1").build(); // 设置作业,具体操作在SimpleJob类里


   CronTrigger trigger = (CronTrigger)TriggerBuilder.newTrigger().withIdentity("trigger1", "group1").withSchedule(CronScheduleBuilder.cronSchedule("0/20 * * * * ?")).build(); // 设置触发器 每20秒执行一次


   Date ft = sched.scheduleJob(job, trigger); // 设置调度作业

   log.info(job.getKey() + " has been scheduled to run at: " + ft + " and repeat based on expression: " + trigger.getCronExpression());


   job = JobBuilder.newJob(SimpleJob.class).withIdentity("job2", "group1").build();


   trigger = (CronTrigger)TriggerBuilder.newTrigger().withIdentity("trigger2", "group1").withSchedule(CronScheduleBuilder.cronSchedule("15 0/2 * * * ?")).build();


   ft = sched.scheduleJob(job, trigger);

   log.info(job.getKey() + " has been scheduled to run at: " + ft + " and repeat based on expression: " + trigger.getCronExpression());


   job = JobBuilder.newJob(SimpleJob.class).withIdentity("job3", "group1").build();


   trigger = (CronTrigger)TriggerBuilder.newTrigger().withIdentity("trigger3", "group1").withSchedule(CronScheduleBuilder.cronSchedule("0 0/2 8-17 * * ?")).build();


   ft = sched.scheduleJob(job, trigger);

   log.info(job.getKey() + " has been scheduled to run at: " + ft + " and repeat based on expression: " + trigger.getCronExpression());


   job = JobBuilder.newJob(SimpleJob.class).withIdentity("job4", "group1").build();


   trigger = (CronTrigger)TriggerBuilder.newTrigger().withIdentity("trigger4", "group1").withSchedule(CronScheduleBuilder.cronSchedule("0 0/3 17-23 * * ?")).build();


   ft = sched.scheduleJob(job, trigger);

   log.info(job.getKey() + " has been scheduled to run at: " + ft + " and repeat based on expression: " + trigger.getCronExpression());


   job = JobBuilder.newJob(SimpleJob.class).withIdentity("job5", "group1").build();


   trigger = (CronTrigger)TriggerBuilder.newTrigger().withIdentity("trigger5", "group1").withSchedule(CronScheduleBuilder.cronSchedule("0 0 10am 1,15 * ?")).build();


   ft = sched.scheduleJob(job, trigger);

   log.info(job.getKey() + " has been scheduled to run at: " + ft + " and repeat based on expression: " + trigger.getCronExpression());


   job = JobBuilder.newJob(SimpleJob.class).withIdentity("job6", "group1").build();


   trigger = (CronTrigger)TriggerBuilder.newTrigger().withIdentity("trigger6", "group1").withSchedule(CronScheduleBuilder.cronSchedule("0,30 * * ? * MON-FRI")).build();


   ft = sched.scheduleJob(job, trigger);

   log.info(job.getKey() + " has been scheduled to run at: " + ft + " and repeat based on expression: " + trigger.getCronExpression());


   job = JobBuilder.newJob(SimpleJob.class).withIdentity("job7", "group1").build();


   trigger = (CronTrigger)TriggerBuilder.newTrigger().withIdentity("trigger7", "group1").withSchedule(CronScheduleBuilder.cronSchedule("0,30 * * ? * SAT,SUN")).build();


   ft = sched.scheduleJob(job, trigger);

   log.info(job.getKey() + " has been scheduled to run at: " + ft + " and repeat based on expression: " + trigger.getCronExpression());


   log.info("------- Starting Scheduler ----------------");


   sched.start(); // 开启调度任务,执行作业


   log.info("------- Started Scheduler -----------------");


   log.info("------- Waiting five minutes... ------------");

   try

   {

     Thread.sleep(300000L); // 线程休眠5分钟,任务还继续执行

   }

   catch (Exception e)

   {

   }


   log.info("------- Shutting Down ---------------------");


   sched.shutdown(true); // 终止任务


   log.info("------- Shutdown Complete -----------------");


   SchedulerMetaData metaData = sched.getMetaData();

   log.info("Executed " + metaData.getNumberOfJobsExecuted() + " jobs.");

 }


 public static void main(String[] args)

   throws Exception

 {

   CronTriggerExample example = new CronTriggerExample();

   example.run();

 }

}


SimpleJob.java :


package org.quartz.examples.example3;


import java.util.Date;

import org.quartz.Job;

import org.quartz.JobDetail;

import org.quartz.JobExecutionContext;

import org.quartz.JobExecutionException;

import org.quartz.JobKey;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;


public class SimpleJob

 implements Job

{

 private static Logger _log = LoggerFactory.getLogger(SimpleJob.class);


 public void execute(JobExecutionContext context)

   throws JobExecutionException // 具体的作业

 {

   JobKey jobKey = context.getJobDetail().getKey();

   _log.info("SimpleJob says: " + jobKey + " executing at " + new Date());

   System.out.println("执行任务");

 }

}


执行结果:

2013-08-14 14:13:10,635  com.test.quartz.CronTriggerExample  70 - ------- Initializing -------------------

2013-08-14 14:13:10,679  org.quartz.impl.StdSchedulerFactory  1184 - Using default implementation for ThreadExecutor

2013-08-14 14:13:10,683  org.quartz.simpl.SimpleThreadPool  268 - Job execution threads will use class loader of thread: main

2013-08-14 14:13:10,709  org.quartz.core.SchedulerSignalerImpl  61 - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl

2013-08-14 14:13:10,717  org.quartz.core.QuartzScheduler  249 - Quartz Scheduler v.2.2.0 created.

2013-08-14 14:13:10,718  org.quartz.simpl.RAMJobStore  155 - RAMJobStore initialized.

2013-08-14 14:13:10,720  org.quartz.core.QuartzScheduler  318 - Scheduler meta-data: Quartz Scheduler (v2.2.0) 'DefaultQuartzScheduler' with instanceId 'NON_CLUSTERED'

 Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.

 NOT STARTED.

 Currently in standby mode.

 Number of jobs executed: 0

 Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads.

 Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered.


2013-08-14 14:13:10,721  org.quartz.impl.StdSchedulerFactory  1339 - Quartz scheduler 'DefaultQuartzScheduler' initialized from default resource file in Quartz package: 'quartz.properties'

2013-08-14 14:13:10,722  org.quartz.impl.StdSchedulerFactory  1343 - Quartz scheduler version: 2.2.0

2013-08-14 14:13:10,722  com.test.quartz.CronTriggerExample  75 - ------- Initialization Complete --------

2013-08-14 14:13:10,722  com.test.quartz.CronTriggerExample  77 - ------- Scheduling Jobs ----------------

2013-08-14 14:13:10,742  com.test.quartz.CronTriggerExample  89 - group1.job1 has been scheduled to run at: Wed Aug 14 14:13:10 CST 2013 and repeat based on expression: 0/5 * * * * ?

2013-08-14 14:13:10,742  com.test.quartz.CronTriggerExample  174 - ------- Starting Scheduler ----------------

2013-08-14 14:13:10,743  org.quartz.core.QuartzScheduler  588 - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED started.

2013-08-14 14:13:10,743  com.test.quartz.CronTriggerExample  178 - ------- Started Scheduler -----------------

2013-08-14 14:13:10,743  com.test.quartz.CronTriggerExample  180 - ------- Waiting one minute... ------------

2013-08-14 14:13:10,745  org.quartz.core.QuartzSchedulerThread  268 - batch acquisition of 1 triggers

2013-08-14 14:13:10,751  org.quartz.simpl.PropertySettingJobFactory  51 - Producing instance of Job 'group1.job1', class=com.test.quartz.SimpleJob

2013-08-14 14:13:10,754  org.quartz.core.QuartzSchedulerThread  268 - batch acquisition of 1 triggers

2013-08-14 14:13:10,754  org.quartz.core.JobRunShell  206 - Calling execute on job group1.job1

2013-08-14 14:13:10,754  com.test.quartz.SimpleJob  18 - SimpleJob says: group1.job1 executing at Wed Aug 14 14:13:10 CST 2013

执行任务

2013-08-14 14:13:11,717  org.quartz.utils.UpdateChecker  54 - Checking for available updated version of Quartz...

2013-08-14 14:13:11,775  org.quartz.utils.UpdateChecker  49 - Quartz version update check failed: www.terracotta.org

2013-08-14 14:13:15,000  org.quartz.simpl.PropertySettingJobFactory  51 - Producing instance of Job 'group1.job1', class=com.test.quartz.SimpleJob

2013-08-14 14:13:15,001  org.quartz.core.JobRunShell  206 - Calling execute on job group1.job1

2013-08-14 14:13:15,001  com.test.quartz.SimpleJob  18 - SimpleJob says: group1.job1 executing at Wed Aug 14 14:13:15 CST 2013

执行任务

2013-08-14 14:13:15,002  org.quartz.core.QuartzSchedulerThread  268 - batch acquisition of 1 triggers

2013-08-14 14:13:20,001  org.quartz.simpl.PropertySettingJobFactory  51 - Producing instance of Job 'group1.job1', class=com.test.quartz.SimpleJob

2013-08-14 14:13:20,008  org.quartz.core.QuartzSchedulerThread  268 - batch acquisition of 1 triggers

2013-08-14 14:13:20,009  org.quartz.core.JobRunShell  206 - Calling execute on job group1.job1

2013-08-14 14:13:20,009  com.test.quartz.SimpleJob  18 - SimpleJob says: group1.job1 executing at Wed Aug 14 14:13:20 CST 2013

执行任务

2013-08-14 14:13:25,000  org.quartz.simpl.PropertySettingJobFactory  51 - Producing instance of Job 'group1.job1', class=com.test.quartz.SimpleJob

2013-08-14 14:13:25,001  org.quartz.core.QuartzSchedulerThread  268 - batch acquisition of 1 triggers

2013-08-14 14:13:25,001  org.quartz.core.JobRunShell  206 - Calling execute on job group1.job1

2013-08-14 14:13:25,001  com.test.quartz.SimpleJob  18 - SimpleJob says: group1.job1 executing at Wed Aug 14 14:13:25 CST 2013

执行任务


阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯