Skip to content

Auto start quartz tables #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 3 additions & 16 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,12 @@
git clone https://github.com/callicoder/spring-boot-mysql-rest-api-tutorial.git
```

**2. Create MySQL database**

```bash
create database quartz_demo
```

**3. Change MySQL username and password as per your MySQL installation**
**2. Change MySQL username and password as per your MySQL installation**

open `src/main/resources/application.properties`, and change `spring.datasource.username` and `spring.datasource.password` properties as per your mysql installation


**4. Setup Spring Mail**
**3. Setup Spring Mail**

The project is using Gmail's SMTP server for sending emails. Whether you use Gmail or any other SMTP server, you'll need to configure the following mail properties accordingly -

Expand All @@ -45,15 +39,8 @@ If you're using Gmail, you need to allow the third party apps to send emails by
+ Go to https://myaccount.google.com/security?pli=1#connectedapps
+ Set ‘Allow less secure apps’ to YES

**5. Create Quartz Tables**

The project stores all the scheduled Jobs in MySQL database. You'll need to create the tables that Quartz uses to store Jobs and other job-related data. Please create Quartz specific tables by executing the `quartz_tables.sql` script located inside `src/main/resources` directory.

```bash
mysql> source <PATH_TO_QUARTZ_TABLES.sql>
```

**6. Build and run the app using maven**
**4. Build and run the app using maven**

Finally, You can run the app by typing the following command from the root directory of the project -

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
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;
Expand All @@ -21,9 +20,11 @@
@RestController
public class EmailJobSchedulerController {
private static final Logger logger = LoggerFactory.getLogger(EmailJobSchedulerController.class);
private final Scheduler scheduler;

@Autowired
private Scheduler scheduler;
public EmailJobSchedulerController(Scheduler scheduler) {
this.scheduler = scheduler;
}

@PostMapping("/scheduleEmail")
public ResponseEntity<ScheduleEmailResponse> scheduleEmail(@Valid @RequestBody ScheduleEmailRequest scheduleEmailRequest) {
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/com/example/quartzdemo/job/EmailJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

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;
Expand All @@ -20,14 +18,16 @@
public class EmailJob extends QuartzJobBean {
private static final Logger logger = LoggerFactory.getLogger(EmailJob.class);

@Autowired
private JavaMailSender mailSender;
private final JavaMailSender mailSender;
private final MailProperties mailProperties;

@Autowired
private MailProperties mailProperties;
public EmailJob(JavaMailSender mailSender, MailProperties mailProperties) {
this.mailSender = mailSender;
this.mailProperties = mailProperties;
}

@Override
protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
protected void executeInternal(JobExecutionContext jobExecutionContext){
logger.info("Executing Job with key {}", jobExecutionContext.getJobDetail().getKey());

JobDataMap jobDataMap = jobExecutionContext.getMergedJobDataMap();
Expand Down
16 changes: 9 additions & 7 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.url = jdbc:mysql://localhost:3306/quartz_demo?useSSL=false
spring.datasource.username = root
spring.datasource.password = callicoder
# Auto create database if it doesn't exist
spring.datasource.url = jdbc:mysql://localhost:3306/quartz_demo?createDatabaseIfNotExist=true
spring.datasource.username =
spring.datasource.password =


## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
Expand All @@ -11,13 +13,13 @@ spring.jpa.hibernate.ddl-auto = update

## QuartzProperties
spring.quartz.job-store-type=jdbc
spring.quartz.jdbc.schema=classpath:org/quartz/impl/jdbcjobstore/tables_mysql_innodb.sql
spring.quartz.jdbc.initialize-schema=always
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.host=
spring.mail.port=
spring.mail.username=
spring.mail.password=

spring.mail.properties.mail.smtp.auth=true
Expand Down
179 changes: 0 additions & 179 deletions src/main/resources/quartz_tables.sql

This file was deleted.