当前位置: 首页 > news >正文

redisson -- 延迟队列RDelayedQueue

1.maven配置

<!-- 用于管理起步工程的依赖管理 --><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.6.11</version><relativePath/> </parent>
<dependencies><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.2</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId><version>2.6.2</version></dependency><dependency><groupId>org.redisson</groupId><artifactId>redisson-spring-boot-starter</artifactId><version>3.10.6</version></dependency>
</dependencies>

2.yml配置

server:port: 9007servlet:context-path: /spring:redis:# 超时时间timeout: 10000ms# 服务器地址host: 127.0.0.1# 服务器端口port: 6379# 数据库database: 0# 密码password: 123456lettuce:pool:# 最大连接数,默认8max-active: 1024# 最大连接阻塞等待时间,默认-1max-wait: 10000ms# 最大空闲连接max-idle: 200# 最小空闲连接min-idle: 5

3.config

import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;public class RedissonConfig {@Bean(destroyMethod = "shutdown")public RedissonClient redissonClient(RedisProperties redisProperties){Config config = new Config();SingleServerConfig singleServerConfig = config.useSingleServer();singleServerConfig.setAddress("redis://" + redisProperties.getHost() + ":" + redisProperties.getPort());singleServerConfig.setPassword(redisProperties.getPassword());singleServerConfig.setKeepAlive(true);// 根据config创建出RedissonClient实例RedissonClient redissonClient = Redisson.create(config);return redissonClient;}}

4.service

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

5.结果

http://www.lryc.cn/news/340230.html

相关文章:

  • Flex弹性盒子布局案例(认识弹性布局)
  • 【ros2】ros1和ros2可以同时在一台机器上运行吗
  • PMSM MATLAB
  • 笔记本电脑上的聊天机器人: 在英特尔 Meteor Lake 上运行 Phi-2
  • 【Web】陇原战“疫“2021网络安全大赛 题解
  • 010Node.js自定义模块通过exports的使用,两种暴露的方法及区别(二)
  • 【CVE-2010-2883】进行钓鱼攻击的研究
  • 【Python】如何在Ubuntu上设置Python脚本开机自启
  • 计算机视觉——OpenCV Python基于颜色识别的目标检测
  • 2024中国内燃机展-北京汽车发动机零部件展
  • 【iOS】——SDWebImage源码学习
  • 树和二叉树(一)
  • RAID 磁盘阵列及RAID配置实战
  • listpack
  • Web3与社会契约:去中心化治理的新模式
  • 实体类List重复校验
  • loadash常用的函数方法
  • 【零基础入门TypeScript】模块
  • Scala 之数组
  • 【Phytium】飞腾D2000 UEFI/EDK2 适配 RTC(IIC SD3077)
  • 如何利用纯前端技术,实现一个网页版视频编辑器?
  • stm32实现hid键盘
  • 【单例模式】饿汉式、懒汉式、静态内部类--简单例子
  • windows关闭Windows Search功能
  • 政安晨:【深度学习神经网络基础】(九)—— 在深度学习神经网络反向传播训练中理解梯度
  • 免费的 ChatGPT、GPTs、AI绘画(国内版)
  • UniApp 微信小程序:在 onLaunch 中等待异步方法执行完成后,再调用页面中的接口
  • 【招贤纳士】长期有效
  • 华为配置静态ARP示例
  • LRTimelapse for Mac:专业延时摄影视频制作利器