Skip to content

Commit

Permalink
spring quartz 数据库,已完成
Browse files Browse the repository at this point in the history
  • Loading branch information
YunaiV committed Nov 28, 2019
1 parent 74dce3c commit ab97d9c
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 15 deletions.
7 changes: 7 additions & 0 deletions lab-28/lab-28-task-quartz-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>

<!-- 方便等会写单元测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cn.iocoder.springboot.lab28.task;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application02 {

public static void main(String[] args) {
// 设置 Tomcat 随机端口
System.setProperty("server.port", "0");

// 启动 Spring Boot 应用
SpringApplication.run(Application.class, args);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.autoconfigure.quartz.QuartzDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -26,6 +27,7 @@ public DataSourceProperties userDataSourceProperties() {
/**
* 创建 user 数据源
*/
@Primary
@Bean(name = "userDataSource")
@ConfigurationProperties(prefix = "spring.datasource.user.hikari") // 读取 spring.datasource.user 配置到 HikariDataSource 对象
public DataSource userDataSource() {
Expand All @@ -49,6 +51,7 @@ public DataSourceProperties quartzDataSourceProperties() {
*/
@Bean(name = "quartzDataSource")
@ConfigurationProperties(prefix = "spring.datasource.quartz.hikari")
@QuartzDataSource
public DataSource quartzDataSource() {
// 获得 DataSourceProperties 对象
DataSourceProperties properties = this.quartzDataSourceProperties();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
@Configuration
public class ScheduleConfiguration {

@Configuration
public static class DemoJob01Configuration {

@Bean
Expand All @@ -36,7 +35,6 @@ public Trigger demoJob01Trigger() {

}

@Configuration
public static class DemoJob02Configuration {

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
package cn.iocoder.springboot.lab28.task.job;

import cn.iocoder.springboot.lab28.task.service.DemoService;
import org.quartz.DisallowConcurrentExecution;
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.scheduling.quartz.QuartzJobBean;

import java.util.concurrent.atomic.AtomicInteger;

@DisallowConcurrentExecution
public class DemoJob01 extends QuartzJobBean {

private Logger logger = LoggerFactory.getLogger(getClass());

private final AtomicInteger counts = new AtomicInteger();

@Autowired
private DemoService demoService;

@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
logger.info("[executeInternal][定时第 ({}) 次执行, demoService 为 ({})]", counts.incrementAndGet(),
demoService);
protected void executeInternal(JobExecutionContext context) {
logger.info("[executeInternal][我开始的执行了, demoService 为 ({})]", demoService);
}

}
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package cn.iocoder.springboot.lab28.task.job;

import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.quartz.QuartzJobBean;

@DisallowConcurrentExecution
public class DemoJob02 extends QuartzJobBean {

private Logger logger = LoggerFactory.getLogger(getClass());

@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
protected void executeInternal(JobExecutionContext context) {
logger.info("[executeInternal][我开始的执行了]");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,20 @@ spring:
properties: # 添加 Quartz Scheduler 附加属性,更多可以看 http://www.quartz-scheduler.org/documentation/2.4.0-SNAPSHOT/configuration.html 文档
org:
quartz:
# JobStore 相关配置
jobStore:
# 数据源名称
dataSource: quartzDataSource # 使用的数据源
class: org.quartz.impl.jdbcjobstore.JobStoreTX # JobStore 实现类
driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate
tablePrefix: QRTZ_ # Quartz 表前缀
isClustered: true # 是集群模式
clusterCheckinInterval: 1000
useProperties: false
# 线程池相关配置
threadPool:
threadCount: 25 # 线程池大小。默认为 10 。
threadPriority: 5 # 线程优先级
class: org.quartz.simpl.SimpleThreadPool # 线程池类型
jdbc: # 使用 JDBC 的 JobStore 的时候,JDBC 的配置
initialize-schema: never # TODO
initialize-schema: never # 是否自动使用 SQL 初始化 Quartz 表结构。这里设置成 never ,我们手动创建表结构。
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package cn.iocoder.springboot.lab28.task;

import cn.iocoder.springboot.lab28.task.job.DemoJob01;
import cn.iocoder.springboot.lab28.task.job.DemoJob02;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class QuartzSchedulerTest {

@Autowired
private Scheduler scheduler;

@Test
public void addDemoJob01Config() throws SchedulerException {
// 创建 JobDetail
JobDetail jobDetail = JobBuilder.newJob(DemoJob01.class)
.withIdentity("demoJob01") // 名字为 demoJob01
.storeDurably() // 没有 Trigger 关联的时候任务是否被保留。因为创建 JobDetail 时,还没 Trigger 指向它,所以需要设置为 true ,表示保留。
.build();
// 创建 Trigger
SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(5) // 频率。
.repeatForever(); // 次数。
Trigger trigger = TriggerBuilder.newTrigger()
.forJob(jobDetail) // 对应 Job 为 demoJob01
.withIdentity("demoJob01Trigger") // 名字为 demoJob01Trigger
.withSchedule(scheduleBuilder) // 对应 Schedule 为 scheduleBuilder
.build();
// 添加调度任务
scheduler.scheduleJob(jobDetail, trigger);
}

@Test
public void addDemoJob02Config() throws SchedulerException {
// 创建 JobDetail
JobDetail jobDetail = JobBuilder.newJob(DemoJob02.class)
.withIdentity("demoJob02") // 名字为 demoJob02
.storeDurably() // 没有 Trigger 关联的时候任务是否被保留。因为创建 JobDetail 时,还没 Trigger 指向它,所以需要设置为 true ,表示保留。
.build();
// 创建 Trigger
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/10 * * * * ? *");
Trigger trigger = TriggerBuilder.newTrigger()
.forJob(jobDetail) // 对应 Job 为 demoJob01
.withIdentity("demoJob02Trigger") // 名字为 demoJob01Trigger
.withSchedule(scheduleBuilder) // 对应 Schedule 为 scheduleBuilder
.build();
// 添加调度任务
scheduler.scheduleJob(jobDetail, trigger);
// scheduler.scheduleJob(jobDetail, Sets.newSet(trigger), true);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
@Configuration
public class ScheduleConfiguration {

@Configuration
public static class DemoJob01Configuration {

@Bean
Expand All @@ -36,7 +35,6 @@ public Trigger demoJob01Trigger() {

}

@Configuration
public static class DemoJob02Configuration {

@Bean
Expand Down
15 changes: 15 additions & 0 deletions lab-28/lab-28-task-xxl-job/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>lab-28</artifactId>
<groupId>cn.iocoder.springboot.labs</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>lab-28-task-xxl-job</artifactId>


</project>
1 change: 1 addition & 0 deletions lab-28/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<module>lab-28-task-demo</module>
<module>lab-28-task-quartz-memory</module>
<module>lab-28-task-quartz-jdbc</module>
<module>lab-28-task-xxl-job</module>
</modules>


Expand Down

0 comments on commit ab97d9c

Please sign in to comment.