引用地址: https://www.callicoder.com/spring-boot-quartz-scheduler-email-scheduling-example/
代码地址: https://github.com/callicoder/spring-boot-quartz-scheduler-email-scheduling
Quartz is an open source Java library for scheduling Jobs. It has a very rich set of features including but not limited to persistent Jobs, transactions, and clustering.
You can schedule Jobs to be executed at a certain time of day, or periodically at a certain interval, and much more. Quartz provides a fluent API for creating jobs and scheduling them.
Quartz Jobs can be persisted into a database, or a cache, or in-memory. This is in contrast to Spring’s built-in task scheduling API that doesn’t support persistent jobs.
In this article, you’ll learn how to schedule Jobs in spring boot using Quartz Scheduler by building a simple Email Scheduling application. The application will have a Rest API that allows clients to schedule Emails at a later time.
We’ll use MySQL to persist all the jobs and other job-related data.
Sounds interesting? Let’s start…
Creating the Application
Let’s bootstrap the application using Spring Boot CLI. Open your terminal and type the following command -
spring init -d=web,jpa,mysql,quartz,mail -n=quartz-demo quartz-demo
The above command will generate the project with all the specified dependencies in a folder named quartz-demo
.
Note that, you can also use Spring Initializr web tool to bootstrap the project by following the instructions below -
- Open http://start.spring.io
- Enter
quartz-demo
in the Artifact field. - Add
Web
,JPA
,MySQL
,Quartz
, andMail
in the dependencies section. - Click Generate Project to generate and download the project.
That’s it! You may now import the project into your favorite IDE and start working.
Directory Structure
Following is the directory structure of the complete application for your reference. We’ll create all the required folders and classes one-by-one in this article -
<source media="(max-width: 520px)" srcset="https://www.callicoder.com/assets/images/post/medium/spring-boot-quartz-scheduler-email-scheduling-project-example.jpg" style="box-sizing: border-box;"></source>
Configuring MySQL database, Quartz Scheduler, and Mail Sender
Let’s configure Quartz Scheduler, MySQL database, and Spring Mail. MySQL database will be used for storing Quartz Jobs, and Spring Mail will be used to send emails.
Open src/main/resources/application.properties
file and add the following properties -
## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.url = jdbc:mysql://localhost:3306/quartz_demo?useSSL=false
spring.datasource.username = root
spring.datasource.password = callicoder
## QuartzProperties
spring.quartz.job-store-type = jdbc
spring.quartz.properties.org.quartz.threadPool.threadCount = 5
## MailProperties
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=rajeevc217@gmail.com
spring.mail.password=
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
You’ll need to create a MySQL database named quartz_demo
. Also, don’t forget to change the spring.datasource.username
and spring.datasource.password
properties as per your MySQL installation.
We’ll be using Gmail’s SMTP server for sending emails. Please add your password in the spring.mail.password
property. You may also pass this property at runtime as command line argument or set it in the environment variable.
Note that, Gmail’s SMTP access is disabled by default. To allow this app to send emails using your Gmail account -
- Go to https://myaccount.google.com/security?pli=1#connectedapps
- Set ‘Allow less secure apps’ to YES
Creating Quartz Tables
Since we have configured Quartz to store Jobs in the database, we’ll need to create the tables that Quartz uses to store Jobs and other job-related meta-data.
Please download the following SQL script and run it in your MySQL database to create all the Quartz specific tables.
After downloading the above SQL script, login to MySQL and run the script like this -
mysql> source <PATH_TO_QUARTZ_TABLES.sql>
Overview of Quartz Scheduler’s APIs and Terminologies
1. Scheduler
The Primary API for scheduling, unscheduling, adding, and removing Jobs.
2. Job
The interface to be implemented by classes that represent a ‘job’ in Quartz. It has a single method called execute()
where you write the work that needs to be performed by the Job.
3. JobDetail
A JobDetail represents an instance of a Job. It also contains additional data in the form of a JobDataMap that is passed to the Job when it is executed.
Every JobDetail is identified by a JobKey that consists of a name and a group. The name must be unique within a group.
4. Trigger
A Trigger, as the name suggests, defines the schedule at which a given Job will be executed. A Job can have many Triggers, but a Trigger can only be associated with one Job.
Every Trigger is identified by a TriggerKey that comprises of a name and a group. The name must be unique within a group.
Just like JobDetails, Triggers can also send parameters/data to the Job.
5. JobBuilder
JobBuilder is a fluent builder-style API to construct JobDetail instances.
6. TriggerBuilder
TriggerBuilder is used to instantiate Triggers.
Creating a REST API to schedule Email Jobs dynamically in Quartz
All right! Let’s now create a REST API to scheduler email Jobs in Quartz dynamically. All the Jobs will be persisted in the database and executed at the specified schedule.
Before writing the API, Let’s create the DTO classes that will be used as request and response payloads for the scheduleEmail
API -
ScheduleEmailRequest
package com.example.quartzdemo.payload;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.time.ZoneId;
public class ScheduleEmailRequest {
@Email
@NotEmpty
private String email;
@NotEmpty
private String subject;
@NotEmpty
private String body;
@NotNull
private LocalDateTime dateTime;
@NotNull
private ZoneId timeZone;
// Getters and Setters (Omitted for brevity)
}
ScheduleEmailResponse
package com.example.quartzdemo.payload;
import com.fasterxml.jackson.annotation.JsonInclude;
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ScheduleEmailResponse {
private boolean success;
private String jobId;
private String jobGroup;
private String message;
public ScheduleEmailResponse(boolean success, String message) {
this.success = success;
this.message = message;
}
public ScheduleEmailResponse(boolean success, String jobId, String jobGroup, String message) {
this.success = success;
this.jobId = jobId;
this.jobGroup = jobGroup;
this.message = message;
}
// Getters and Setters (Omitted for brevity)
}
ScheduleEmail Rest API
The following controller defines the /scheduleEmail
REST API that schedules email Jobs in Quartz -
package com.example.quartzdemo.controller;
import com.example.quartzdemo.job.EmailJob;
import com.example.quartzdemo.payload.ScheduleEmailRequest;
import com.example.quartzdemo.payload.ScheduleEmailResponse;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.UUID;
@RestController
public class EmailJobSchedulerController {
private static final Logger logger = LoggerFactory.getLogger(EmailJobSchedulerController.class);
@Autowired
private Scheduler scheduler;
@PostMapping("/scheduleEmail")
public ResponseEntity<ScheduleEmailResponse> scheduleEmail(@Valid @RequestBody ScheduleEmailRequest scheduleEmailRequest) {
try {
ZonedDateTime dateTime = ZonedDateTime.of(scheduleEmailRequest.getDateTime(), scheduleEmailRequest.getTimeZone());
if(dateTime.isBefore(ZonedDateTime.now())) {
ScheduleEmailResponse scheduleEmailResponse = new ScheduleEmailResponse(false,
"dateTime must be after current time");
return ResponseEntity.badRequest().body(scheduleEmailResponse);
}
JobDetail jobDetail = buildJobDetail(scheduleEmailRequest);
Trigger trigger = buildJobTrigger(jobDetail, dateTime);
scheduler.scheduleJob(jobDetail, trigger);
ScheduleEmailResponse scheduleEmailResponse = new ScheduleEmailResponse(true,
jobDetail.getKey().getName(), jobDetail.getKey().getGroup(), "Email Scheduled Successfully!");
return ResponseEntity.ok(scheduleEmailResponse);
} catch (SchedulerException ex) {
logger.error("Error scheduling email", ex);
ScheduleEmailResponse scheduleEmailResponse = new ScheduleEmailResponse(false,
"Error scheduling email. Please try later!");
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(scheduleEmailResponse);
}
}
private JobDetail buildJobDetail(ScheduleEmailRequest scheduleEmailRequest) {
JobDataMap jobDataMap = new JobDataMap();
jobDataMap.put("email", scheduleEmailRequest.getEmail());
jobDataMap.put("subject", scheduleEmailRequest.getSubject());
jobDataMap.put("body", scheduleEmailRequest.getBody());
return JobBuilder.newJob(EmailJob.class)
.withIdentity(UUID.randomUUID().toString(), "email-jobs")
.withDescription("Send Email Job")
.usingJobData(jobDataMap)
.storeDurably()
.build();
}
private Trigger buildJobTrigger(JobDetail jobDetail, ZonedDateTime startAt) {
return TriggerBuilder.newTrigger()
.forJob(jobDetail)
.withIdentity(jobDetail.getKey().getName(), "email-triggers")
.withDescription("Send Email Trigger")
.startAt(Date.from(startAt.toInstant()))
.withSchedule(SimpleScheduleBuilder.simpleSchedule().withMisfireHandlingInstructionFireNow())
.build();
}
}
Spring Boot has built-in support for Quartz. It automatically creates a Quartz Scheduler
bean with the configuration that we supplied in the application.properties
file. That’s why we could directly inject the Scheduler
in the controller.
In the /scheduleEmail
API,
-
We first validate the request body
-
Then, Build a JobDetail instance with a JobDataMap that contains the recipient email, subject, and body. The
JobDetail
that we create is of typeEmailJob
. We’ll defineEmailJob
in the next section. -
Next, we Build a Trigger instance that defines when the Job should be executed.
-
Finally, we schedule the Job using
scheduler.scheduleJob()
API.
Creating the Quartz Job to sends emails
Let’s now define the Job that sends the actual emails. Spring Boot provides a wrapper around Quartz Scheduler’s Job interface called QuartzJobBean
. This allows you to create Quartz Jobs as Spring beans where you can autowire other beans.
Let’s create our EmailJob
by extending QuartzJobBean
-
package com.example.quartzdemo.job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.nio.charset.StandardCharsets;
@Component
public class EmailJob extends QuartzJobBean {
private static final Logger logger = LoggerFactory.getLogger(EmailJob.class);
@Autowired
private JavaMailSender mailSender;
@Autowired
private MailProperties mailProperties;
@Override
protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
logger.info("Executing Job with key {}", jobExecutionContext.getJobDetail().getKey());
JobDataMap jobDataMap = jobExecutionContext.getMergedJobDataMap();
String subject = jobDataMap.getString("subject");
String body = jobDataMap.getString("body");
String recipientEmail = jobDataMap.getString("email");
sendMail(mailProperties.getUsername(), recipientEmail, subject, body);
}
private void sendMail(String fromEmail, String toEmail, String subject, String body) {
try {
logger.info("Sending Email to {}", toEmail);
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(message, StandardCharsets.UTF_8.toString());
messageHelper.setSubject(subject);
messageHelper.setText(body, true);
messageHelper.setFrom(fromEmail);
messageHelper.setTo(toEmail);
mailSender.send(message);
} catch (MessagingException ex) {
logger.error("Failed to send email to {}", toEmail);
}
}
}
Running the Application and Testing the API
It’s time to run the application and watch the live action. Open your terminal, go to the root directory of the project and type the following command to run it -
mvn spring-boot:run -Dspring.mail.password=<YOUR_SMTP_PASSWORD>
You don’t need to pass the spring.mail.password
command line argument if you have already set the password in the application.properties
file.
The application will start on port 8080
by default. Let’s now schedule an email using the /scheduleEmail
API -
<source media="(max-width: 520px)" srcset="https://www.callicoder.com/assets/images/post/medium/spring-boot-quartz-scheduler-email-scheduling-api.jpg" style="box-sizing: border-box;"></source>
And, Here I get the email at the specified time :-)
<source media="(max-width: 520px)" srcset="https://www.callicoder.com/assets/images/post/medium/spring-boot-quartz-scheduler-dyanmic-email-job-scheduling-example.jpg" style="box-sizing: border-box;"></source>
Conclusion
That’s all folks! I hope you enjoyed the article. You can find the complete source code of the project in the Github Repository. Consider giving the project a star on Github if you find it useful.
References
quartz 的数据库表结构:
# | |
# In your Quartz properties file, you'll need to set | |
# org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate | |
# | |
# | |
# By: Ron Cordell - roncordell | |
# I didn't see this anywhere, so I thought I'd post it here. This is the script from Quartz to create the tables in a MySQL database, modified to use INNODB instead of MYISAM. | |
DROP TABLE IF EXISTS QRTZ_FIRED_TRIGGERS; | |
DROP TABLE IF EXISTS QRTZ_PAUSED_TRIGGER_GRPS; | |
DROP TABLE IF EXISTS QRTZ_SCHEDULER_STATE; | |
DROP TABLE IF EXISTS QRTZ_LOCKS; | |
DROP TABLE IF EXISTS QRTZ_SIMPLE_TRIGGERS; | |
DROP TABLE IF EXISTS QRTZ_SIMPROP_TRIGGERS; | |
DROP TABLE IF EXISTS QRTZ_CRON_TRIGGERS; | |
DROP TABLE IF EXISTS QRTZ_BLOB_TRIGGERS; | |
DROP TABLE IF EXISTS QRTZ_TRIGGERS; | |
DROP TABLE IF EXISTS QRTZ_JOB_DETAILS; | |
DROP TABLE IF EXISTS QRTZ_CALENDARS; | |
CREATE TABLE QRTZ_JOB_DETAILS( | |
SCHED_NAME VARCHAR(120) NOT NULL, | |
JOB_NAME VARCHAR(190) NOT NULL, | |
JOB_GROUP VARCHAR(190) NOT NULL, | |
DESCRIPTION VARCHAR(250) NULL, | |
JOB_CLASS_NAME VARCHAR(250) NOT NULL, | |
IS_DURABLE VARCHAR(1) NOT NULL, | |
IS_NONCONCURRENT VARCHAR(1) NOT NULL, | |
IS_UPDATE_DATA VARCHAR(1) NOT NULL, | |
REQUESTS_RECOVERY VARCHAR(1) NOT NULL, | |
JOB_DATA BLOB NULL, | |
PRIMARY KEY (SCHED_NAME,JOB_NAME,JOB_GROUP)) | |
ENGINE=InnoDB; | |
CREATE TABLE QRTZ_TRIGGERS ( | |
SCHED_NAME VARCHAR(120) NOT NULL, | |
TRIGGER_NAME VARCHAR(190) NOT NULL, | |
TRIGGER_GROUP VARCHAR(190) NOT NULL, | |
JOB_NAME VARCHAR(190) NOT NULL, | |
JOB_GROUP VARCHAR(190) NOT NULL, | |
DESCRIPTION VARCHAR(250) NULL, | |
NEXT_FIRE_TIME BIGINT(13) NULL, | |
PREV_FIRE_TIME BIGINT(13) NULL, | |
PRIORITY INTEGER NULL, | |
TRIGGER_STATE VARCHAR(16) NOT NULL, | |
TRIGGER_TYPE VARCHAR(8) NOT NULL, | |
START_TIME BIGINT(13) NOT NULL, | |
END_TIME BIGINT(13) NULL, | |
CALENDAR_NAME VARCHAR(190) NULL, | |
MISFIRE_INSTR SMALLINT(2) NULL, | |
JOB_DATA BLOB NULL, | |
PRIMARY KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP), | |
FOREIGN KEY (SCHED_NAME,JOB_NAME,JOB_GROUP) | |
REFERENCES QRTZ_JOB_DETAILS(SCHED_NAME,JOB_NAME,JOB_GROUP)) | |
ENGINE=InnoDB; | |
CREATE TABLE QRTZ_SIMPLE_TRIGGERS ( | |
SCHED_NAME VARCHAR(120) NOT NULL, | |
TRIGGER_NAME VARCHAR(190) NOT NULL, | |
TRIGGER_GROUP VARCHAR(190) NOT NULL, | |
REPEAT_COUNT BIGINT(7) NOT NULL, | |
REPEAT_INTERVAL BIGINT(12) NOT NULL, | |
TIMES_TRIGGERED BIGINT(10) NOT NULL, | |
PRIMARY KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP), | |
FOREIGN KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP) | |
REFERENCES QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)) | |
ENGINE=InnoDB; | |
CREATE TABLE QRTZ_CRON_TRIGGERS ( | |
SCHED_NAME VARCHAR(120) NOT NULL, | |
TRIGGER_NAME VARCHAR(190) NOT NULL, | |
TRIGGER_GROUP VARCHAR(190) NOT NULL, | |
CRON_EXPRESSION VARCHAR(120) NOT NULL, | |
TIME_ZONE_ID VARCHAR(80), | |
PRIMARY KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP), | |
FOREIGN KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP) | |
REFERENCES QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)) | |
ENGINE=InnoDB; | |
CREATE TABLE QRTZ_SIMPROP_TRIGGERS | |
( | |
SCHED_NAME VARCHAR(120) NOT NULL, | |
TRIGGER_NAME VARCHAR(190) NOT NULL, | |
TRIGGER_GROUP VARCHAR(190) NOT NULL, | |
STR_PROP_1 VARCHAR(512) NULL, | |
STR_PROP_2 VARCHAR(512) NULL, | |
STR_PROP_3 VARCHAR(512) NULL, | |
INT_PROP_1 INT NULL, | |
INT_PROP_2 INT NULL, | |
LONG_PROP_1 BIGINT NULL, | |
LONG_PROP_2 BIGINT NULL, | |
DEC_PROP_1 NUMERIC(13,4) NULL, | |
DEC_PROP_2 NUMERIC(13,4) NULL, | |
BOOL_PROP_1 VARCHAR(1) NULL, | |
BOOL_PROP_2 VARCHAR(1) NULL, | |
PRIMARY KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP), | |
FOREIGN KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP) | |
REFERENCES QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)) | |
ENGINE=InnoDB; | |
CREATE TABLE QRTZ_BLOB_TRIGGERS ( | |
SCHED_NAME VARCHAR(120) NOT NULL, | |
TRIGGER_NAME VARCHAR(190) NOT NULL, | |
TRIGGER_GROUP VARCHAR(190) NOT NULL, | |
BLOB_DATA BLOB NULL, | |
PRIMARY KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP), | |
INDEX (SCHED_NAME,TRIGGER_NAME, TRIGGER_GROUP), | |
FOREIGN KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP) | |
REFERENCES QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)) | |
ENGINE=InnoDB; | |
CREATE TABLE QRTZ_CALENDARS ( | |
SCHED_NAME VARCHAR(120) NOT NULL, | |
CALENDAR_NAME VARCHAR(190) NOT NULL, | |
CALENDAR BLOB NOT NULL, | |
PRIMARY KEY (SCHED_NAME,CALENDAR_NAME)) | |
ENGINE=InnoDB; | |
CREATE TABLE QRTZ_PAUSED_TRIGGER_GRPS ( | |
SCHED_NAME VARCHAR(120) NOT NULL, | |
TRIGGER_GROUP VARCHAR(190) NOT NULL, | |
PRIMARY KEY (SCHED_NAME,TRIGGER_GROUP)) | |
ENGINE=InnoDB; | |
CREATE TABLE QRTZ_FIRED_TRIGGERS ( | |
SCHED_NAME VARCHAR(120) NOT NULL, | |
ENTRY_ID VARCHAR(95) NOT NULL, | |
TRIGGER_NAME VARCHAR(190) NOT NULL, | |
TRIGGER_GROUP VARCHAR(190) NOT NULL, | |
INSTANCE_NAME VARCHAR(190) NOT NULL, | |
FIRED_TIME BIGINT(13) NOT NULL, | |
SCHED_TIME BIGINT(13) NOT NULL, | |
PRIORITY INTEGER NOT NULL, | |
STATE VARCHAR(16) NOT NULL, | |
JOB_NAME VARCHAR(190) NULL, | |
JOB_GROUP VARCHAR(190) NULL, | |
IS_NONCONCURRENT VARCHAR(1) NULL, | |
REQUESTS_RECOVERY VARCHAR(1) NULL, | |
PRIMARY KEY (SCHED_NAME,ENTRY_ID)) | |
ENGINE=InnoDB; | |
CREATE TABLE QRTZ_SCHEDULER_STATE ( | |
SCHED_NAME VARCHAR(120) NOT NULL, | |
INSTANCE_NAME VARCHAR(190) NOT NULL, | |
LAST_CHECKIN_TIME BIGINT(13) NOT NULL, | |
CHECKIN_INTERVAL BIGINT(13) NOT NULL, | |
PRIMARY KEY (SCHED_NAME,INSTANCE_NAME)) | |
ENGINE=InnoDB; | |
CREATE TABLE QRTZ_LOCKS ( | |
SCHED_NAME VARCHAR(120) NOT NULL, | |
LOCK_NAME VARCHAR(40) NOT NULL, | |
PRIMARY KEY (SCHED_NAME,LOCK_NAME)) | |
ENGINE=InnoDB; | |
CREATE INDEX IDX_QRTZ_J_REQ_RECOVERY ON QRTZ_JOB_DETAILS(SCHED_NAME,REQUESTS_RECOVERY); | |
CREATE INDEX IDX_QRTZ_J_GRP ON QRTZ_JOB_DETAILS(SCHED_NAME,JOB_GROUP); | |
CREATE INDEX IDX_QRTZ_T_J ON QRTZ_TRIGGERS(SCHED_NAME,JOB_NAME,JOB_GROUP); | |
CREATE INDEX IDX_QRTZ_T_JG ON QRTZ_TRIGGERS(SCHED_NAME,JOB_GROUP); | |
CREATE INDEX IDX_QRTZ_T_C ON QRTZ_TRIGGERS(SCHED_NAME,CALENDAR_NAME); | |
CREATE INDEX IDX_QRTZ_T_G ON QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_GROUP); | |
CREATE INDEX IDX_QRTZ_T_STATE ON QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_STATE); | |
CREATE INDEX IDX_QRTZ_T_N_STATE ON QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP,TRIGGER_STATE); | |
CREATE INDEX IDX_QRTZ_T_N_G_STATE ON QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_GROUP,TRIGGER_STATE); | |
CREATE INDEX IDX_QRTZ_T_NEXT_FIRE_TIME ON QRTZ_TRIGGERS(SCHED_NAME,NEXT_FIRE_TIME); | |
CREATE INDEX IDX_QRTZ_T_NFT_ST ON QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_STATE,NEXT_FIRE_TIME); | |
CREATE INDEX IDX_QRTZ_T_NFT_MISFIRE ON QRTZ_TRIGGERS(SCHED_NAME,MISFIRE_INSTR,NEXT_FIRE_TIME); | |
CREATE INDEX IDX_QRTZ_T_NFT_ST_MISFIRE ON QRTZ_TRIGGERS(SCHED_NAME,MISFIRE_INSTR,NEXT_FIRE_TIME,TRIGGER_STATE); | |
CREATE INDEX IDX_QRTZ_T_NFT_ST_MISFIRE_GRP ON QRTZ_TRIGGERS(SCHED_NAME,MISFIRE_INSTR,NEXT_FIRE_TIME,TRIGGER_GROUP,TRIGGER_STATE); | |
CREATE INDEX IDX_QRTZ_FT_TRIG_INST_NAME ON QRTZ_FIRED_TRIGGERS(SCHED_NAME,INSTANCE_NAME); | |
CREATE INDEX IDX_QRTZ_FT_INST_JOB_REQ_RCVRY ON QRTZ_FIRED_TRIGGERS(SCHED_NAME,INSTANCE_NAME,REQUESTS_RECOVERY); | |
CREATE INDEX IDX_QRTZ_FT_J_G ON QRTZ_FIRED_TRIGGERS(SCHED_NAME,JOB_NAME,JOB_GROUP); | |
CREATE INDEX IDX_QRTZ_FT_JG ON QRTZ_FIRED_TRIGGERS(SCHED_NAME,JOB_GROUP); | |
CREATE INDEX IDX_QRTZ_FT_T_G ON QRTZ_FIRED_TRIGGERS(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP); | |
CREATE INDEX IDX_QRTZ_FT_TG ON QRTZ_FIRED_TRIGGERS(SCHED_NAME,TRIGGER_GROUP); | |
commit; |
相关推荐
Spring Boot Quartz Scheduler示例:构建电子邮件调度应用 完整的教程: : 要求 Java-1.8.x Maven-3.xx MySQL-5.xx 设定步骤 1.克隆应用程序 git clone ...
在IT行业中,Spring Boot和Quartz是两个非常重要的框架,它们在企业级应用开发中扮演着关键角色。Spring Boot简化了Spring应用的初始搭建以及开发过程,提供了开箱即用的功能,而Quartz则是一个强大的任务调度库,常...
在Spring Boot应用中整合Quartz定时任务是一种常见的需求,它可以帮助我们执行周期性的后台任务,如数据同步、报表生成等。Spring Boot与Quartz的结合提供了便捷的配置方式,特别是通过YAML(YAML Ain't Markup ...
当我们结合 Spring Boot 和 Quartz,我们可以构建一个强大的定时任务管理系统,这在许多业务场景中非常有用,比如数据清理、报告生成、定期发送邮件等。 首先,让我们深入理解一下Spring Boot如何与Quartz集成。在...
quartz scheduler 入门教程 Quartz Scheduler 是一种功能丰富、开源的任务调度程序库,可以在任何 Java 程序中使用。它可以用来创建简单或者复杂的执行次数可以达成千上万的任务。任务可以是任何 Java 可以做的事情...
<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> ``` 在这个项目中,我们还可以看到Quartz的监听器(如`JobListener`和`TriggerListener`)的使用,它们可以...
在Spring中,我们可以使用`SchedulerFactoryBean`作为Spring Bean,它会自动管理和启动Quartz Scheduler。我们还需要创建一个JobDetail,它包含了Job的具体信息,以及一个Trigger,定义了Job的执行时间。这两者可以...
在Spring Boot中集成Quartz,首先需要添加Quartz和Spring Boot Starter Scheduler的依赖。在`pom.xml`文件中,添加如下依赖: ```xml <groupId>org.springframework.boot <artifactId>spring-boot-starter-...
spring-boot-quartz-demo, 使用Quartz框架的样例 Spring Boot 应用程序 spring-boot-quartz-demo使用Quartz框架的样例 Spring Boot 应用程序基于 https://gist.github.com/jelies/5085593 附加功能( 断点,失火处理...
在这个"springboot与scheduler结合的定时任务工具、实例项目"中,我们将深入探讨如何在Spring Boot应用中利用Spring Scheduler来执行计划任务。 首先,Spring Scheduler提供了一个方便的方式来安排和执行周期性任务...
1. 启动:Spring Boot会自动启动Quartz Scheduler。如果你需要自定义启动逻辑,可以重写`ApplicationRunner`或`CommandLineRunner`接口的`run`方法。 2. 管理任务:Quartz提供了API来管理和操作任务,包括启动、...
3. **Scheduler(调度器)**: Scheduler是Quartz的核心,负责管理所有的Job和Trigger,并根据Trigger的时间规则执行Job。 **使用quartz_jobs.xml配置文件** 在Quartz.NET 3.x中,虽然XML配置文件并不是默认的首选...
本资源是一个最新 Spring 4.2.2 集成 Quartz Scheduler 2.2.2 的一个简单的 demo,也是博客《最新 Spring 4.2.2 集成 Quartz Scheduler 2.2.2 任务调度示例》的配套示例项目,该博客地址是:...
Spring 提供了与 Quartz 集成的模块,使得在 Spring 应用程序中使用 Quartz 更加便捷,包括 Job 实例的管理、触发器的配置以及对 Quartz Scheduler 的控制。 在这个 "spring+quartz demo" 中,我们可以学习到如何将...
Quartz Scheduler的Spring-Boot自动配置只是Quartz Scheduler的Spring-Boot启动器。 当然,Quartz Scheduler已经有好几个启动器,但是它们都不能满足我的所有需求,因此我创建了自己的启动器。 这只是一个业余项目。...
《Spring Quartz:构建可扩展的企业级应用》是一本专注于Quartz Job调度框架的专业书籍,它深入探讨了如何在Spring框架中有效地使用Quartz进行任务调度。Quartz是Java平台上广泛使用的开源作业调度库,它提供了丰富...
- `spring-boot-email-example`:展示如何通过 JavaMailSender 发送邮件。 **4. 数据访问** Spring Boot 提供了对多种数据存储的支持,包括: - SQL 数据库:通过 Spring Data JPA、MyBatis 等与 Hibernate、JDBC ...
implementation 'org.quartz-scheduler:quartz:2.x.y.RELEASE' implementation 'org.quartz-scheduler:quartz-jobs:2.x.y.RELEASE' ``` 2. **配置Quartz** 在Spring的配置文件(如applicationContext.xml)中,...
- 配置Quartz:在Spring的配置文件(如`applicationContext.xml`)中,定义`SchedulerFactoryBean`,配置Quartz的基本参数,如线程池大小、JobStore类型等。 - 注册Job和Trigger:在配置文件中,通过`<bean>`标签...
Spring Quartz是一款广泛使用的Java任务调度框架,用于在应用程序中安排周期性或一次性任务。Quartz集群(Cluster)是指多个Quartz实例(节点)共享相同的作业和触发器,从而实现高可用性和故障转移。本文将深入探讨...