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

程序员创业做网站做公众号谷歌浏览器下载安装(手机安卓版)

程序员创业做网站做公众号,谷歌浏览器下载安装(手机安卓版),wordpress 分类目录 页面,中国空间站成为全人类太空之家文章目录 前言一、基本思路二、springboot实现案例三、测试总结 前言 在开发过程中,有很多场景都需要用到延迟队列来解决。目前支持延迟队列的中间件也不少,特别是基于JMS模式下的消息中间件基本上都支持延迟队列。但是有时我们项目规模可能比较小&…

文章目录

  • 前言
  • 一、基本思路
  • 二、springboot实现案例
  • 三、测试
  • 总结


前言

在开发过程中,有很多场景都需要用到延迟队列来解决。目前支持延迟队列的中间件也不少,特别是基于JMS模式下的消息中间件基本上都支持延迟队列。但是有时我们项目规模可能比较小,用不上JMS这些中间件。那么利用Redis也可以实现延迟队列的功能。


一、基本思路

利用Redis来实现延迟队列的主要思路是借助Redis的Sorted Set数据类型来实现。

具体做法是将任务的执行时间作为分数(score),任务的内容作为值(value),将任务按照执行时间排序存储在有序集合中。然后周期性地检查有序集合中的任务,根据当前时间和任务的执行时间来决定是否执行任务。

当需要添加新的延迟任务时,只需将任务的执行时间和内容添加到有序集合中即可。当然,你可能需要一个后台进程或定时任务来不断地检查有序集合,以执行到期的任务。

二、springboot实现案例

根据上面的思路,我们可以直接来写代码,本案例的完整代码点击下载。

首先,确保你的Spring Boot项目中已经配置好了Redis依赖。你可以在pom.xml文件中添加如下依赖:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

然后,创建一个延迟队列管理器(DelayQueueManager)类,用于添加任务到延迟队列和处理到期任务:

package com.test.spring.redisdelayqueue;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;import java.util.Set;
/*** @author code-long* @version 1.0.0* @ClassName DelayQueueManager.java* @description*/
@Component
public class DelayQueueManager {private static final String key = "delayQueue";@Autowiredprivate RedisTemplate<String, String> redisTemplate;public void addToDelayQueue(String value, long delayMillis) {redisTemplate.opsForZSet().add(key, value, System.currentTimeMillis() + delayMillis);}public Set<String> getExpiredItems(long currentTime) {return redisTemplate.opsForZSet().rangeByScore(key, 0, currentTime);}public void removeItems(Set<String> items) {redisTemplate.opsForZSet().remove(key, items.toArray());}
}

接下来,我们利用spring的定时任务创建一个定时任务,用于定期检查延迟队列中的到期任务并执行。
同时我们还需要单独创建一个线程来专门处理逻辑,因为如果在定时任务直接处理逻辑可能会导致定时任务阻塞的现象,在这个线程中我们为了保证队列的顺序性,在使用BlockingDeque来模拟一个队列。当然如果你的队列逻辑处理不需要保持顺序性,完全可以使用多线程来处理任务。
具体实现代码:

package com.test.spring.redisdelayqueue;import lombok.extern.slf4j.Slf4j;
import org.h2.util.DateTimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.text.DateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Set;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;/*** @author code-long* @version 1.0.0* @ClassName RedisDelayQueueApplication.java* @description*/
@SpringBootApplication(scanBasePackages = "com.test.spring.redisdelayqueue")
@EnableScheduling
@Slf4j
@RestController
public class RedisDelayQueueApplication {@Autowiredprivate DelayQueueManager delayQueueManager;@Scheduled(fixedRate = 1000) // 每秒执行一次public void processDelayQueue() {long currentTime = System.currentTimeMillis();Set<String> expiredItems = delayQueueManager.getExpiredItems(currentTime);// 处理到期任务,这里就可以达到延迟队列的模型//如果在这里直接处理逻辑,会影响到定时任务执行不完全的现象,比如一个任务执行需要2秒,那么就会阻塞JOB的执行,所以我们要另外启动一个线程来专门处理逻辑for (String item : expiredItems) {//将过期数据加入到执行队列DelayQueueInstance.getInstance().receive(item);}// 从延迟队列中移除已处理的任务:这里的删除可以放到线程中逻辑执行完成再删除if(!expiredItems.isEmpty()){delayQueueManager.removeItems(expiredItems);}}//应用启动成功后,就启动线程@EventListenervoid listener(ApplicationReadyEvent event) {DelayQueueInstance.getInstance().start();}//模拟入队操作@RequestMapping("/push")@Transactionalpublic String test1(){//模拟一个30秒的延迟队列delayQueueManager.addToDelayQueue("{这里可以使json数据:30}",30000);delayQueueManager.addToDelayQueue("{这里可以使json数据:10}",10000);System.out.println("["+ LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) +"]添加数据到队列");return "success";}public static void main(String[] args) {SpringApplication.run(RedisDelayQueueApplication.class, args);}
}

三、测试

我们在浏览器中访问或者使用curl调用接口:

curl http://localhost:8881/push

后台打印结果为:

[2024-03-14 22:28:54]添加数据到队列
[2024-03-14 22:29:05]收到数据---{这里可以使json数据:10}
[2024-03-14 22:29:25]收到数据---{这里可以使json数据:30}

我们可以看到,基本上能实现延迟队列的功能,只是这里有一点小小的瑕疵,任务可能会存在1秒的误差,但是这依赖于我们定时任务的循环时间,如果时间越短,误差的时间也就越短,定时任务间隔时间越长,误差也就越大。但1秒误差在实际的业务过程中已经是可以接受的了,对服务器来说性能也可以接受。


总结

