Redisson延时任务发布

什么是延时任务

  • 简单来讲就是不知道开始时间是什么时候,开始时间是未知的没有固定的开始时间,用户的可以操作指定任意时刻时间。通常是由一个事件触发的在这个事件触发之后的一段时间内触发另一个事件,任务可以立即执行。定时任务是有固定周期有明确的触发时间,延时任务是特定时间内会触发。

应用场景有那些:

  1. 定时发布文章、邮件
  2. 用户订单下单之后20分钟后,如果用户没有付钱,则由系统自动取消订单;如果在20分钟内期间下单成功,任务取消。
  3. 接口三方接口出现网络问题,1分钟后重试,如果失败,2分钟重试,直到出现阈值在终止。

快速入门

1.创建Springboot工程添加依赖

java 复制代码
<dependencies>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
     <!--redisson核心依赖-->
    <dependency>
        <groupId>org.redisson</groupId>
        <artifactId>redisson-spring-boot-starter</artifactId>
        <version>3.16.3</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
</dependencies>

添加启动类:

java 复制代码
@SpringBootApplication
@EnableAsync //开启异步注解
public class TestApp {
    public static void main(String[] args) {
        SpringApplication.run(TestApp.class, args);
    }
}

添加配置文件

xml 复制代码
server:
  port: 8001
spring:
  application:
    name: redisson-test
  redis:
    host: 127.0.0.1
    redisson:
      config:
        clusterServersConfig:
          nodeAddresses:
          - "redis://127.0.0.1:6379"

消息发送

java 复制代码
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private RedissonClient redissonClient;

    @GetMapping("/{time}")
    public ResponseEntity send(@PathVariable("time") int time) {
        // 发送消息到redis
        // 需要使用redisson的客户端
        RBlockingQueue<String> blockingQueue = redissonClient.getBlockingQueue("test-dqueue");
        RDelayedQueue<String> delayedQueue = redissonClient.getDelayedQueue(blockingQueue);
        delayedQueue.offer("hello", time, TimeUnit.SECONDS);
        System.out.println("发送消息完成,延迟时间为: " + time +" 发送时间为: "+new Date());
        return ResponseEntity.ok("done");
    }
}

消息监听

java 复制代码
@Service
public class RedisListener {

    @Autowired
    private TestTasks testTasks;

    @PostConstruct  // 启动时自动执行该任务
    public void handleMessage() throws InterruptedException {
        testTasks.handleMessage();
    }
}

异步执行任务

java 复制代码
@Service
@Async
public class TestTasks {

    @Autowired
    private RedissonClient redissonClient;

    public void handleMessage() throws InterruptedException {
        RBlockingQueue<String> blockingQueue = redissonClient.getBlockingQueue("test-dqueue");
        // 发送一条初始化的消息
        RDelayedQueue<String> delayedQueue = redissonClient.getDelayedQueue(blockingQueue);
        while (true) {
            // 监听消息,如果没有监听到消息,会进入30秒的阻塞状态
            String result = blockingQueue.poll(30, TimeUnit.SECONDS);
            if (result != null && !result.equals("init")) {
                System.out.println("接收到消息: " + result + " " + new Date());
            }
        }
    }
}
相关推荐
ps酷教程4 小时前
Jackson 解决没有无参构造函数的反序列化问题
java
NiceCloud喜云4 小时前
Opus 4.8 的 Effort Control 怎么选:Low 到 Max 五档策略
android·java·大数据·前端·c++·python·spring
_日拱一卒5 小时前
LeetCode:994腐烂的橘子
java·数据结构·算法·leetcode·深度优先
隔窗听雨眠6 小时前
Nginx网关响应慢排查手记
java·服务器·nginx
智慧物业老杨6 小时前
智慧物业合同周期管理系统:从风险预警到智能交接的全流程数智化落地方案
java·人工智能·python
源码宝6 小时前
MES系统源码:Java8 + SpringBoot2.7 + MySQL8 + Redis,后端源码清爽易扩展
java·后端·源码·springboot·mes系统·源码二开·mes源码
JAVA社区7 小时前
Java高级全套教程(十)—— SpringCloudAlibaba超详细实战详解
java·开发语言·spring cloud·面试·职场和发展
金銀銅鐵7 小时前
[Java] 如何理解 class 文件中方法的 descriptor?
java·后端
云烟成雨TD7 小时前
Spring AI Alibaba 1.x 系列【63】AI Agent 长期记忆
java·人工智能·spring
憧憬成为java架构高手的小白7 小时前
苍穹外卖--day09
java·spring boot·百度