首先从Quartz官网上获得相应的jar包,Quartz package包含了许多jar文件,quartz-all-xxx.jar是Quartz的主要的library。获得jar包后引用到自己的工程里(我是把jar下载到我的本地Maven库中)
引完jar包,开始我们第一个Quartz的小Demo:首先我们需要定义我们自己的Job
然后定义scheduler 和trigger 来完成我们Quartz的小Demo
处理结果:
哈哈 成功了...
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>${quartz.version}</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-oracle</artifactId>
<version>${quartz.version}</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-weblogic</artifactId>
<version>${quartz.version}</version>
</dependency>
引完jar包,开始我们第一个Quartz的小Demo:首先我们需要定义我们自己的Job
public class MyJob implements Job {
private Logger logger = LoggerFactory.getLogger(MyJob.class);
public MyJob(){
}
@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
logger.info("my job Start ... ..."+new Date());
}
}
然后定义scheduler 和trigger 来完成我们Quartz的小Demo
try {
logger.info("------------------initializing-----------------");
SchedulerFactory factory = new StdSchedulerFactory();
Scheduler scheduler = factory.getScheduler();
logger.info("-------------------initialize complete----------");
//computer a time that is on the next round time
Date runTime = DateBuilder.evenMinuteDate(new Date());
logger.info("-------------------scheduling job---------------");
//define a Job and tie it to our MyJob class
JobDetail job = JobBuilder.newJob(MyJob.class)
.withIdentity("job1", "group1")
.build();
//Trigger the job to run on the next round time
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity("trigger1", "group1")
.startAt(runTime)
.build();
//Tell quartz to schedule the job using our trigger
scheduler.scheduleJob(job, trigger);
logger.info(job.getKey()+"run at :"+runTime);
//Start up the scheduler (nothing can actually run until the scheduler has bean started)
scheduler.start();
logger.info("-------------------started Scheduler-------------");
//wait the scheduler so long enough so that the scheduler as an opportunity to run the job
logger.info("------------------waiting 65 seconds...----------");
try{
//wait the 65 seconds to show job
Thread.sleep(65L*1000L);
//execute ...
}catch (Exception e) {
}
//shutdown the scheduler
logger.info("----------------shutting down ----------------");
scheduler.shutdown(true);
logger.info("----------------shutdown completed------------");
} catch (SchedulerException e) {
e.printStackTrace();
}
处理结果:
INFO - ------------------initializing-----------------
INFO - Using default implementation for ThreadExecutor
INFO - Job execution threads will use class loader of thread: main
INFO - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
INFO - Quartz Scheduler v.2.1.6 created.
INFO - RAMJobStore initialized.
INFO - Scheduler meta-data: Quartz Scheduler (v2.1.6) '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.
INFO - Quartz scheduler 'DefaultQuartzScheduler' initialized from default resource file in Quartz package: 'quartz.properties'
INFO - Quartz scheduler version: 2.1.6
INFO - -------------------initialize complete----------
INFO - -------------------scheduling job---------------
INFO - group1.job1run at :Tue Nov 20 13:55:00 CST 2012
INFO - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED started.
INFO - -------------------started Scheduler-------------
INFO - ------------------waiting 65 seconds...----------
DEBUG - batch acquisition of 0 triggers
DEBUG - Checking for available updated version of Quartz...
DEBUG - batch acquisition of 1 triggers
DEBUG - Producing instance of Job 'group1.job1', class=com.sdw.quartz1.MyJob
DEBUG - batch acquisition of 0 triggers
DEBUG - Calling execute on job group1.job1
INFO - my job Start ... ...Tue Nov 20 13:55:00 CST 2012
INFO - ----------------shutting down ----------------
INFO - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED shutting down.
INFO - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED paused.
DEBUG - Shutting down threadpool...
DEBUG - WorkerThread is shut down.
DEBUG - WorkerThread is shut down.
DEBUG - WorkerThread is shut down.
DEBUG - WorkerThread is shut down.
DEBUG - WorkerThread is shut down.
DEBUG - WorkerThread is shut down.
DEBUG - WorkerThread is shut down.
DEBUG - WorkerThread is shut down.
DEBUG - WorkerThread is shut down.
DEBUG - WorkerThread is shut down.
DEBUG - No executing jobs remaining, all threads stopped.
DEBUG - Shutdown of threadpool complete.
INFO - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED shutdown complete.
INFO - ----------------shutdown completed------------
哈哈 成功了...