久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Java 延遲隊(duì)列的常用的實(shí)現(xiàn)方式

瀏覽:2日期:2022-08-14 18:36:00

延遲隊(duì)列的使用場(chǎng)景還比較多,例如:

1、超時(shí)未收到支付回調(diào),主動(dòng)查詢支付狀態(tài);

2、規(guī)定時(shí)間內(nèi),訂單未支付,自動(dòng)取消;

。。。

總之,但凡需要在未來的某個(gè)確定的時(shí)間點(diǎn)執(zhí)行檢查的場(chǎng)景中都可以用延遲隊(duì)列。

常見的手段主要有:定時(shí)任務(wù)掃描、RocketMQ延遲隊(duì)列、Java自動(dòng)的延遲隊(duì)列、監(jiān)聽Redis Key過期等等

1. DelayQueue

首先,定義一個(gè)延遲任務(wù)

package com.cjs.example;import lombok.Data;import java.util.concurrent.Delayed;import java.util.concurrent.TimeUnit;/** * @author ChengJianSheng * @since 2021/3/18 */@Datapublic class DelayTask implements Delayed { private Long orderId; private long expireTime; public DelayTask(Long orderId, long expireTime) { this.orderId = orderId; this.expireTime = expireTime; } @Override public long getDelay(TimeUnit unit) { return expireTime - System.currentTimeMillis(); } @Override public int compareTo(Delayed o) { return (int) (getDelay(TimeUnit.MILLISECONDS) - o.getDelay(TimeUnit.MILLISECONDS)); }}

然后,定義一個(gè)管理類

package com.cjs.example;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.CommandLineRunner;import org.springframework.stereotype.Component;import java.util.concurrent.DelayQueue;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;/** * @author ChengJianSheng * @since 2021/3/19 */@Slf4j@Componentpublic class DelayQueueManager implements CommandLineRunner { private DelayQueue<DelayTask> queue = new DelayQueue<>(); @Autowired private ParkOrderQueryHandler handler; @Override public void run(String... strings) throws Exception { ExecutorService executorService = Executors.newSingleThreadExecutor(); executorService.execute(new Runnable() { @Override public void run() { while (true) { try { DelayTask task = queue.take(); handler.handle(task); } catch (InterruptedException e) { e.printStackTrace(); } } } }); } public void put(DelayTask task) { queue.put(task); }}

插入任務(wù)

@Slf4j@Servicepublic class PayServiceImpl implements PayService { @Autowired private DelayQueueManager delayQueueManager; @Override public void pay() { delayQueueManager.put(new DelayTask(1, 15)); delayQueueManager.put(new DelayTask(2, 30)); delayQueueManager.put(new DelayTask(3, 60)); }}2. Redis Key過期回調(diào)

修改redis.conf文件

# bind 127.0.0.1 -::1protected-mode nonotify-keyspace-events Ex

Java 延遲隊(duì)列的常用的實(shí)現(xiàn)方式

[root@localhost redis-6.2.1]$ src/redis-server redis.conf <?xml version='1.0' encoding='UTF-8'?><project xmlns='http://maven.apache.org/POM/4.0.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd'> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.4</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo0401</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo0401</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>

RedisConfig.java

package com.example.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.listener.RedisMessageListenerContainer;/** * @author ChengJianSheng * @since 2021/4/2 */@Configurationpublic class RedisConfig { @Bean public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) { RedisMessageListenerContainer container = new RedisMessageListenerContainer(); container.setConnectionFactory(connectionFactory); return container; }}

創(chuàng)建一個(gè)監(jiān)聽類

package com.example.listener;import org.springframework.data.redis.connection.Message;import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;import org.springframework.data.redis.listener.RedisMessageListenerContainer;import org.springframework.stereotype.Component;/** * @author ChengJianSheng * @since 2021/4/2 */@Componentpublic class MyRedisKeyExpirationListener extends KeyExpirationEventMessageListener { public MyRedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) { super(listenerContainer); } @Override public void onMessage(Message message, byte[] pattern) { String expiredKey = message.toString(); System.out.println('監(jiān)聽到Key: ' + expiredKey + ' 已過期'); }}3. RocketMQ

Java 延遲隊(duì)列的常用的實(shí)現(xiàn)方式

官方文檔:https://help.aliyun.com/document_detail/29549.htm

以上就是Java 延遲隊(duì)列的常用的實(shí)現(xiàn)方式的詳細(xì)內(nèi)容,更多關(guān)于Java 延遲隊(duì)列實(shí)現(xiàn)方式的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 99精品国产高清在线观看 | 国精品产品区三区 | 亚洲一二三区电影 | 97人人做人人人难人人做 | 日日夜夜天天干干 | 精品国产乱码久久久久久影片 | 91影库| 成人看片免费网站 | 国产精品久久久久久久久久久免费看 | avhd101在线成人播放 | 亚洲精品一区二区三区精华液 | 日日摸夜夜添夜夜添高潮视频 | 色爱区综合| 久久精品播放 | 中文字幕av亚洲精品一部二部 | 精品在线播放 | 手机看片169 | 国产成人福利在线 | 青青草视频在线免费观看 | 亚洲视频久久久 | 成人一区av | 日本 欧美 三级 高清 视频 | 日韩av一区二区三区在线观看 | 久久人人爽人人爽人人片av软件 | 日韩视频在线播放 | 亚洲精品色 | 夜夜爽99久久国产综合精品女不卡 | 欧美成人精品在线观看 | 亚洲一区视频 | 国产欧美日韩精品一区 | 99热播在线| 国产在线精品一区 | 国产精品黄网站在线观看 | 亚洲精品一 | 中文字幕一区二区三区不卡 | 国产一区二区三区四区在线观看 | 中文字幕在线亚洲 | 欧美一级日韩片 | 在线99热| 久久久99精品免费观看 | 久久网一区二区三区 |