yujian
2019-12-10 c8041ec0544bf122e6819e6bf698997ccbf30aaf
fanli/src/main/java/com/yeshi/fanli/aspect/RequestSerializableAspect.java
@@ -30,6 +30,7 @@
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.params.SetParams;
@Component
@Aspect
@@ -96,33 +97,32 @@
                  cacheKey = "rs-" + StringUtil.Md5(cacheKey);
                  // jiedis原子性做拦截
                  Jedis jedis = jedisPool.getResource();
                  long threadId= Thread.currentThread().getId();
                  try {
                     Constant.waitingThreadSet.add(Thread.currentThread().getId());
                     long result = 0;
                     Constant.waitingThreadSet.add(threadId);
                     String result = null;
                     long startTime = System.currentTimeMillis();
                     // 等待响应
                     while (result <= 0) {
                        result = jedis.setnx(cacheKey, "1");
                        LogHelper.error("触发并发锁:" + cacheAlias);
                        LogHelper.error("redis键:" + cacheKey);
                        if (result <= 0) {
                     while (StringUtil.isNullOrEmpty(result)) {
                        result = jedis.set(cacheKey, "1", new SetParams().nx().ex(30));
                        if (StringUtil.isNullOrEmpty(result)) {
                           try {
                              Thread.sleep(50);
                           } catch (InterruptedException e) {
                              e.printStackTrace();
                           }
                           if (System.currentTimeMillis() - startTime > 1000 * 60L) {
                           if (System.currentTimeMillis() - startTime > 1000 * 30L) {
                              Constant.waitingThreadSet.remove(Thread.currentThread().getId());
                              out.print(JsonUtil.loadFalseResult("连接超时"));
                              return null;
                           }
                        } else {
                           // 设置30秒处理时间
                           jedis.expire(cacheKey, 30);
                           break;
                        }
                     }
                     if (result > 0) {
                     if (!StringUtil.isNullOrEmpty(result)) {
                        try {
                           return joinPoint.proceed();
                        } catch (Throwable e) {
@@ -134,7 +134,7 @@
                        }
                     }
                  } finally {
                     jedisPool.returnResource(jedis);
                     jedis.close();
                  }
               }
            } catch (Exception e) {// 原子性保护出错