Get the App
SLTechnology News&Howtos  ›  Development  › 

Case Analysis of springboot Integration quartz

Shulou Source: shulou.com Published: 2022-06-01 17:34:47 12月04日 Update

This article mainly explains the "springboot integration quartz case analysis", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "springboot integration quartz case analysis" bar!

A brief introduction to quartz

1.Quartz is an open source task scheduling framework. It is its core function to perform tasks based on a regular, regular policy, such as every Friday from 8: 00 a.m. to 9: 00 a.m., every 10 minutes.

2.Quartz has three core elements: Scheduler, Job, and Trigger.

2.1.Job (tasks): is an interface with a method void execute () that you can implement to define the tasks that need to be performed (specific logic code).

Each time 2.2.JobDetail:Quartz executes job, it creates a new Job instance and receives a Job implementation class so that the runtime uses the reflection call mechanism of newInstance () to instantiate Job.JobDetail to describe the Job implementation class and related static information, such as the group name of the task in scheduler.

2.3.Trigger (trigger): describe the time-triggered rule implementation classes SimpleTrigger and CronTrigger that trigger Job execution. Various complex scheduling schemes can be defined through crom expressions.

2.4.Calendar: a collection of calendar-specific times. A Trigger can be associated with multiple calendar. For example, if a task is performed at 10:00 every Monday morning and is not performed on a statutory holiday, it can be excluded at a fixed point through calendar.

2.5.Scheduler (Scheduler): a stand-alone running container representing a Quartz. Trigger and JobDetail can be registered with Scheduler. Scheduler can bind a Trigger to a JobDetail so that when the Trigger is triggered, the corresponding Job is executed. A Job can correspond to multiple Trigger, but a Trigger can only correspond to one Job.

II. Demo structure

Third, code detailed explanation

Pom.xml

Org.springframework.boot spring-boot-starter-quartz

DateTimeJob.java (the specific business logic code is written here)

Executing logic code in job task to realize the timing use of task

Public class DateTimeJob extends QuartzJobBean {@ Override protected void executeInternal (JobExecutionContext jobExecutionContext) throws JobExecutionException {/ / get the associated data in JobDetail String msg = (String) jobExecutionContext.getJobDetail (). GetJobDataMap () .get ("msg"); System.out.println ("current time:" + new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss") .format (new Date ()) + "- -" + msg);}}

QuartzConfig.java (configuration class)

The job execution cycle and detailed configuration are defined in the

@ Configurationpublic class QuartzConfig {@ Bean public JobDetail printTimeJobDetail () {return JobBuilder.newJob (DateTimeJob.class) / / PrintTimeJob our business class .withIdentity ("DateTimeJob") / can give the JobDetail an id / / each JobDetail has a Map containing the data associated with this Job In the Job class, you can get .usingJobData ("msg", "Hello Quartz") / / associated key pair .storeDuried () / / even if there is no Trigger association, you do not need to delete the JobDetail .build (). } @ Bean public Trigger printTimeJobTrigger () {CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule ("0 1 *?"); return TriggerBuilder.newTrigger () .forJob (printTimeJobDetail ()) / / associate the above JobDetail .withIdentity ("quartzTaskService") / / give Trigger a name .withschedule (cronScheduleBuilder) .build () Fourth, code effect

Thank you for your reading, the above is the content of "springboot Integration quartz case Analysis". After the study of this article, I believe you have a deeper understanding of the problem of springboot Integration quartz case Analysis, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Tasks examples code associations instance analysis analysis integration can pass scheduling logic learning business information content multiple interfaces data time core triggers Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS OPPO Reno vpn MySQL Docker