From 116d1e6e18a74ac53c9f77b301827579e364b1f0 Mon Sep 17 00:00:00 2001
From: Bunny <1319900154@qq.com>
Date: Fri, 26 Jul 2024 23:49:07 +0800
Subject: [PATCH] =?UTF-8?q?:zap:=20feat(=E4=BF=AE=E6=94=B9):=20=E4=BF=AE?=
=?UTF-8?q?=E6=94=B9=E6=96=87=E4=BB=B6=E9=85=8D=E7=BD=AE=EF=BC=8C=E6=8C=87?=
=?UTF-8?q?=E5=AE=9A=E6=95=B0=E6=8D=AE=E5=BA=93?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
common/pom.xml | 12 +++++
service/pom.xml | 14 +-----
.../service/controller/JobController.java | 2 +-
.../service/quartz/QuartzConfiguration.java | 50 +++++++++++++++++++
.../service/quartz/QuartzJobFactory.java | 28 +++++++++++
service/src/main/resources/application.yml | 6 ++-
service/src/main/resources/quartz.properties | 11 ++++
7 files changed, 107 insertions(+), 16 deletions(-)
create mode 100644 service/src/main/java/cn/bunny/service/quartz/QuartzConfiguration.java
create mode 100644 service/src/main/java/cn/bunny/service/quartz/QuartzJobFactory.java
create mode 100644 service/src/main/resources/quartz.properties
diff --git a/common/pom.xml b/common/pom.xml
index 838efbf..fdde5d5 100644
--- a/common/pom.xml
+++ b/common/pom.xml
@@ -25,5 +25,17 @@
io.jsonwebtoken
jjwt
+
+
+ org.quartz-scheduler
+ quartz
+ 2.3.2
+
+
+
+ com.mchange
+ c3p0
+ 0.9.5.5
+
diff --git a/service/pom.xml b/service/pom.xml
index f5f767e..904dfa8 100644
--- a/service/pom.xml
+++ b/service/pom.xml
@@ -60,19 +60,7 @@
org.aspectj
aspectjweaver
-
-
- org.quartz-scheduler
- quartz
- 2.3.2
-
-
-
- com.mchange
- c3p0
- 0.9.5.5
-
-
+
com.baomidou
dynamic-datasource-spring-boot3-starter
diff --git a/service/src/main/java/cn/bunny/service/controller/JobController.java b/service/src/main/java/cn/bunny/service/controller/JobController.java
index 8f3f586..d4affb6 100644
--- a/service/src/main/java/cn/bunny/service/controller/JobController.java
+++ b/service/src/main/java/cn/bunny/service/controller/JobController.java
@@ -26,7 +26,7 @@ public class JobController {
@PathVariable Integer page,
@PathVariable Integer limit
) {
- Page pageParams = new Page<>();
+ Page pageParams = new Page<>(page,limit);
PageResult pageResult = jobService.pageQuery(pageParams);
return Result.success(pageResult);
}
diff --git a/service/src/main/java/cn/bunny/service/quartz/QuartzConfiguration.java b/service/src/main/java/cn/bunny/service/quartz/QuartzConfiguration.java
new file mode 100644
index 0000000..05aad90
--- /dev/null
+++ b/service/src/main/java/cn/bunny/service/quartz/QuartzConfiguration.java
@@ -0,0 +1,50 @@
+package cn.bunny.service.quartz;
+
+import org.quartz.Scheduler;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.config.PropertiesFactoryBean;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.core.io.ClassPathResource;
+import org.springframework.scheduling.quartz.SchedulerFactoryBean;
+
+import java.io.IOException;
+import java.util.Properties;
+
+@Configuration
+public class QuartzConfiguration {
+
+ @Autowired
+ private QuartzJobFactory quartzJobFactory;
+
+ //创建调度器工厂
+ @Bean
+ public SchedulerFactoryBean schedulerFactoryBean(){
+ //1.创建SchedulerFactoryBean
+ //2.加载自定义的quartz.properties配置文件
+ //3.设置MyJobFactory
+ SchedulerFactoryBean factoryBean=new SchedulerFactoryBean();
+ try {
+ factoryBean.setAutoStartup(true);
+ factoryBean.setStartupDelay(3);//延时5秒启动
+ factoryBean.setQuartzProperties(quartzProperties());
+ factoryBean.setJobFactory(quartzJobFactory);
+ return factoryBean;
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Bean
+ public Properties quartzProperties() throws IOException {
+ PropertiesFactoryBean propertiesFactoryBean=new PropertiesFactoryBean();
+ propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
+ propertiesFactoryBean.afterPropertiesSet();
+ return propertiesFactoryBean.getObject();
+ }
+
+ @Bean(name="scheduler")
+ public Scheduler scheduler(){
+ return schedulerFactoryBean().getScheduler();
+ }
+}
\ No newline at end of file
diff --git a/service/src/main/java/cn/bunny/service/quartz/QuartzJobFactory.java b/service/src/main/java/cn/bunny/service/quartz/QuartzJobFactory.java
new file mode 100644
index 0000000..b42d42a
--- /dev/null
+++ b/service/src/main/java/cn/bunny/service/quartz/QuartzJobFactory.java
@@ -0,0 +1,28 @@
+package cn.bunny.service.quartz;
+
+import lombok.extern.slf4j.Slf4j;
+import org.jetbrains.annotations.NotNull;
+import org.quartz.spi.TriggerFiredBundle;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
+import org.springframework.scheduling.quartz.AdaptableJobFactory;
+import org.springframework.stereotype.Component;
+
+@Component
+@Slf4j
+public class QuartzJobFactory extends AdaptableJobFactory {
+
+ // 这个对象Spring会帮我们自动注入进来
+ @Autowired
+ private AutowireCapableBeanFactory capableBeanFactory;
+
+ // 重写创建Job任务的实例方法
+ @NotNull
+ @Override
+ protected Object createJobInstance(@NotNull TriggerFiredBundle bundle) throws Exception {
+ Object jobInstance = super.createJobInstance(bundle);
+ // 通过以下方式,解决Job任务无法使用Spring中的Bean问题
+ capableBeanFactory.autowireBean(jobInstance);
+ return super.createJobInstance(bundle);
+ }
+}
\ No newline at end of file
diff --git a/service/src/main/resources/application.yml b/service/src/main/resources/application.yml
index 926a0d7..1cd5215 100644
--- a/service/src/main/resources/application.yml
+++ b/service/src/main/resources/application.yml
@@ -8,7 +8,7 @@ spring:
name: bunny-service
datasource:
dynamic:
- primary: quartz #设置默认的数据源或者数据源组,默认值即为master
+ primary: master #设置默认的数据源或者数据源组,默认值即为master
strict: false #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
grace-destroy: false #是否优雅关闭数据源,默认为false,设置为true时,关闭数据源时如果数据源中还存在活跃连接,至多等待10s后强制关闭
datasource:
@@ -41,11 +41,13 @@ spring:
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
+
quartz:
+ # 使用数据库连接
job-store-type: jdbc
jdbc:
+ # 是否设置
initialize-schema: embedded
- scheduler-name: quartzScheduler
auto-startup: true
wait-for-jobs-to-complete-on-shutdown: true
overwrite-existing-jobs: false
diff --git a/service/src/main/resources/quartz.properties b/service/src/main/resources/quartz.properties
new file mode 100644
index 0000000..6e65fc6
--- /dev/null
+++ b/service/src/main/resources/quartz.properties
@@ -0,0 +1,11 @@
+org.quartz.scheduler.instanceName=quartzScheduler
+org.quartz.threadPool.threadCount=5
+org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX
+org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate
+org.quartz.jobStore.tablePrefix=QRTZ_
+org.quartz.jobStore.dataSource=myDS
+org.quartz.dataSource.myDS.driver=com.mysql.cj.jdbc.Driver
+org.quartz.dataSource.myDS.URL=jdbc:mysql://106.15.251.123:3305/myDS?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=true
+org.quartz.dataSource.myDS.user=root
+org.quartz.dataSource.myDS.password=02120212
+org.quartz.dataSource.myDS.maxConnections=5
\ No newline at end of file