温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何实现Java中的延迟队列

发布时间:2022-02-23 10:20:33 来源:亿速云 阅读:213 作者:小新 栏目:开发技术

小编给大家分享一下如何实现Java中的延迟队列,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!

常见的实现方法主要有:定时任务扫描、RocketMQ延迟队列、Java自动的延迟队列、监听Redis Key过期等等

1.  DelayQueue

首先,定义一个延迟任务

package com.cjs.example; import lombok.Data; import java.util.concurrent.Delayed; import java.util.concurrent.TimeUnit; /**  * @author ChengJianSheng  * @since 2021/3/18  */ @Data public 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));  } }

然后,定义一个管理类

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 @Component public 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);  } }

插入任务

@Slf4j @Service public 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过期回调

修改redis.conf文件

# bind 127.0.0.1 -::1
protected-mode no
notify-keyspace-events Ex

如何实现Java中的延迟队列

[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  */ @Configuration public class RedisConfig {  @Bean  public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {   RedisMessageListenerContainer container = new RedisMessageListenerContainer();   container.setConnectionFactory(connectionFactory);   return container;  } }

创建一个监听类

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  */ @Component public 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("监听到Key: " + expiredKey + " 已过期");  } }

看完了这篇文章,相信你对“如何实现Java中的延迟队列”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI