10 changed files with 138 additions and 2 deletions
@ -0,0 +1,36 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||
|
xmlns="http://maven.apache.org/POM/4.0.0" |
||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
||||
|
<parent> |
||||
|
<groupId>org.dromara</groupId> |
||||
|
<artifactId>ruoyi-common</artifactId> |
||||
|
<version>${revision}</version> |
||||
|
</parent> |
||||
|
<modelVersion>4.0.0</modelVersion> |
||||
|
|
||||
|
<artifactId>ruoyi-common-quartz</artifactId> |
||||
|
|
||||
|
<description> |
||||
|
ruoyi-common-quartz Quartz定时任务 |
||||
|
</description> |
||||
|
|
||||
|
<dependencies> |
||||
|
|
||||
|
<dependency> |
||||
|
<groupId>org.springframework.boot</groupId> |
||||
|
<artifactId>spring-boot-starter-quartz</artifactId> |
||||
|
</dependency> |
||||
|
|
||||
|
<dependency> |
||||
|
<groupId>org.projectlombok</groupId> |
||||
|
<artifactId>lombok</artifactId> |
||||
|
</dependency> |
||||
|
|
||||
|
<dependency> |
||||
|
<groupId>org.dromara</groupId> |
||||
|
<artifactId>ruoyi-common-core</artifactId> |
||||
|
</dependency> |
||||
|
|
||||
|
</dependencies> |
||||
|
</project> |
||||
@ -0,0 +1,49 @@ |
|||||
|
package org.dromara.common.quartz.config; |
||||
|
|
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.dromara.common.quartz.job.DemoTimeJob; |
||||
|
import org.quartz.*; |
||||
|
import org.springframework.boot.autoconfigure.AutoConfiguration; |
||||
|
import org.springframework.context.annotation.Bean; |
||||
|
import org.springframework.context.annotation.ComponentScan; |
||||
|
|
||||
|
/** |
||||
|
* Quartz 自动配置 |
||||
|
* |
||||
|
* @author ruoyi |
||||
|
*/ |
||||
|
@Slf4j |
||||
|
@AutoConfiguration |
||||
|
@ComponentScan("org.dromara.common.quartz") |
||||
|
public class QuartzAutoConfiguration { |
||||
|
|
||||
|
public QuartzAutoConfiguration() { |
||||
|
log.info("初始化 Quartz 定时任务配置"); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Demo任务详情 |
||||
|
*/ |
||||
|
@Bean |
||||
|
public JobDetail demoTimeJobDetail() { |
||||
|
return JobBuilder.newJob(DemoTimeJob.class) |
||||
|
.withIdentity("demoTimeJob", "demo") |
||||
|
.storeDurably() |
||||
|
.build(); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Demo任务触发器 - 每秒执行一次 |
||||
|
*/ |
||||
|
@Bean |
||||
|
public Trigger demoTimeJobTrigger(JobDetail demoTimeJobDetail) { |
||||
|
return TriggerBuilder.newTrigger() |
||||
|
.forJob(demoTimeJobDetail) |
||||
|
.withIdentity("demoTimeTrigger", "demo") |
||||
|
.withSchedule(SimpleScheduleBuilder.simpleSchedule() |
||||
|
.withIntervalInSeconds(1) |
||||
|
.repeatForever()) |
||||
|
.build(); |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,25 @@ |
|||||
|
package org.dromara.common.quartz.job; |
||||
|
|
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.quartz.JobExecutionContext; |
||||
|
import org.springframework.scheduling.quartz.QuartzJobBean; |
||||
|
|
||||
|
import java.time.LocalDateTime; |
||||
|
import java.time.format.DateTimeFormatter; |
||||
|
|
||||
|
/** |
||||
|
* 演示定时任务 - 每秒打印当前时间 |
||||
|
* |
||||
|
* @author ruoyi |
||||
|
*/ |
||||
|
@Slf4j |
||||
|
public class DemoTimeJob extends QuartzJobBean { |
||||
|
|
||||
|
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
||||
|
|
||||
|
@Override |
||||
|
protected void executeInternal(JobExecutionContext context) { |
||||
|
log.info("[Quartz Demo] 当前时间: {}", LocalDateTime.now().format(FORMATTER)); |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1 @@ |
|||||
|
org.dromara.common.quartz.config.QuartzAutoConfiguration |
||||
Loading…
Reference in new issue