Skip to content

Commit

Permalink
feat quartz supports persistence.
Browse files Browse the repository at this point in the history
  • Loading branch information
feellmoose committed Dec 21, 2023
1 parent 191c479 commit af09689
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 120 deletions.
60 changes: 60 additions & 0 deletions src/main/java/sast/evento/config/QuartzConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package sast.evento.config;

import com.zaxxer.hikari.HikariDataSource;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.autoconfigure.quartz.JobStoreType;
import org.springframework.boot.autoconfigure.quartz.QuartzProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import java.util.Properties;

@Configuration
public class QuartzConfig {

@Value("${spring.quartz.datasource.url}")
private String url;
@Value("${spring.quartz.datasource.username}")
private String username;
@Value("${spring.quartz.datasource.password}")
private String password;
@Value("${spring.quartz.datasource.driver-class-name}")
private String driverClassName;
@Value("${spring.quartz.datasource.name}")
private String name;

@Bean(name = "schedulerFactoryBean")
public SchedulerFactoryBean schedulerFactoryBean() {
SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
schedulerFactoryBean.setDataSource(quartzDataSource());
schedulerFactoryBean.setTransactionManager(quartzDataSourceTransactionManager());
schedulerFactoryBean.setAutoStartup(true);
schedulerFactoryBean.setSchedulerName("quartzScheduler");
schedulerFactoryBean.setSchedulerFactoryClass(StdSchedulerFactory.class);
schedulerFactoryBean.setOverwriteExistingJobs(true);
schedulerFactoryBean.setExposeSchedulerInRepository(true);
return schedulerFactoryBean;
}

private HikariDataSource quartzDataSource() {
DataSourceProperties dataSourceProperties = new DataSourceProperties();
dataSourceProperties.setUrl(url);
dataSourceProperties.setUsername(username);
dataSourceProperties.setPassword(password);
dataSourceProperties.setDriverClassName(driverClassName);
dataSourceProperties.setName(name);
return dataSourceProperties.initializeDataSourceBuilder()
.type(HikariDataSource.class)
.build();
}

private DataSourceTransactionManager quartzDataSourceTransactionManager() {
return new DataSourceTransactionManager(quartzDataSource());
}

}
28 changes: 0 additions & 28 deletions src/main/java/sast/evento/config/SchedulerConfig.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package sast.evento.service.impl;

import jakarta.annotation.Resource;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobDataMap;
Expand All @@ -8,7 +9,7 @@
import sast.evento.exception.LocalRunTimeException;
import sast.evento.job.EventStateUpdateJob;
import sast.evento.service.EventStateScheduleService;
import sast.evento.utils.SchedulerUtil;
import sast.evento.utils.SchedulerService;

import java.util.Date;

