Skip to content

Commit f516120

Browse files
committed
quartz + JPA
1 parent 5167bc4 commit f516120

File tree

7 files changed

+178
-20
lines changed

7 files changed

+178
-20
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.ylc.note.quartz.constant;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
6+
/**
7+
* 代码全万行,注释第一行
8+
* 注释不规范,同事泪两行
9+
*
10+
* @author YuLc
11+
* @version 1.0.0
12+
* @date 2020-09-12
13+
*/
14+
public class EnumConstant {
15+
16+
@Getter
17+
@AllArgsConstructor
18+
public enum JobStatus {
19+
20+
PAUSED("0", "暂停"),
21+
NORMAL("1", "正常");
22+
23+
24+
private String code;
25+
26+
private String value;
27+
28+
}
29+
30+
}

quartz/src/main/java/org/ylc/note/quartz/controller/ScheduleController.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package org.ylc.note.quartz.controller;
22

3-
import org.springframework.web.bind.annotation.RequestMapping;
4-
import org.springframework.web.bind.annotation.RestController;
3+
import org.springframework.web.bind.annotation.*;
4+
import org.ylc.note.quartz.entity.ScheduleJob;
5+
import org.ylc.note.quartz.service.ScheduleJobService;
6+
7+
import java.util.List;
58

69
/**
710
* 代码全万行,注释第一行
@@ -15,6 +18,22 @@
1518
@RestController
1619
public class ScheduleController {
1720

21+
private final ScheduleJobService scheduleJobService;
22+
23+
public ScheduleController(ScheduleJobService scheduleJobService) {
24+
this.scheduleJobService = scheduleJobService;
25+
}
26+
27+
@GetMapping("/list")
28+
public List<ScheduleJob> list() {
29+
List<ScheduleJob> jobs = scheduleJobService.list();
30+
jobs.forEach(System.out::print);
31+
return jobs;
32+
}
1833

34+
@PostMapping("/create")
35+
public void create(@RequestBody ScheduleJob job) {
36+
scheduleJobService.createJob(job);
37+
}
1938

2039
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package org.ylc.note.quartz.entity;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
6+
import javax.persistence.*;
7+
import java.time.LocalDateTime;
8+
9+
/**
10+
* 代码全万行,注释第一行
11+
* 注释不规范,同事泪两行
12+
* <p>
13+
* 定时任务
14+
*
15+
* @author YuLc
16+
* @version 1.0.0
17+
* @date 2020-09-12
18+
*/
19+
@Getter
20+
@Setter
21+
@Entity
22+
@Table(name = "schedule_job")
23+
public class ScheduleJob {
24+
25+
@Id
26+
@GeneratedValue(strategy = GenerationType.IDENTITY)
27+
private Long id;
28+
29+
@Column(name = "class_name", nullable = false)
30+
private String className;
31+
32+
@Column(name = "method_name", nullable = false)
33+
private String methodName;
34+
35+
private String parameter;
36+
37+
@Column(name = "cron_expression", nullable = false)
38+
private String cronExpression;
39+
40+
private String status;
41+
42+
@Column(name = "job_sched")
43+
private String jobSched;
44+
45+
private String remark;
46+
47+
@Column(name = "create_time", nullable = false)
48+
private LocalDateTime createTime;
49+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.ylc.note.quartz.repository;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
import org.ylc.note.quartz.entity.ScheduleJob;
5+
6+
/**
7+
* 代码全万行,注释第一行
8+
* 注释不规范,同事泪两行
9+
* <p>
10+
* 定时任务数据库操作接口
11+
*
12+
* @author YuLc
13+
* @version 1.0.0
14+
* @date 2020-09-12
15+
*/
16+
public interface ScheduleJobRepository extends JpaRepository<ScheduleJob, Long> {
17+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.ylc.note.quartz.service;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Service;
5+
import org.ylc.note.quartz.constant.EnumConstant;
6+
import org.ylc.note.quartz.entity.ScheduleJob;
7+
import org.ylc.note.quartz.repository.ScheduleJobRepository;
8+
9+
import java.time.LocalDateTime;
10+
import java.util.List;
11+
12+
/**
13+
* 代码全万行,注释第一行
14+
* 注释不规范,同事泪两行
15+
*
16+
* @author YuLc
17+
* @version 1.0.0
18+
* @date 2020-09-11
19+
*/
20+
@Service
21+
public class ScheduleJobService {
22+
23+
@Autowired
24+
private ScheduleJobRepository scheduleJobRepository;
25+
26+
@Autowired
27+
private QuartzService quartzService;
28+
29+
public List<ScheduleJob> list() {
30+
return scheduleJobRepository.findAll();
31+
}
32+
33+
public void createJob(ScheduleJob job) {
34+
job.setStatus(EnumConstant.JobStatus.NORMAL.getCode());
35+
job.setCreateTime(LocalDateTime.now());
36+
scheduleJobRepository.save(job);
37+
quartzService.createJob();
38+
}
39+
40+
public void pauseJob() {
41+
quartzService.pauseJob();
42+
}
43+
44+
public void restoreJob() {
45+
quartzService.restoreJob();
46+
}
47+
48+
public void modifyJob() {
49+
quartzService.modifyJob();
50+
}
51+
52+
public void removeJob() {
53+
quartzService.restoreJob();
54+
}
55+
}

quartz/src/main/java/org/ylc/note/quartz/service/ScheduleService.java

Lines changed: 0 additions & 15 deletions
This file was deleted.

quartz/src/main/resources/application.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ spring:
22
application:
33
name: quzrtz
44
datasource:
5-
url: jdbc:mysql://localhost:3306/quarzt?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&autoReconnect=true
5+
url: jdbc:mysql://localhost:3306/quartz?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&autoReconnect=true
66
username: root
7-
password: root2019!
7+
password: root2020!
88
driver-class-name: com.mysql.cj.jdbc.Driver
9+
type: com.zaxxer.hikari.HikariDataSource
910
# Hikari 连接池配置
1011
hikari:
1112
# 连接池名称
@@ -21,4 +22,6 @@ spring:
2122
maxLifetime: 1800000
2223
connection-test-query: select 1
2324
jpa:
24-
database: mysql
25+
database: mysql
26+
# 打印SQL语句
27+
show-sql: true

0 commit comments

Comments
 (0)