Timed Task Framework Quartz Learning Notes

Posted by lbaxterl on Mon, 24 Feb 2020 18:06:12 +0100

In this post:

This article only does the introduction and the effect preview project construction, does not do the code analysis.

Introduction to Quartz

Quartz is a rich and open source job scheduling library that can be integrated into almost any Java application, from the smallest stand-alone application to the largest e-commerce system. Quartz can create either simple jobs or tens of thousands of complex jobs with job definitions that conform to the standard Java component specifications and can execute almost everything you write.In addition, it includes many enterprise-level capabilities, such as support for JTA transactions and clusters.

Job scheduling sounds like a big term, and it's a common understanding that there's something the program needs to do at a fixed time. Quartz is a framework that helps us do this, and it's easy to integrate with our applications.

Quartz Quick Start Guide

1. Download and Install

  • Official download address: http://www.quartz-scheduler.org/downloads , select Stable Releases to download
  • install
    • Unzip the downloaded stable version (for example, quartz-2.3.0-distribution.tar.gz).Get the jar package under the unzipped folder lib.
    • Create a new Java project, create a new lib folder in the project root directory, and copy all the jar packages Quartz needs.
    • The jar required to configure Quartz is a dependency (different IDEs operate differently, for example, IDEA)
    • Create new HelloJob.java, QuartzTest.java, quartz.properties, log4j.properties.

HelloJob.java

package org.demo.quickstart;  

import org.quartz.Job;  
import org.quartz.JobExecutionContext;  
import org.quartz.JobExecutionException;  
import org.slf4j.Logger;  
import org.slf4j.LoggerFactory;  
  
import java.util.Date;  
  
public class HelloJob implements Job {  
  private static Logger _log = LoggerFactory.getLogger(HelloJob.class);  
  
