In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces what Quartz is, the content is very detailed, interested friends can use it for reference, I hope it can be helpful to you.
Quartz is an open source job scheduling framework written entirely by Java, which provides a simple but powerful mechanism for job scheduling in Java applications.
Core concepts of Quartz
Job: indicates the specific content of a work to be performed. There is only one method in this interface: void execute (JobExecutionContext context).
JobDetail: represents a specific executable scheduler, Job is the content that this executable needs to execute, in addition, JobDetail also contains the task scheduling scheme and strategy.
Trigger: indicates the configuration of a scheduling parameter and when to adjust it.
Scheduler: represents a scheduling container in which multiple JobDetail and Trigger can be registered.
Quartz operating environment
Quartz can run embedded in another standalone application.
Quartz can be instantiated within the application server (or Servlet container) and participate in XA transactions.
Quartz can be run as a stand-alone program (within its own JVM) and can be used through RMI.
Quartz can be instantiated as a separate project cluster (load balancing and failover capabilities) for job execution.
Public class Quickstart {public static void main (String [] args) {try {Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler (); scheduler.start (); scheduler.shutdown ();} catch (SchedulerException e) {e.printStackTrace ();}
The running log is as follows:
14 main 53 main 00.764 [main] INFO org.quartz.impl.StdSchedulerFactory-Using default implementation for ThreadExecutor14:53:00.790 [main] INFO org.quartz.simpl.SimpleThreadPool-Job execution threads will use class loader of thread: main14:53:00.856 [main] INFO org.quartz.core.SchedulerSignalerImpl-Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl14:53:00.856 [main] INFO org.quartz.core.QuartzScheduler-QuartzScheduler v.2.3.0 created.14:53:00. [main] INFO org.quartz.simpl.RAMJobStore-RAMJobStore initialized.14:53:00.858 [main] INFO org.quartz.core.QuartzScheduler-Scheduler meta-data: QuartzScheduler (v2.3.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.14:53:00.858 [main] INFO org.quartz.impl.StdSchedulerFactory-Quartz scheduler 'DefaultQuartzScheduler' initialized from default resource file in Quartz package:' quartz.properties'14:53:00.858 [main] INFO org.quartz.impl.StdSchedulerFactory-Quartz scheduler version: 2.3.014 DefaultQuartzScheduler' initialized from default resource file in Quartz package 53 Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED started.14:53:00.859 00.859 [main] INFO org.quartz.core.QuartzScheduler-Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED started.14:53:00.859 [main] INFO org.quartz.core.QuartzScheduler-Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED shutting down.14:53:00.859 [main] INFO org.quartz.core.QuartzScheduler-Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED paused.14:53:00.859 [main] DEBUG org.quartz.simpl.SimpleThreadPool-Shutting down threadpool...14:53:00.859 [main] DEBUG org.quartz.simpl.SimpleThreadPool-Shutdown of threadpool complete.14:53:00.859 [main] INFO org.quartz.core.QuartzScheduler-Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED shutdown complete.14: 53 WorkerThread is shut down.14:53:01.303 01.303 [DefaultQuartzScheduler_Worker-1] DEBUG org.quartz.simpl.SimpleThreadPool-WorkerThread is shut down.14:53:01.303 [DefaultQuartzScheduler_Worker-2] DEBUG org.quartz.simpl.SimpleThreadPool-WorkerThread is shut down.14:53:01.304 [DefaultQuartzScheduler_Worker-6] DEBUG org.quartz.simpl.SimpleThreadPool-WorkerThread is shut down.14:53:01.304 [DefaultQuartzScheduler_Worker-5] DEBUG org.quartz.simpl.SimpleThreadPool-WorkerThread is shut down.14:53:01.305 [DefaultQuartzScheduler_ Worker-9] DEBUG org.quartz.simpl.SimpleThreadPool-WorkerThread is shut down.14:53:01.323 [DefaultQuartzScheduler_Worker-3] DEBUG org.quartz.simpl.SimpleThreadPool-WorkerThread is shut down.14:53:01.338 [DefaultQuartzScheduler_Worker-7] DEBUG org.quartz.simpl.SimpleThreadPool-WorkerThread is shut down.14:53:01.338 [DefaultQuartzScheduler_Worker-8] DEBUG org.quartz.simpl.SimpleThreadPool-WorkerThread is shut down.14:53:01.338 [DefaultQuartzScheduler_Worker-4] DEBUG org.quartz SimpleThreadPool-WorkerThread is shut down.14:53:01.338 [DefaultQuartzScheduler_Worker-10] DEBUG org.quartz.simpl.SimpleThreadPool-WorkerThread is shut down.
Life cycle of Scheduler
It starts when SchedulerFactory creates it and ends when Scheduler calls the shutdown () method; after Scheduler is created, you can add, delete, and enumerate Job and Trigger, and perform its scheduling-related operations, but it is only after Scheduler calls the start () method that Trigger is actually triggered (that is, job is executed).
The connection between Job and Trigger
A Job is a class that implements the Job interface. When a Trigger of Job is triggered, the execute () method is called by a worker thread of the calling program, and the JobExecutionContext object passed to the execute () method provides a "runtime" environment to the job instance. After the Trigger of a Job is triggered, the execute () method is called by a worker thread of Scheduler, and some information about the Job runtime is stored in the JobExecutionContext object passed to the execute () method.
Trigger is used to trigger Job execution. When scheduling a Job, create an Trigger instance and set scheduling-related properties. Quartz comes with various types of Trigger, the most commonly used of which are SimpleTrigger and CronTrigger.
Key of Job and Trigger
When registering Job and Trigger with Scheduler, you can set key for them and configure their identity attributes, and the Key of Job and Trigger can be used to put Job and Trigger into different groups and then operate on a packet-based basis. Note: the names of Job and Trigger under the same group must be unique, that is, the Key of a Job or Trigger consists of a name (name) and a group (group).
Question: why is there both Job and Trigger?
Many task schedulers do not distinguish between Job and Trigger, and some schedulers simply define a Job; by an execution time and some Job identifiers. Others combine Quartz's Job and Trigger objects into one. The developers of Quartz believe that it is reasonable to separate the scheduling from the tasks to be scheduled. For example, after Job is created, it can be saved in Scheduler, independent of Trigger, and multiple Trigger,Job and Trigger in the same Job can be loosely coupled. When the Trigger associated with the Job in Scheduler expires, you can configure Job to be rescheduled later instead of redefining Job, and Trigger can be modified or replaced. Without redefining the Job associated with it.
The connection between Job and JobDetail
JobDetail instances are created through JobBuilder classes. In Quartz's description language, JobDetail is called "Job definition" or "JobDetail instance", and an executing Job is called "Job instance", so when we use "Job", we generally refer to Job definition, or JobDetail; when we refer to a class that implements the Job interface, we usually use "Job class".
Job status and concurrency
There are some things to note about the state data and concurrency of Job. You can add some annotations to the Job class to avoid problems:
DisallowConcurrentExecution: add this annotation to the Job class and tell Quartz not to execute the same Job definition concurrently. Note that although the annotation is added to the Job class, it actually acts on JobDetail.
PersistJobDataAfterExecution: add this comment to the Job class, telling Quartz to update the data of the JobDataMap in JobDetail after the successful execution of the execute method of the job class (without any exception), so that the Job (that is, JobDetail) in the next execution will be the updated data instead of the old data before the update. Like the @ DisallowConcurrentExecution annotation, this annotation is added to the Job class, but it actually works on JobDetail.
If you use @ PersistJobDataAfterExecution annotations, we strongly recommend that you use @ DisallowConcurrentExecution annotations at the same time, because when two instances of the same Job (JobDetail) are executed concurrently, the data stored in JobDataMap is likely to be uncertain due to competition.
JobDataMap
JobDataMap is an implementation of the Java Map interface that can contain an unlimited number of (serialized) data objects that can be used when the Job instance executes.
If you use a persistent storage mechanism, you need to be careful when deciding what data to store in JobDataMap, because all objects stored in JobDataMap will be serialized, which may lead to inconsistent versions of the class.
JobExecutionException
Only one type of exception is allowed in the execute method, and in practice, everything in the execute method should be placed in a "try-catch" block so that you know how to handle the exception that occurs.
A complete example of a quick start
Public class Quickstart {public static void main (String [] args) {try {Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler (); scheduler.start (); JobDetail job = newJob (HelloJob.class) .withidentity ("job1", "group1") .usingJobData ("name", "quartz"). Build () Trigger trigger = newTrigger (). WithIdentity ("trigger1", "group1"). StartNow (). Withschedule (simpleSchedule (). WithIntervalInSeconds (30). RepeatForever ()). Build (); scheduler.scheduleJob (job, trigger); try {Thread.sleep (1000L * 60L * 10L);} catch (InterruptedException e) {e.printStackTrace () } scheduler.shutdown ();} catch (SchedulerException e) {e.printStackTrace ();} public class HelloJob implements Job {@ Override public void execute (JobExecutionContext context) throws JobExecutionException {try {JobDetail job = context.getJobDetail (); String name = job.getJobDataMap () .getString ("name") System.out.println ("hello" + name + "at" + new Date ());} catch (Exception e) {e.printStackTrace ();} about what Quartz is, I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.