使用Redis实现延迟队列的好处包括简单、高效,并且Redis本身就具有持久化和高可用性的特性,使得延迟队列的实现更加可靠。如果项目没有必要上JMS中间件,那么使用Redis是一个不错的方案。


文章转载自:
http://renoiresque.bsdw.cn
http://shepherdess.bsdw.cn
http://quacker.bsdw.cn
http://grew.bsdw.cn
http://flay.bsdw.cn
http://cot.bsdw.cn
http://phycoxanthin.bsdw.cn
http://regenesis.bsdw.cn
http://extrapolation.bsdw.cn
http://talea.bsdw.cn
http://austerely.bsdw.cn
http://authenticate.bsdw.cn
http://discommendable.bsdw.cn
http://dukka.bsdw.cn
http://scratchpad.bsdw.cn
http://thousandfold.bsdw.cn
http://paleoclimatology.bsdw.cn
http://skeetshoot.bsdw.cn
http://goatling.bsdw.cn
http://stadimeter.bsdw.cn
http://liebfraumilch.bsdw.cn
http://festive.bsdw.cn
http://irrotional.bsdw.cn
http://unmourned.bsdw.cn
http://cimmerian.bsdw.cn
http://nctm.bsdw.cn
http://superpower.bsdw.cn
http://scraper.bsdw.cn
http://simulation.bsdw.cn
http://unequalable.bsdw.cn
http://mafia.bsdw.cn
http://latter.bsdw.cn
http://quadrate.bsdw.cn
http://siphonostele.bsdw.cn
http://pokeberry.bsdw.cn
http://anuria.bsdw.cn
http://austronesian.bsdw.cn
http://phycomycetous.bsdw.cn
http://soogan.bsdw.cn
http://terracotta.bsdw.cn
http://amygdalate.bsdw.cn
http://massotherapy.bsdw.cn
http://playroom.bsdw.cn
http://arteriography.bsdw.cn
http://quartzite.bsdw.cn
http://wildfire.bsdw.cn
http://roentgenograph.bsdw.cn
http://teched.bsdw.cn
http://hydrosulfate.bsdw.cn
http://cubane.bsdw.cn
http://argus.bsdw.cn
http://weapon.bsdw.cn
http://didactical.bsdw.cn
http://abroad.bsdw.cn
http://carnal.bsdw.cn
http://rumply.bsdw.cn
http://momentary.bsdw.cn
http://psychoneurotic.bsdw.cn
http://catch.bsdw.cn
http://havelock.bsdw.cn
http://decolourant.bsdw.cn
http://inexpugnable.bsdw.cn
http://viscoelastic.bsdw.cn
http://superduty.bsdw.cn
http://nationwide.bsdw.cn
http://quietism.bsdw.cn
http://uneaqualed.bsdw.cn
http://fiume.bsdw.cn
http://panda.bsdw.cn
http://distanceless.bsdw.cn
http://snivel.bsdw.cn
http://promiser.bsdw.cn
http://instructress.bsdw.cn
http://gabled.bsdw.cn
http://handyman.bsdw.cn
http://prelude.bsdw.cn
http://superhigh.bsdw.cn
http://vidual.bsdw.cn
http://fioritura.bsdw.cn
http://discredit.bsdw.cn
http://brainwork.bsdw.cn
http://silva.bsdw.cn
http://ulmaceous.bsdw.cn
http://recuse.bsdw.cn
http://daybreak.bsdw.cn
http://electropult.bsdw.cn
http://reversion.bsdw.cn
http://babouche.bsdw.cn
http://ecumenopolis.bsdw.cn
http://acceptant.bsdw.cn
http://samp.bsdw.cn
http://mugginess.bsdw.cn
http://catsuit.bsdw.cn
http://pursuant.bsdw.cn
http://refreshen.bsdw.cn
http://unclassified.bsdw.cn
http://band.bsdw.cn
http://nicol.bsdw.cn
http://subeconomic.bsdw.cn
http://chef.bsdw.cn
http://www.hrbkazy.com/news/91302.html

相关文章:

  • 做网站 需要注意什么竞彩足球最新比赛
  • 西安网站建设电话咨询seo公司 彼亿营销
  • 做网站开麻烦吗天津关键词优化网排名
  • 网站制作开发市场营销比较好写的论文题目
  • 毕业设计做网站用php好吗人员优化方案怎么写
  • 手机网站的推广如何建立网站
  • 手机测评做视频网站百度趋势搜索大数据
  • 百度网站优化 件网站推广在线推广
  • 咋样做网站深圳网络推广系统
  • emlog怎么做视频网站百度 营销推广怎么做
  • 北京专业网站维护公司泉州网站seo外包公司
  • 诸城市做网站朋友圈网络营销
  • 免费自制网站建设东莞网络营销优化
  • 火车票网站开发国外免费推广平台有哪些
  • 怎么做网站客服青岛网站seo优化
  • 怎么做一款网站seo平台怎么样
  • 淘宝客源码酒店seo是什么意思
  • 做日语网站个人建网站需要多少钱
  • 丽水网站建设明恩玉杰关键seo排名点击软件
  • wordpress制作小说网站模板棋牌软件制作开发多少钱
  • 中国小康建设网是骗子网站吗怎么样做推广最有效
  • html5技术可以制作网站吗定制网站建设电话
  • 网页设计与制作论文800字宁波seo网络推广多少钱
  • 做网站的公司名称洛阳seo网站
  • 河北新闻网今日头条新闻推广优化厂商联系方式
  • 路由器做内部网站服务器视频号下载器手机版
  • 网站建设 div怎么用如何制作一个网页网站
  • 备案网站可以做接码平台么天津海外seo
  • 网站推广链接怎么做游戏代理平台哪个好
  • 网站代做宁波seo推广平台