利用Redisson实现订单关闭(redis怎么关闭服务)

  本篇文章为你整理了利用Redisson实现订单关闭(redis怎么关闭服务)的详细内容,包含有redis 订单 redis怎么关闭服务 redis实现订单倒计时取消 redis防止订单重复提交 利用Redisson实现订单关闭,希望能帮助你了解 利用Redisson实现订单关闭。

  redisson_delay_queue:{ closeKey } :订单数据

  redisson_delay_queue_timeout:{ closeKey } :zset类型,按时间戳排序

  

 /**

 

   * 创建订单,并设置过期时间

   * @throws IOException

   @Test

   void createOrder() {

   RBlockingDeque OrderInfo blockingDeque = redissonClient.getBlockingDeque(closeKey);

   RDelayedQueue OrderInfo delayedQueue = redissonClient.getDelayedQueue(blockingDeque);

   // 100条订单

   int n = 100;

   Random random = new Random();

   for (int i = 0; i i++) {

   // 1~100之间的正整数

   int i1 = random.nextInt(100) + 1;

   LocalDateTime now = LocalDateTime.now();

   delayedQueue.offer(new OrderInfo(i + 1, "close: " + i1, now, now.plusSeconds(i1)), i1, TimeUnit.SECONDS);

  

 

  关闭订单,这里会产生订阅。redis会出现redisson_delay_queue_channel。

  

 /**

 

   * 关闭订单

   * @throws IOException

   @Test

   void closeOrder() {

   ReentrantLock lock = new ReentrantLock();

   // 5个线程

   int poolSize = 5;

   List CompletableFuture Void futureList = new ArrayList ();

   for (int i = 0; i poolSize; i++) {

   futureList.add(CompletableFuture.runAsync(() - {

   RBlockingDeque OrderInfo blockingDeque = redissonClient.getBlockingDeque(closeKey);

   // 加入监听

   redissonClient.getDelayedQueue(blockingDeque);

   while (true) {

   OrderInfo take;

   try {

   take = blockingDeque.take();

   } catch (Exception e) {

   continue;

   if (take == null) {

   continue;

   // 验证多次是否会重复关闭。正常里不会近,只是验证下。正式环境,可以删除

   try {

   lock.lock();

   if(closed.contains(take.getId())){

   log.info("测试是否会抢占:已存在其他线程处理关闭订单[{}]", take.getId());

   closed.add(take.getId());

   }finally {

   lock.unlock();

   // 处理订单关闭逻辑

   log.info("订单[{}]关闭中。。。", take.getId());

   log.info("订单[{}]已关闭!order={}", take.getId(), toJsonString(take));

   }));

   // 模拟正式环境中进程一直在运行,因为test时,没有join则会只执行一次出现消费完数据后进程就关闭了

   CompletableFuture.allOf(futureList.toArray(new CompletableFuture[0])).join();

  

 

  完整测试类:

  

package cn.skyjilygao.demo;

 

  import lombok.AllArgsConstructor;

  import lombok.Data;

  import lombok.NoArgsConstructor;

  import lombok.extern.slf4j.Slf4j;

  import org.junit.jupiter.api.Test;

  import org.redisson.api.RBlockingDeque;

  import org.redisson.api.RDelayedQueue;

  import org.redisson.api.RedissonClient;

  import org.springframework.beans.factory.annotation.Autowired;

  import org.springframework.boot.test.context.SpringBootTest;

  import java.io.IOException;

  import java.time.LocalDateTime;

  import java.util.ArrayList;

  import java.util.List;

  import java.util.Random;

  import java.util.Set;

  import java.util.concurrent.CompletableFuture;

  import java.util.concurrent.ConcurrentSkipListSet;

  import java.util.concurrent.TimeUnit;

  import java.util.concurrent.locks.ReentrantLock;

  import static cn.skyjilygao.util.EntityUtil.toJsonString;

  @Slf4j

  @SpringBootTest

  public class CloseOrderTests {

   @Autowired

   private RedissonClient redissonClient;

   public static String closeKey = "order_close_test";

   public volatile static Set Integer closed = new ConcurrentSkipListSet ();

   * 创建订单,并设置过期时间

   * @throws IOException

   @Test

   void createOrder() {

   RBlockingDeque OrderInfo blockingDeque = redissonClient.getBlockingDeque(closeKey);

   RDelayedQueue OrderInfo delayedQueue = redissonClient.getDelayedQueue(blockingDeque);

   int a = 100;

   Random random = new Random(100);

   for (int i = 0; i i++) {

   int i1 = random.nextInt(1 + i) + 1;

   delayedQueue.offer(new OrderInfo(i + 1, "close: " + i1, LocalDateTime.now(), LocalDateTime.now().plusSeconds(i1)), i1, TimeUnit.SECONDS);

   * 关闭订单

   * @throws IOException

   @Test

   void closeOrder() {

   ReentrantLock lock = new ReentrantLock();

   // 5个线程

   int poolSize = 5;

   List CompletableFuture Void futureList = new ArrayList ();

   for (int i = 0; i poolSize; i++) {

   futureList.add(CompletableFuture.runAsync(() - {

   RBlockingDeque OrderInfo blockingDeque = redissonClient.getBlockingDeque(closeKey);

   // 加入监听

   redissonClient.getDelayedQueue(blockingDeque);

   while (true) {

   OrderInfo take;

   try {

   take = blockingDeque.take();

   } catch (Exception e) {

   continue;

   if (take == null) {

   continue;

   try {

   lock.lock();

   if(closed.contains(take.getId())){

   log.info("测试是否会抢占:已存在其他线程处理关闭订单[{}]", take.getId());

   closed.add(take.getId());

   }finally {

   lock.unlock();

   log.info("订单[{}]关闭中。。。", take.getId());

   log.info("订单[{}]已关闭!order={}", take.getId(), toJsonString(take));

   }));

   // 模拟正式环境中进程一直在运行,因为test时,没有join则会只执行一次出现消费完数据后进程就关闭了

   CompletableFuture.allOf(futureList.toArray(new CompletableFuture[0])).join();

  以上就是利用Redisson实现订单关闭(redis怎么关闭服务)的详细内容,想要了解更多 利用Redisson实现订单关闭的内容,请持续关注盛行IT软件开发工作室。

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: