前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >quartz工具类含倒计时

quartz工具类含倒计时

作者头像
阿超
发布2023-12-19 10:24:34
1930
发布2023-12-19 10:24:34
举报
文章被收录于专栏:快乐阿超

一个人有无成就,决定于他青年时期是不是有志气。——谢觉哉

分享一个自己写的Quartz工具类,可以轻松实现倒计时功能:

代码语言:javascript
复制
import cn.hutool.core.date.DateUtil;
import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;
import org.dromara.streamquery.stream.core.collection.Lists;
import org.quartz.*;
import org.quartz.core.jmx.JobDataMapSupport;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.matchers.GroupMatcher;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.Set;

@UtilityClass
public class QuartzUtil {

    @SneakyThrows
    public static Scheduler startTaskAt(LocalDateTime time, JobKey jobKey,
                                        Map<String, Object> jobDataMap, Class<? extends Job> jobClazz) {
        var scheduler = StdSchedulerFactory.getDefaultScheduler();
        scheduler.deleteJob(jobKey);
        var jobDetail = JobBuilder.newJob().ofType(jobClazz).withIdentity(jobKey)
                .usingJobData(JobDataMapSupport.newJobDataMap(jobDataMap)).build();
        var trigger = TriggerBuilder.newTrigger().withIdentity(jobKey.getName(), jobKey.getGroup())
                .startAt(DateUtil.date(time)).build();
        scheduler.scheduleJob(jobDetail, trigger);
        scheduler.start();
        return scheduler;
    }

    public static Scheduler startTaskTimeSeconds(Long timeSeconds, JobKey jobKey,
                                                 Map<String, Object> jobDataMap, Class<? extends Job> jobClazz) {
        try {
            var scheduler = StdSchedulerFactory.getDefaultScheduler();
            scheduler.deleteJob(jobKey);
            var jobDetail = JobBuilder.newJob().ofType(jobClazz).withIdentity(jobKey)
                    .usingJobData(JobDataMapSupport.newJobDataMap(jobDataMap)).build();
            var trigger = TriggerBuilder.newTrigger()
                    .withIdentity(jobKey.getName(), jobKey.getGroup())
                    .withSchedule(SimpleScheduleBuilder.repeatSecondlyForTotalCount(timeSeconds.intValue()))
                    .startNow().build();
            scheduler.scheduleJob(jobDetail, trigger);
            scheduler.start();
            return scheduler;
        } catch (SchedulerException e) {
            throw new ApiServerException(e);
        }
    }


    public static void deleteJob(JobKey readyStartJobKey) {
        try {
            var scheduler = StdSchedulerFactory.getDefaultScheduler();
            scheduler.deleteJob(readyStartJobKey);
        } catch (SchedulerException e) {
            throw new ApiServerException(e);
        }
    }

    public static void deleteJobs(String groupName) {
        try {
            var scheduler = StdSchedulerFactory.getDefaultScheduler();
            Set<JobKey> jobKeys = scheduler.getJobKeys(GroupMatcher.groupEquals(groupName));
            scheduler.deleteJobs(Lists.ofColl(jobKeys));
        } catch (SchedulerException e) {
            throw new ApiServerException(e);
        }
    }

    public static void pauseJob(JobKey key) {
        try {
            var scheduler = StdSchedulerFactory.getDefaultScheduler();
            scheduler.pauseJob(key);
        } catch (SchedulerException e) {
            throw new ApiServerException(e);
        }
    }
}

对应的单元测试:

代码语言:javascript
复制
import lombok.SneakyThrows;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobKey;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

class QuartzUtilTest {

    @Test
    @SneakyThrows
    @Disabled("sleep")
    void startTaskAtTest() {
        var obj = new AtomicInteger();
        Map<String, Object> map = Map.of("test", obj);
        var jobKey = JobKey.jobKey("startTaskAtTest", "test");
        var time = LocalDateTime.now().plus(Duration.ofSeconds(3));
        QuartzUtil.startTaskAt(time, jobKey, map, StartTaskAtTestJob.class);
        TimeUnit.SECONDS.sleep(4);
        Assertions.assertEquals(1, obj.get());
    }

    @Test
    @SneakyThrows
    @Disabled("sleep")
    void startTaskTimeSecondsTest() {
        var obj = new AtomicInteger(3);
        Map<String, Object> map = Map.of("countdown", obj);
        var jobKey = JobKey.jobKey("startTaskTimeSecondsTest", "test");
        QuartzUtil.startTaskTimeSeconds(3L, jobKey, map, StartTaskTimeSecondsTestJob.class);
        TimeUnit.SECONDS.sleep(4);
        Assertions.assertEquals(0, obj.get());
    }

    public static class StartTaskAtTestJob implements Job {
        @Override
        public void execute(JobExecutionContext context) throws JobExecutionException {
            var jobDataMap = context.getMergedJobDataMap();
            var test = jobDataMap.get("test");
            if (test instanceof AtomicInteger obj) {
                obj.set(1);
            }
        }
    }

    public static class StartTaskTimeSecondsTestJob implements Job {
        @Override
        public void execute(JobExecutionContext context) throws JobExecutionException {
            var jobDataMap = context.getMergedJobDataMap();
            var countdown = jobDataMap.get("countdown");
            if (countdown instanceof AtomicInteger obj) {
                obj.set(obj.decrementAndGet());
            }
        }
    }

}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-12-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档