Expand All @@ -19,6 +20,9 @@
@Slf4j
@Service
public class EventStateScheduleServiceImpl implements EventStateScheduleService {

@Resource
private SchedulerService schedulerService;
private static final String notStartStateJobGroupName = "update_not_start_state_job_group";
private static final String checkingInStateJobGroupName = "update_checking_in_state_job_group";
private static final String inProgressStateJobGroupName = "update_in_process_state_job_group";
Expand All @@ -38,13 +42,13 @@ public void scheduleJob(Integer eventId, Date startTime, Integer state) {
jobDataMap.put("state", state);
switch (state) {
case 1 ->
SchedulerUtil.addJob(stringEventId, notStartStateJobGroupName, stringEventId, notStartStateTriggerGroupName, EventStateUpdateJob.class, jobDataMap, startTime);
schedulerService.addJob(stringEventId, notStartStateJobGroupName, stringEventId, notStartStateTriggerGroupName, EventStateUpdateJob.class, jobDataMap, startTime);
case 2 ->
SchedulerUtil.addJob(stringEventId, checkingInStateJobGroupName, stringEventId, checkingInStateTriggerGroupName, EventStateUpdateJob.class, jobDataMap, startTime);
schedulerService.addJob(stringEventId, checkingInStateJobGroupName, stringEventId, checkingInStateTriggerGroupName, EventStateUpdateJob.class, jobDataMap, startTime);
case 3 ->
SchedulerUtil.addJob(stringEventId, inProgressStateJobGroupName, stringEventId, inProgressStateTriggerGroupName, EventStateUpdateJob.class, jobDataMap, startTime);
schedulerService.addJob(stringEventId, inProgressStateJobGroupName, stringEventId, inProgressStateTriggerGroupName, EventStateUpdateJob.class, jobDataMap, startTime);
case 5 ->
SchedulerUtil.addJob(stringEventId, endedStateJobGroupName, stringEventId, endedStateTriggerGroupName, EventStateUpdateJob.class, jobDataMap, startTime);
schedulerService.addJob(stringEventId, endedStateJobGroupName, stringEventId, endedStateTriggerGroupName, EventStateUpdateJob.class, jobDataMap, startTime);
default -> throw new LocalRunTimeException(ErrorEnum.SCHEDULER_ERROR);
}
}
Expand All @@ -54,41 +58,37 @@ public void removeJob(Integer eventId, Integer state) {
String stringEventId = String.valueOf(eventId);
switch (state) {
case 1 ->
SchedulerUtil.removeJob(stringEventId, notStartStateJobGroupName, stringEventId, notStartStateTriggerGroupName);
schedulerService.removeJob(stringEventId, notStartStateJobGroupName, stringEventId, notStartStateTriggerGroupName);
case 2 ->
SchedulerUtil.removeJob(stringEventId, checkingInStateJobGroupName, stringEventId, checkingInStateTriggerGroupName);
schedulerService.removeJob(stringEventId, checkingInStateJobGroupName, stringEventId, checkingInStateTriggerGroupName);
case 3 ->
SchedulerUtil.removeJob(stringEventId, inProgressStateJobGroupName, stringEventId, inProgressStateTriggerGroupName);
schedulerService.removeJob(stringEventId, inProgressStateJobGroupName, stringEventId, inProgressStateTriggerGroupName);
case 5 ->
SchedulerUtil.removeJob(stringEventId, endedStateJobGroupName, stringEventId, endedStateTriggerGroupName);
schedulerService.removeJob(stringEventId, endedStateJobGroupName, stringEventId, endedStateTriggerGroupName);
default -> throw new LocalRunTimeException(ErrorEnum.SCHEDULER_ERROR);
}
}

@SneakyThrows
public void removeJobs(Integer eventId) {
String stringEventId = String.valueOf(eventId);
SchedulerUtil.removeJob(stringEventId, notStartStateJobGroupName, stringEventId, notStartStateTriggerGroupName);
SchedulerUtil.removeJob(stringEventId, checkingInStateJobGroupName, stringEventId, checkingInStateTriggerGroupName);
SchedulerUtil.removeJob(stringEventId, inProgressStateJobGroupName, stringEventId, inProgressStateTriggerGroupName);
SchedulerUtil.removeJob(stringEventId, endedStateJobGroupName, stringEventId, endedStateTriggerGroupName);
schedulerService.removeJob(stringEventId, notStartStateJobGroupName, stringEventId, notStartStateTriggerGroupName);
schedulerService.removeJob(stringEventId, checkingInStateJobGroupName, stringEventId, checkingInStateTriggerGroupName);
schedulerService.removeJob(stringEventId, inProgressStateJobGroupName, stringEventId, inProgressStateTriggerGroupName);
schedulerService.removeJob(stringEventId, endedStateJobGroupName, stringEventId, endedStateTriggerGroupName);
}

@SneakyThrows
public void updateJob(Integer eventId, Date startTime, Integer state) {
String stringEventId = String.valueOf(eventId);
if(!switch (state) {
case 1 ->
SchedulerUtil.resetJobTrigger(stringEventId, notStartStateTriggerGroupName, startTime);
case 2 ->
SchedulerUtil.resetJobTrigger(stringEventId, checkingInStateTriggerGroupName, startTime);
case 3 ->
SchedulerUtil.resetJobTrigger(stringEventId, inProgressStateTriggerGroupName, startTime);
case 5 ->
SchedulerUtil.resetJobTrigger(stringEventId, endedStateTriggerGroupName, startTime);
if (!switch (state) {
case 1 -> schedulerService.resetJobTrigger(stringEventId, notStartStateTriggerGroupName, startTime);
case 2 -> schedulerService.resetJobTrigger(stringEventId, checkingInStateTriggerGroupName, startTime);
case 3 -> schedulerService.resetJobTrigger(stringEventId, inProgressStateTriggerGroupName, startTime);
case 5 -> schedulerService.resetJobTrigger(stringEventId, endedStateTriggerGroupName, startTime);
default -> throw new LocalRunTimeException(ErrorEnum.SCHEDULER_ERROR);
}){
scheduleJob(eventId,startTime,state);
}) {
scheduleJob(eventId, startTime, state);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sast.evento.service.impl;


import jakarta.annotation.Resource;
import lombok.Getter;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -10,11 +11,13 @@
import sast.evento.exception.LocalRunTimeException;
import sast.evento.job.WxSubscribeJob;
import sast.evento.service.SubscribeMessageService;
import sast.evento.utils.SchedulerUtil;
import sast.evento.utils.SchedulerService;

import java.text.SimpleDateFormat;
import java.util.Date;

import static sast.evento.utils.SchedulerService.simpleDateFormatPattern;

/**
* @projectName: Test
* @author: feelMoose
Expand All @@ -40,6 +43,8 @@ public class SubscribeMessageServiceImpl implements SubscribeMessageService {
private static final String triggerGroupName = "trigger_wx_subscribe";
@Getter
private static Boolean isOpen = true;
@Resource
private SchedulerService schedulerService;

/* 开启任务 */
public void open() {
Expand All @@ -54,7 +59,7 @@ public void close() {
/* 查看任务是否关闭 */
@SneakyThrows
public Boolean isClose() {
return (!isOpen) || SchedulerUtil.isShutdown();
return (!isOpen) || schedulerService.isShutdown();
}

/* 添加定时读取并发送活动提醒任务 */
Expand All @@ -63,11 +68,11 @@ public void addWxSubScribeJob(Integer eventId, Date startTime) {
if (isClose()) {
throw new LocalRunTimeException(ErrorEnum.WX_SUBSCRIBE_ERROR, "Wx subscribe message service is close");
}
String cron = new SimpleDateFormat(SchedulerUtil.simpleDateFormatPattern).format(startTime);
String cron = new SimpleDateFormat(simpleDateFormatPattern).format(startTime);
JobDataMap jobDataMap = new JobDataMap();
jobDataMap.put("eventId", eventId);
String stringEventId = String.valueOf(eventId);
SchedulerUtil.addJob(stringEventId, jobGroupName, stringEventId, triggerGroupName, WxSubscribeJob.class, jobDataMap, cron);
schedulerService.addJob(stringEventId, jobGroupName, stringEventId, triggerGroupName, WxSubscribeJob.class, jobDataMap, cron);
}

/* 更新任务时间 */
Expand All @@ -76,9 +81,9 @@ public void updateWxSubScribeJob(Integer eventId, Date startTime) {
if (isClose()) {
throw new LocalRunTimeException(ErrorEnum.WX_SUBSCRIBE_ERROR, "Wx subscribe message service is close");
}
String cron = new SimpleDateFormat(SchedulerUtil.simpleDateFormatPattern).format(startTime);
if(!SchedulerUtil.resetJobTrigger(String.valueOf(eventId), triggerGroupName, cron)){
addWxSubScribeJob(eventId,startTime);
String cron = new SimpleDateFormat(simpleDateFormatPattern).format(startTime);
if (!schedulerService.resetJobTrigger(String.valueOf(eventId), triggerGroupName, cron)) {
addWxSubScribeJob(eventId, startTime);
}
}

Expand All @@ -89,7 +94,7 @@ public void removeWxSubScribeJob(Integer eventId) {
throw new LocalRunTimeException(ErrorEnum.WX_SUBSCRIBE_ERROR, "Wx subscribe message service is close.");
}
String stringEventId = String.valueOf(eventId);
SchedulerUtil.removeJob(stringEventId, jobGroupName, stringEventId, triggerGroupName);
schedulerService.removeJob(stringEventId, jobGroupName, stringEventId, triggerGroupName);
}

}
Loading

0 comments on commit af09689

Please sign in to comment.