Python 官方文档:入门教程 => 点击学习
quartz 官方例子3所需jar:quartz-2.2.0.jarquartz-jobs-2.2.0.jarslf4j-api-1.7.5.jarslf4j-log4j12-1.7.5.jarlog4j-1.2.16.jarquartz.
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
执行任务
--结束END--
本文标题: quartz2.2 官方例子3
本文链接: https://lsjlt.com/news/189209.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0