diff --git a/src/main/java/sast/evento/config/QuartzConfig.java b/src/main/java/sast/evento/config/QuartzConfig.java new file mode 100644 index 0000000..72f9900 --- /dev/null +++ b/src/main/java/sast/evento/config/QuartzConfig.java @@ -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()); + } + +} diff --git a/src/main/java/sast/evento/config/SchedulerConfig.java b/src/main/java/sast/evento/config/SchedulerConfig.java deleted file mode 100644 index 9470433..0000000 --- a/src/main/java/sast/evento/config/SchedulerConfig.java +++ /dev/null @@ -1,28 +0,0 @@ -package sast.evento.config; - -import lombok.extern.slf4j.Slf4j; -import org.quartz.SchedulerException; -import org.springframework.context.ApplicationListener; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.event.ContextRefreshedEvent; -import sast.evento.common.enums.ErrorEnum; -import sast.evento.exception.LocalRunTimeException; -import sast.evento.utils.SchedulerUtil; - -/** - * @Author: Love98 - * @Date: 9/9/2023 7:11 PM - */ -@Slf4j -@Configuration -public class SchedulerConfig implements ApplicationListener { - @Override - public void onApplicationEvent(ContextRefreshedEvent event) { - try { - log.info("Start Scheduler"); - SchedulerUtil.startScheduler(); - } catch (SchedulerException e) { - throw new LocalRunTimeException(ErrorEnum.SCHEDULER_ERROR, "error start"); - } - } -} diff --git a/src/main/java/sast/evento/service/impl/EventStateScheduleServiceImpl.java b/src/main/java/sast/evento/service/impl/EventStateScheduleServiceImpl.java index 0d80a6c..200383e 100644 --- a/src/main/java/sast/evento/service/impl/EventStateScheduleServiceImpl.java +++ b/src/main/java/sast/evento/service/impl/EventStateScheduleServiceImpl.java @@ -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; @@ -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; @@ -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"; @@ -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); } } @@ -54,13 +58,13 @@ 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); } } @@ -68,27 +72,23 @@ public void removeJob(Integer eventId, Integer state) { @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); } } diff --git a/src/main/java/sast/evento/service/impl/SubscribeMessageServiceImpl.java b/src/main/java/sast/evento/service/impl/SubscribeMessageServiceImpl.java index 3968587..7aea5af 100644 --- a/src/main/java/sast/evento/service/impl/SubscribeMessageServiceImpl.java +++ b/src/main/java/sast/evento/service/impl/SubscribeMessageServiceImpl.java @@ -1,6 +1,7 @@ package sast.evento.service.impl; +import jakarta.annotation.Resource; import lombok.Getter; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; @@ -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 @@ -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() { @@ -54,7 +59,7 @@ public void close() { /* 查看任务是否关闭 */ @SneakyThrows public Boolean isClose() { - return (!isOpen) || SchedulerUtil.isShutdown(); + return (!isOpen) || schedulerService.isShutdown(); } /* 添加定时读取并发送活动提醒任务 */ @@ -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); } /* 更新任务时间 */ @@ -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); } } @@ -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); } } diff --git a/src/main/java/sast/evento/utils/SchedulerUtil.java b/src/main/java/sast/evento/utils/SchedulerService.java similarity index 65% rename from src/main/java/sast/evento/utils/SchedulerUtil.java rename to src/main/java/sast/evento/utils/SchedulerService.java index 5baa6a5..c4bc364 100644 --- a/src/main/java/sast/evento/utils/SchedulerUtil.java +++ b/src/main/java/sast/evento/utils/SchedulerService.java @@ -1,11 +1,16 @@ package sast.evento.utils; +import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.quartz.*; +import org.quartz.impl.StdScheduler; import org.quartz.impl.StdSchedulerFactory; import org.quartz.impl.matchers.KeyMatcher; import org.quartz.impl.triggers.CronTriggerImpl; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.lang.Nullable; +import org.springframework.scheduling.quartz.SchedulerFactoryBean; +import org.springframework.stereotype.Component; import java.util.Date; @@ -15,17 +20,19 @@ * @date: 2023/7/26 22:35 */ @Slf4j -public class SchedulerUtil { +@Component +public class SchedulerService { public static final String simpleDateFormatPattern = "ss mm HH dd MM ? yyyy"; - private static final StdSchedulerFactory schedulerFactory = new StdSchedulerFactory(); + @Resource + private StdScheduler schedulerFactoryBean; - public static Scheduler getScheduler() throws SchedulerException { - return schedulerFactory.getScheduler(); + public Scheduler getScheduler() { + return this.schedulerFactoryBean; } - public static void addJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName, Class jobClass, @Nullable JobDataMap jobDataMap, String cron) throws SchedulerException { - Scheduler scheduler = getScheduler(); - if (scheduler.isShutdown()) { + public void addJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName, Class jobClass, @Nullable JobDataMap jobDataMap, String cron) throws SchedulerException { + Scheduler scheduler = this.getScheduler(); + if (!scheduler.isStarted()) { throw new RuntimeException("Please contact admin to start the scheduler first."); } JobDetail jobDetail = JobBuilder.newJob(jobClass) @@ -40,9 +47,9 @@ public static void addJob(String jobName, String jobGroupName, String triggerNam scheduler.scheduleJob(jobDetail, trigger); } - public static void addRepeatJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName, Class jobClass, @Nullable JobDataMap jobDataMap, String cron, Date start, Date end) throws SchedulerException { - Scheduler scheduler = getScheduler(); - if (scheduler.isShutdown()) { + public void addRepeatJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName, Class jobClass, @Nullable JobDataMap jobDataMap, String cron, Date start, Date end) throws SchedulerException { + Scheduler scheduler = this.getScheduler(); + if (!scheduler.isStarted()) { throw new RuntimeException("Please contact admin to start the scheduler first."); } JobDetail jobDetail = JobBuilder.newJob(jobClass) @@ -58,12 +65,13 @@ public static void addRepeatJob(String jobName, String jobGroupName, String trig scheduler.scheduleJob(jobDetail, trigger); } - public static void addJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName, Class jobClass, @Nullable JobDataMap jobDataMap, Date date) throws SchedulerException { - Scheduler scheduler = getScheduler(); - if (scheduler.isShutdown()) { + public void addJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName, Class jobClass, @Nullable JobDataMap jobDataMap, Date date) throws SchedulerException { + Scheduler scheduler = this.getScheduler(); + if (!scheduler.isStarted()) { throw new RuntimeException("Please contact admin to start the scheduler first."); } log.info(""" + jobName: {} jobGroup: {} triggerName: {} @@ -81,8 +89,8 @@ public static void addJob(String jobName, String jobGroupName, String triggerNam } - public static Boolean resetJobTrigger(String triggerName, String triggerGroupName, String cron) throws Exception { - Scheduler scheduler = getScheduler(); + public Boolean resetJobTrigger(String triggerName, String triggerGroupName, String cron) throws Exception { + Scheduler scheduler = this.getScheduler(); TriggerKey triggerKey = new TriggerKey(triggerName, triggerGroupName); CronTriggerImpl trigger = (CronTriggerImpl) scheduler.getTrigger(triggerKey); if (trigger == null) { @@ -97,8 +105,8 @@ public static Boolean resetJobTrigger(String triggerName, String triggerGroupNam return true; } - public static Boolean resetJobTrigger(String triggerName, String triggerGroupName, Date date) throws Exception { - Scheduler scheduler = getScheduler(); + public Boolean resetJobTrigger(String triggerName, String triggerGroupName, Date date) throws Exception { + Scheduler scheduler = this.getScheduler(); TriggerKey triggerKey = new TriggerKey(triggerName, triggerGroupName); SimpleTrigger trigger = (SimpleTrigger) scheduler.getTrigger(triggerKey); if (trigger == null) { @@ -116,8 +124,8 @@ public static Boolean resetJobTrigger(String triggerName, String triggerGroupNam return true; } - public static Boolean resetRepeatJob(String triggerName, String triggerGroupName, String cron, Date start, Date end) throws Exception { - Scheduler scheduler = getScheduler(); + public Boolean resetRepeatJob(String triggerName, String triggerGroupName, String cron, Date start, Date end) throws Exception { + Scheduler scheduler = this.getScheduler(); TriggerKey triggerKey = new TriggerKey(triggerName, triggerGroupName); CronTriggerImpl trigger = (CronTriggerImpl) scheduler.getTrigger(triggerKey); if (trigger == null) { @@ -138,36 +146,36 @@ public static Boolean resetRepeatJob(String triggerName, String triggerGroupName return true; } - public static void addJobListener(String jobName, String jobGroup, JobListener listener) throws SchedulerException { - Scheduler scheduler = getScheduler(); + public void addJobListener(String jobName, String jobGroup, JobListener listener) throws SchedulerException { + Scheduler scheduler = this.getScheduler(); JobKey jobKey = new JobKey(jobName, jobGroup); Matcher matcher = KeyMatcher.keyEquals(jobKey); scheduler.getListenerManager().addJobListener(listener, matcher); } - public static void removeJobListener(String name, String jobName, String jobGroup) throws SchedulerException { + public void removeJobListener(String name, String jobName, String jobGroup) throws SchedulerException { JobKey jobKey = new JobKey(jobName, jobGroup); Matcher matcher = KeyMatcher.keyEquals(jobKey); - Scheduler scheduler = getScheduler(); + Scheduler scheduler = this.getScheduler(); scheduler.getListenerManager().removeJobListenerMatcher(name, matcher); } - public static void addTriggerListener(String triggerName, String triggerGroup, TriggerListener listener) throws SchedulerException { - Scheduler scheduler = getScheduler(); + public void addTriggerListener(String triggerName, String triggerGroup, TriggerListener listener) throws SchedulerException { + Scheduler scheduler = this.getScheduler(); TriggerKey triggerKey = new TriggerKey(triggerName, triggerGroup); Matcher matcher = KeyMatcher.keyEquals(triggerKey); scheduler.getListenerManager().addTriggerListener(listener, matcher); } - public static void removeTriggerListener(String name, String triggerName, String triggerGroup) throws SchedulerException { + public void removeTriggerListener(String name, String triggerName, String triggerGroup) throws SchedulerException { TriggerKey triggerKey = new TriggerKey(triggerName, triggerGroup); Matcher matcher = KeyMatcher.keyEquals(triggerKey); - Scheduler scheduler = getScheduler(); + Scheduler scheduler = this.getScheduler(); scheduler.getListenerManager().removeTriggerListenerMatcher(name, matcher); } - public static void removeJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName) throws SchedulerException { - Scheduler scheduler = getScheduler(); + public void removeJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName) throws SchedulerException { + Scheduler scheduler = this.getScheduler(); if (!isJobExist(jobName, jobGroupName, triggerName, triggerGroupName)) { log.info("Job: {} is not exist.", jobName); return; @@ -178,13 +186,13 @@ public static void removeJob(String jobName, String jobGroupName, String trigger scheduler.deleteJob(new JobKey(jobName, jobGroupName)); } - public static Boolean isJobExist(String jobName, String jobGroupName, String triggerName, String triggerGroupName) throws SchedulerException { - Scheduler scheduler = getScheduler(); + public Boolean isJobExist(String jobName, String jobGroupName, String triggerName, String triggerGroupName) throws SchedulerException { + Scheduler scheduler = this.getScheduler(); return scheduler.checkExists(new JobKey(jobName, jobGroupName)) && scheduler.checkExists(new TriggerKey(triggerName, triggerGroupName)); } - public static void shutdownScheduler() throws SchedulerException { - Scheduler scheduler = getScheduler(); + public void shutdownScheduler() throws SchedulerException { + Scheduler scheduler = this.getScheduler(); if (!scheduler.isShutdown()) { scheduler.shutdown(); log.info("Scheduler shut down."); @@ -194,8 +202,8 @@ public static void shutdownScheduler() throws SchedulerException { } - public static void startScheduler() throws SchedulerException { - Scheduler scheduler = getScheduler(); + public void startScheduler() throws SchedulerException { + Scheduler scheduler = this.getScheduler(); if (!scheduler.isStarted()) { scheduler.start(); log.info("Scheduler start."); @@ -204,8 +212,8 @@ public static void startScheduler() throws SchedulerException { } } - public static Boolean isShutdown() throws SchedulerException { - return getScheduler().isShutdown(); + public Boolean isShutdown() throws SchedulerException { + return this.getScheduler().isShutdown(); } } \ No newline at end of file diff --git a/src/test/java/sast/evento/SastEventoBackendApplicationTests.java b/src/test/java/sast/evento/SastEventoBackendApplicationTests.java index 3f406be..10343e2 100644 --- a/src/test/java/sast/evento/SastEventoBackendApplicationTests.java +++ b/src/test/java/sast/evento/SastEventoBackendApplicationTests.java @@ -13,14 +13,16 @@ import sast.evento.service.LoginService; import sast.evento.utils.JsonUtil; import sast.evento.utils.RedisUtil; -import sast.evento.utils.SchedulerUtil; -import sast.sastlink.sdk.service.impl.RestTemplateSastLinkService; +import sast.evento.utils.SchedulerService; +import sast.sastlink.sdk.service.SastLinkService; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; +import static sast.evento.utils.SchedulerService.simpleDateFormatPattern; + @SpringBootTest class SastEventoBackendApplicationTests { @Resource @@ -28,10 +30,13 @@ class SastEventoBackendApplicationTests { @Resource private RedisUtil redisUtil; + @Resource + private SchedulerService schedulerService; + @Resource private LocationService locationService; @Resource - private RestTemplateSastLinkService sastLinkService; + private SastLinkService sastLinkService; @Resource private SubscribeDepartmentMapper subscribeDepartmentMapper; @@ -45,7 +50,7 @@ void eventSSTest() { String dateStr = "2023-09-05 22:25:59"; SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { - SchedulerUtil.startScheduler(); + schedulerService.startScheduler(); eventStateScheduleService.scheduleJob(47, dateFormat.parse(dateStr), 2); Thread.sleep(1000); eventStateScheduleService.scheduleJob(46, dateFormat.parse(dateStr), 3); @@ -60,7 +65,7 @@ void cronTest() { String dateStr = "2023-09-04 12:16:10"; SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { - System.out.println(SchedulerUtil.simpleDateFormat.format(dateFormat.parse(dateStr))); + System.out.println(new SimpleDateFormat(simpleDateFormatPattern).format(dateFormat.parse(dateStr))); } catch (ParseException e) { throw new RuntimeException(e); } @@ -68,15 +73,17 @@ void cronTest() { @Test void oneTimeJobTest() throws SchedulerException { - Date date = new Date(); - date.setTime(date.getTime() + 5000); - JobDataMap jobDataMapForUtil = new JobDataMap(); - jobDataMapForUtil.put("eventId", 44); - jobDataMapForUtil.put("state", 3); - SchedulerUtil.startScheduler(); - SchedulerUtil.addJob("44", "update_not_start_state_job_group", "44", "update_not_start_state_trigger_group", EventStateUpdateJob.class, jobDataMapForUtil, date); + for (int i = 2; i < 43; i++) { + Date date = new Date(); + date.setTime(date.getTime() + 5000000); + JobDataMap jobDataMapForUtil = new JobDataMap(); + jobDataMapForUtil.put("eventId", i); + jobDataMapForUtil.put("state", 3); + schedulerService.startScheduler(); + schedulerService.addJob(String.valueOf(i), "update_not_start_state_job_group", String.valueOf(i), "update_not_start_state_trigger_group", EventStateUpdateJob.class, jobDataMapForUtil, date); + } try { - Thread.sleep(7000); + Thread.sleep(70000); } catch (Exception e) { System.out.println(e.getMessage()); throw new RuntimeException(e); @@ -86,13 +93,11 @@ void oneTimeJobTest() throws SchedulerException { @Test void oneTimeJobDirectAddingTest() throws SchedulerException { Date date = new Date(); - date.setTime(date.getTime() + 5000); + date.setTime(date.getTime() + 50000); JobDataMap jobDataMapForDirect = new JobDataMap(); jobDataMapForDirect.put("eventId", 37); jobDataMapForDirect.put("state", 2); - Scheduler scheduler = new StdSchedulerFactory().getScheduler(); - scheduler.start(); -// Scheduler scheduler = SchedulerUtil.getScheduler(); +// Scheduler scheduler = schedulerService.getScheduler(); // scheduler.start(); JobDetail jobDetail = JobBuilder.newJob(EventStateUpdateJob.class) .withIdentity("44", "update_not_start_state_job_group") @@ -102,7 +107,6 @@ void oneTimeJobDirectAddingTest() throws SchedulerException { .withIdentity("44", "update_not_start_state_trigger_group") .startAt(date) .build(); - scheduler.scheduleJob(jobDetail, simpleTrigger); try { Thread.sleep(6000); } catch (InterruptedException e) { @@ -117,7 +121,7 @@ void RedisTest() { @Test void SastLinkTest() { - sastLinkService.login("", ""); + } @Test @@ -131,16 +135,16 @@ void combineTest() throws SchedulerException { JobDataMap jobDataMapForUtil = new JobDataMap(); jobDataMapForUtil.put("eventId", 56); jobDataMapForUtil.put("state", 3); -// SchedulerUtil.startScheduler(); - SchedulerUtil.addJob("44", "update_not_start_state_job_group", "44", "update_not_start_state_trigger_group", EventStateUpdateJob.class, jobDataMapForUtil, date); +// schedulerService.startScheduler(); + schedulerService.addJob("44", "update_not_start_state_job_group", "44", "update_not_start_state_trigger_group", EventStateUpdateJob.class, jobDataMapForUtil, date); JobDataMap jobDataMapForDirect = new JobDataMap(); jobDataMapForDirect.put("eventId", 57); jobDataMapForDirect.put("state", 2); - System.out.println("Util HashCode: " + SchedulerUtil.getScheduler().hashCode()); + System.out.println("Util HashCode: " + schedulerService.getScheduler().hashCode()); Scheduler scheduler = new StdSchedulerFactory().getScheduler(); System.out.println("Direct HashCode: " + scheduler.hashCode()); scheduler.start(); -// Scheduler scheduler = SchedulerUtil.getScheduler(); +// Scheduler scheduler = schedulerService.getScheduler(); // scheduler.start(); JobDetail jobDetail = JobBuilder.newJob(EventStateUpdateJob.class) .withIdentity("37", "update_not_start_state_job_group")