forked from yudaocode/SpringBoot-Labs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
YunaiV
committed
Nov 28, 2019
1 parent
74dce3c
commit ab97d9c
Showing
11 changed files
with
120 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
...lab-28-task-quartz-jdbc/src/main/java/cn/iocoder/springboot/lab28/task/Application02.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 4 additions & 8 deletions
12
...lab-28-task-quartz-jdbc/src/main/java/cn/iocoder/springboot/lab28/task/job/DemoJob01.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
5 changes: 3 additions & 2 deletions
5
...lab-28-task-quartz-jdbc/src/main/java/cn/iocoder/springboot/lab28/task/job/DemoJob02.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...-task-quartz-jdbc/src/test/java/cn/iocoder/springboot/lab28/task/QuartzSchedulerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters