当前位置:   article > 正文

redisson -- 延迟队列RDelayedQueue_reddisson实现延时队列

reddisson实现延时队列

1.maven配置

  1. <!-- 用于管理起步工程的依赖管理 -->
  2. <parent>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-parent</artifactId>
  5. <version>2.6.11</version>
  6. <relativePath/>
  7. </parent>
  8. <dependencies>
  9. <dependency>
  10. <groupId>cn.hutool</groupId>
  11. <artifactId>hutool-all</artifactId>
  12. <version>5.8.2</version>
  13. </dependency>
  14. <dependency>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-starter-web</artifactId>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.springframework.boot</groupId>
  20. <artifactId>spring-boot-starter-data-redis</artifactId>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.apache.commons</groupId>
  24. <artifactId>commons-pool2</artifactId>
  25. <version>2.6.2</version>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.redisson</groupId>
  29. <artifactId>redisson-spring-boot-starter</artifactId>
  30. <version>3.10.6</version>
  31. </dependency>
  32. </dependencies>

2.yml配置

  1. server:
  2. port: 9007
  3. servlet:
  4. context-path: /
  5. spring:
  6. redis:
  7. # 超时时间
  8. timeout: 10000ms
  9. # 服务器地址
  10. host: 127.0.0.1
  11. # 服务器端口
  12. port: 6379
  13. # 数据库
  14. database: 0
  15. # 密码
  16. password: 123456
  17. lettuce:
  18. pool:
  19. # 最大连接数,默认8
  20. max-active: 1024
  21. # 最大连接阻塞等待时间,默认-1
  22. max-wait: 10000ms
  23. # 最大空闲连接
  24. max-idle: 200
  25. # 最小空闲连接
  26. min-idle: 5

3.config

  1. import org.redisson.api.RedissonClient;
  2. import org.redisson.config.Config;
  3. import org.redisson.config.SingleServerConfig;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
  6. public class RedissonConfig {
  7. @Bean(destroyMethod = "shutdown")
  8. public RedissonClient redissonClient(RedisProperties redisProperties){
  9. Config config = new Config();
  10. SingleServerConfig singleServerConfig = config.useSingleServer();
  11. singleServerConfig.setAddress("redis://" + redisProperties.getHost() + ":" + redisProperties.getPort());
  12. singleServerConfig.setPassword(redisProperties.getPassword());
  13. singleServerConfig.setKeepAlive(true);
  14. // 根据config创建出RedissonClient实例
  15. RedissonClient redissonClient = Redisson.create(config);
  16. return redissonClient;
  17. }
  18. }

4.service

  1. public void delayed() throws InterruptedException {
  2. // 阻塞队列:RBlockingDeque的实现类为:new RedissonBlockingDeque
  3. RBlockingDeque<String> blockingDeque = redissonClient.getBlockingDeque("delayedQueue");
  4. // 获取延迟队列
  5. RDelayedQueue<String> delayedQueue = redissonClient.getDelayedQueue(blockingDeque);
  6. System.out.println(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + "添加任务到延时队列里面");
  7. // 添加延迟任务
  8. delayedQueue.offer( "task1第一个任务", 6, TimeUnit.SECONDS);
  9. delayedQueue.offer( "task2第二个任务", 4, TimeUnit.SECONDS);
  10. System.out.println("--------------------Begin");
  11. // 通过 take 方法等待并获取到期的任务
  12. while (true) {
  13. String task = blockingDeque.take();
  14. System.out.println(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + "延时队列收到:" + task);
  15. }
  16. }

5.结果

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/626751
推荐阅读
相关标签
  

闽ICP备14008679号