延时性(过期/超时)和周期性的定时任务的实现方式

一、延时性的定时任务(例如订单超时30分钟后自动取消该订单)

任务在发出后的一定时间后进行逻辑处理,例如订单超时30分钟后自动取消该订单

1.使用DelayQueue实现任务即将到期提醒功能(非分布式)

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class TaskTimeoutReminderUtil {
    private static DelayQueue<TimeoutTask> delayQueue = new DelayQueue<>();

    static class TimeoutTask implements Delayed {

        private String taskId;
        private long expireTime;

        public TimeoutTask(String taskId, long timeoutMillis) {
            this.taskId = taskId;
            this.expireTime = System.currentTimeMillis() + timeoutMillis;
        }

        @Override
        public long getDelay(TimeUnit unit) {
            return unit.convert(expireTime - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
        }

        @Override
        public int compareTo(Delayed other) {
            if (other instanceof TimeoutTask) {
                TimeoutTask that = (TimeoutTask) other;
                return Long.compare(this.expireTime, that.expireTime);
            }
            return 0;
        }

        public String getTaskId() {
            return taskId;
        }
    }

    // 添加订单到延迟队列
    public static void addOrder(String taskId, long timeoutMillis) {
        delayQueue.offer(new TimeoutTask(taskId, timeoutMillis));
    }

    @PostConstruct
    public void init() {
        for (int i = 0; i < 10; i++) {
            TaskTimeoutReminderUtil.addOrder("task"+i, 1000*i);
        }
        // 启动一个单独的线程来处理超时任务
        new Thread(() -> {
            while (true) {
                try {
                    TimeoutTask task = delayQueue.take();
                    log.info("任务{}即将到期,请尽快处理", task.getTaskId());
                } catch (InterruptedException e) {
                    log.error("处理超时任务时发生异常");
                    e.printStackTrace();
                }
            }
        }).start();
    }
}

2.使用Redis实现任务即将到期提醒功能(分布式)

方式一:ZSet 实现方式

通过 ZSet 实现定时任务的思路是,将定时任务存放到 ZSet 集合中,并且将过期时间存储到 ZSet 的 Score 字段中,然后通过一个无线循环来判断当前时间内是否有需要执行的定时任务,如果有则进行执行,具体实现代码如下:

import redis.clients.jedis.Jedis;
import utils.JedisUtils;
import java.time.Instant;
import java.util.Set;
 
public class DelayQueueExample {
    // zset key
    private static final String _KEY = "myTaskQueue";
    
    public static void main(String[] args) throws InterruptedException {
        Jedis jedis = JedisUtils.getJedis();
        // 30s 后执行
        long delayTime = Instant.now().plusSeconds(30).getEpochSecond();
        jedis.zadd(_KEY, delayTime, "order_1");
        // 继续添加测试数据
        jedis.zadd(_KEY, Instant.now().plusSeconds(2).getEpochSecond(), "order_2");
        jedis.zadd(_KEY, Instant.now().plusSeconds(2).getEpochSecond(), "order_3");
        jedis.zadd(_KEY, Instant.now().plusSeconds(7).getEpochSecond(), "order_4");
        jedis.zadd(_KEY, Instant.now().plusSeconds(10).getEpochSecond(), "order_5");
        // 开启定时任务队列
        doDelayQueue(jedis);
    }
 
    /**
     * 定时任务队列消费
     * @param jedis Redis 客户端
     */
    public static void doDelayQueue(Jedis jedis) throws InterruptedException {
        while (true) {
            // 当前时间
            Instant nowInstant = Instant.now();
            long lastSecond = nowInstant.plusSeconds(-1).getEpochSecond(); // 上一秒时间
            long nowSecond = nowInstant.getEpochSecond();
            // 查询当前时间的所有任务
            Set<String> data = jedis.zrangeByScore(_KEY, lastSecond, nowSecond);
            for (String item : data) {
                // 消费任务
                System.out.println("消费:" + item);
            }
            // 删除已经执行的任务
            jedis.zremrangeByScore(_KEY, lastSecond, nowSecond);
            Thread.sleep(1000); // 每秒查询一次
        }
    }
}

方式二:键空间通知

我们可以通过 Redis 的键空间通知来实现定时任务,它的实现思路是给所有的定时任务设置一个过期时间,等到了过期之后,我们通过订阅过期消息就能感知到定时任务需要被执行了,此时我们执行定时任务即可。

默认情况下 Redis 是不开启键空间通知的,需要我们通过 config set notify-keyspace-events Ex 的命令手动开启(或在配置文件中修改),开启之后定时任务的代码如下:

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPubSub;
import utils.JedisUtils;
 
public class TaskExample {
    public static final String _TOPIC = "__keyevent@0__:expired"; // 订阅频道名称
    public static void main(String[] args) {
        Jedis jedis = JedisUtils.getJedis();
        // 执行定时任务
        doTask(jedis);
    }
 
    /**
     * 订阅过期消息,执行定时任务
     * @param jedis Redis 客户端
     */
    public static void doTask(Jedis jedis) {
        // 订阅过期消息
        jedis.psubscribe(new JedisPubSub() {
            @Override
            public void onPMessage(String pattern, String channel, String message) {
                // 接收到消息,执行定时任务
                System.out.println("收到消息:" + message);
            }
        }, _TOPIC);
    }
}

二、周期性的定时任务(例如每周五 23:59:59 例行执行巡视任务)

在某个时间点周期性执行任务,例如每周五 23:59:59 例行执行巡视任务
参考:https://blog.csdn.net/HackAzrael/article/details/122194645

1.开启定时任务

开启定时任务只需要在 Spring Boot 的启动类上声明 @EnableScheduling 即可,实现代码如下:

@SpringBootApplication
@EnableScheduling // 开启定时任务
public class DemoApplication {
    // do someing
}

2.添加定时任务

定时任务的添加只需要使用 @Scheduled 注解标注即可,如果有多个定时任务可以创建多个 @Scheduled 注解标注的方法,示例代码如下:

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
@Component // 把此类托管给 Spring,不能省略
public class TaskUtils {
    // 添加定时任务
    @Scheduled(cron = "59 59 23 0 0 5") // cron 表达式,每周五 23:59:59 执行
    public void doTask(){
        System.out.println("我是定时任务~");
    }
}

Cron 表达式介绍
Spring Task 的实现需要使用 cron 表达式来声明执行的频率和规则,cron 表达式是由 6 位或者 7 位组成的(最后一位可以省略),每位之间以空格分隔,每位从左到右代表的含义如下:
在这里插入图片描述
在这里插入图片描述
cron 表达式在线生成地址:https://cron.qqe2.com/

  • 7
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
延时队列和定时任务的区别在于,延时队列是按照元素添加的时间来进行排序,而定时任务是按照预定的时间来触发执行的。延时队列可以用于实现某些需要延迟处理的业务场景,例如延迟发布、延迟重试等场景,而定时任务则适用于需要周期性执行任务的场景。 下面是使用Java实现延时队列的示例代码: ``` import java.util.concurrent.DelayQueue; import java.util.concurrent.Delayed; import java.util.concurrent.TimeUnit; public class DelayQueueDemo { public static void main(String[] args) { DelayQueue<DelayedElement> delayQueue = new DelayQueue<>(); delayQueue.offer(new DelayedElement("A", 3000)); delayQueue.offer(new DelayedElement("B", 2000)); delayQueue.offer(new DelayedElement("C", 1000)); while (!delayQueue.isEmpty()) { try { DelayedElement element = delayQueue.take(); System.out.println(element.getData() + "被取出,时间为:" + System.currentTimeMillis()); } catch (InterruptedException e) { e.printStackTrace(); } } } } class DelayedElement implements Delayed { private String data; private long expireTime; public DelayedElement(String data, long delayTime) { this.data = data; this.expireTime = System.currentTimeMillis() + delayTime; } public String getData() { return data; } @Override public long getDelay(TimeUnit unit) { return unit.convert(expireTime - System.currentTimeMillis(), TimeUnit.MILLISECONDS); } @Override public int compareTo(Delayed o) { return Long.compare(this.getDelay(TimeUnit.MILLISECONDS), o.getDelay(TimeUnit.MILLISECONDS)); } } ``` 在这个示例中,我们定义了一个`DelayedElement`类,它实现了`Delayed`接口,并重写了`getDelay`和`compareTo`方法。`getDelay`方法返回该元素离过期时间还有多少时间,`compareTo`方法用于比较两个元素的顺序。 在`main`方法中,我们首先创建了一个`DelayQueue`对象,并向其中添加了三个元素,分别延时1秒、2秒和3秒。然后我们不断从队列中取出元素,直到队列为空。在取出元素的过程中,由于是按照元素到期时间来排序的,所以最先到期的元素会被最先取出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

funfan0517

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值