Spring Boot Quartz Scheduler Example — Email Scheduling Application

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.

Creating the Application

spring init -d=web,jpa,mysql,quartz,mail -n=quartz-demo quartz-demo

Configuring MySQL database, Quartz Scheduler, and Mail Sender

## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.url = jdbc:mysql://localhost:3306/quartz_demo?useSSL=false
spring.datasource.username = root
spring.datasource.password =
## 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=kjlrawal9@gmail.com
spring.mail.password=
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true

Creating Quartz Tables

mysql> source <PATH_TO_QUARTZ_TABLES.sql>

Overview of Quartz Scheduler’s APIs and Terminologies

Creating a REST API to schedule Email Jobs dynamically in Quartz

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)
}
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

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();
}
}

Creating the Quartz Job to sends emails

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

mvn spring-boot:run -Dspring.mail.password=<YOUR_SMTP_PASSWORD>

Conclusion

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store