 public HelloJob() {  
 }  
  @Override  
  public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {  
 _log.info("Hello World! - " \+ new Date());  
  }  
}`

QuartzTest.java

package org.demo.quickstart;  
  
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;  
  
public class QuartzTest {  
  
  public static void main(String[] args) {  
  
  try {  
  // Grab the Scheduler instance from the Factory  
  Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();  
  
  // and start it off  
  scheduler.start();  
  
  JobDetail job = JobBuilder.newJob(HelloJob.class)  
  .withIdentity("job1", "group1")  
  .build();  
  
  // Trigger the job to run now, and then repeat every 3 seconds  
  Trigger trigger = TriggerBuilder.newTrigger()  
  .withIdentity("trigger1", "group1")  
  .startNow()  
  .withSchedule(SimpleScheduleBuilder.simpleSchedule()  
  .withIntervalInSeconds(3)  
  .repeatForever())  
  .build();  
  
  // Tell quartz to schedule the job using our trigger  
  scheduler.scheduleJob(job, trigger);  
  
  Thread.sleep(12000);  
  scheduler.shutdown();  
  
  } catch (SchedulerException | InterruptedException se) {  
  se.printStackTrace();  
  }  
 }
 }

log4j.properties

log4j.rootLogger=debug,console
# Console  
log4j.appender.console=org.apache.log4j.ConsoleAppender  
log4j.appender.console.Threshold=debug  
log4j.appender.console.ImmediateFlush=true  
log4j.appender.console.Target=System.out  
log4j.appender.console.layout=org.apache.log4j.PatternLayout  
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} \[%p\] \[%l\]: %m%n

quartz.properties

org.quartz.scheduler.instanceName = MyScheduler  
org.quartz.threadPool.threadCount = 3  
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore

2. Startup and Effect Preview

  • Start: Run the main method in QuartzTest.java.
  • Preview (run log)
"C:\Program Files\Java\jdk1.8.0_202\bin\java.exe" "-javaagent:D:\developer\IntelliJ IDEA 2019.3.1\lib\idea_rt.jar=50796:D:\developer\IntelliJ IDEA 2019.3.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_202\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\rt.jar;D:\developerData\ideaProjects\framework\quartz\out\production\quartz;D:\developerData\ideaProjects\framework\quartz\lib\c3p0-0.9.5.2.jar;D:\developerData\ideaProjects\framework\quartz\lib\log4j-1.2.16.jar;D:\developerData\ideaProjects\framework\quartz\lib\slf4j-api-1.7.7.jar;D:\developerData\ideaProjects\framework\quartz\lib\slf4j-log4j12-1.7.7.jar;D:\developerData\ideaProjects\framework\quartz\lib\quartz-2.3.0-SNAPSHOT.jar;D:\developerData\ideaProjects\framework\quartz\lib\quartz-jobs-2.3.0-SNAPSHOT.jar;D:\developerData\ideaProjects\framework\quartz\example\gf.javax.transaction-1.1.jar;D:\developerData\ideaProjects\framework\quartz\example\quartz-examples-2.3.0-SNAPSHOT.jar" org.demo.quickstart.QuartzTest
[INFO] 24 February 11:31:22.308 Afternoon main [org.quartz.impl.StdSchedulerFactory]
Using default implementation for ThreadExecutor

[INFO] 24 February 11:31:22.321 Afternoon main [org.quartz.core.SchedulerSignalerImpl]
Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl

[INFO] 24 February 11:31:22.322 Afternoon main [org.quartz.core.QuartzScheduler]
Quartz Scheduler v.2.3.0-SNAPSHOT created.

[INFO] 24 February 11:31:22.322 Afternoon main [org.quartz.simpl.RAMJobStore]
RAMJobStore initialized.

[INFO] 24 February 11:31:22.323 Afternoon main [org.quartz.core.QuartzScheduler]
Scheduler meta-data: Quartz Scheduler (v2.3.0-SNAPSHOT) 'MyScheduler' 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 3 threads.
  Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered.


[INFO] 24 February 11:31:22.323 Afternoon main [org.quartz.impl.StdSchedulerFactory]
Quartz scheduler 'MyScheduler' initialized from default resource file in Quartz package: 'quartz.properties'

[INFO] 24 February 11:31:22.323 Afternoon main [org.quartz.impl.StdSchedulerFactory]
Quartz scheduler version: 2.3.0-SNAPSHOT

[INFO] 24 February 11:31:22.323 Afternoon main [org.quartz.core.QuartzScheduler]
Scheduler MyScheduler_$_NON_CLUSTERED started.

[INFO] 24 February 11:31:22.342 Afternoon MyScheduler_Worker-1 [org.demo.quickstart.HelloJob]
Hello World! - Mon Feb 24 23:31:22 GMT+08:00 2020

[INFO] 24 February 11:31:25.327 Afternoon MyScheduler_Worker-2 [org.demo.quickstart.HelloJob]
Hello World! - Mon Feb 24 23:31:25 GMT+08:00 2020

[INFO] 24 February 11:31:28.326 Afternoon MyScheduler_Worker-3 [org.demo.quickstart.HelloJob]
Hello World! - Mon Feb 24 23:31:28 GMT+08:00 2020

[INFO] 24 February 11:31:31.326 Afternoon MyScheduler_Worker-1 [org.demo.quickstart.HelloJob]
Hello World! - Mon Feb 24 23:31:31 GMT+08:00 2020

[INFO] 24 February 11:31:34.326 Afternoon MyScheduler_Worker-2 [org.demo.quickstart.HelloJob]
Hello World! - Mon Feb 24 23:31:34 GMT+08:00 2020

[INFO] 24 February 11:31:34.329 Afternoon main [org.quartz.core.QuartzScheduler]
Scheduler MyScheduler_$_NON_CLUSTERED shutting down.

[INFO] 24 February 11:31:34.329 Afternoon main [org.quartz.core.QuartzScheduler]
Scheduler MyScheduler_$_NON_CLUSTERED paused.

[INFO] 24 February 11:31:34.330 Afternoon main [org.quartz.core.QuartzScheduler]
Scheduler MyScheduler_$_NON_CLUSTERED shutdown complete.


Process finished with exit code 0

As you can see, Hello World! Prints every 3s, 12s (4 times), then scheduler.shutdown(); program ends after method.

END

Topics: Java log4j snapshot Apache