yujian
2020-05-09 7e7db2fa55a9a3af46d4fd8ede0dee147f101d64
fanli/src/main/java/com/yeshi/fanli/aspect/RequestSerializableAspect.java
@@ -1,8 +1,6 @@
package com.yeshi.fanli.aspect;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
@@ -14,26 +12,57 @@
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.cache.Cache;
import org.springframework.cache.Cache.ValueWrapper;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.core.annotation.Order;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.stereotype.Component;
import org.yeshi.utils.JsonUtil;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.annotation.RequestSerializableByKey;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.params.SetParams;
@Component
@Aspect
@Order(4)
public class RequestSerializableAspect {
   @Resource
   private EhCacheCacheManager cacheManager;
   private JedisPool jedisPool;
   @Around("execution(public * com.yeshi.fanli.controller.client.*.*(..))")
   public Object requestSerializable(ProceedingJoinPoint joinPoint) throws IOException {
   private ExpressionParser parser = new SpelExpressionParser();
   private DefaultParameterNameDiscoverer nameDiscoverer = new DefaultParameterNameDiscoverer();
   /**
    * 获取表达的值
    *
    * @param spELString
    * @param joinPoint
    * @return
    */
   public String generateKeyBySpEL(String spELString, ProceedingJoinPoint joinPoint) {
      MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
      String[] paramNames = nameDiscoverer.getParameterNames(methodSignature.getMethod());
      Expression expression = parser.parseExpression(spELString);
      EvaluationContext context = new StandardEvaluationContext();
      Object[] args = joinPoint.getArgs();
      for (int i = 0; i < args.length; i++) {
         context.setVariable(paramNames[i], args[i]);
      }
      return expression.getValue(context).toString();
   }
   @Around("execution(public * com.yeshi.fanli.controller.client.*.*.*(..))")
   public Object requestSerializable(ProceedingJoinPoint joinPoint) throws Throwable {
      Signature signature = joinPoint.getSignature();
      MethodSignature methodSignature = (MethodSignature) signature;
      Method targetMethod = methodSignature.getMethod();
@@ -41,65 +70,71 @@
      try {
         Method realMethod = joinPoint.getTarget().getClass().getDeclaredMethod(joinPoint.getSignature().getName(),
               targetMethod.getParameterTypes());
         Object[] args = joinPoint.getArgs();
         PrintWriter out = null;
         String[] strings = methodSignature.getParameterNames();
         Map<String, ParamsTypeValue> map = new HashMap<>();
         Class<?>[] types = methodSignature.getParameterTypes();
         for (int i = 0; i < strings.length; i++) {
            map.put(strings[i], new ParamsTypeValue(types[i], args[i]));
            if (args[i] instanceof PrintWriter) {
               out = (PrintWriter) args[i];
            }
         }
         if (realMethod.isAnnotationPresent(RequestSerializableByKey.class)) {
            RequestSerializableByKey rs = realMethod.getAnnotation(RequestSerializableByKey.class);
            String[] strings = methodSignature.getParameterNames();
            Class<?>[] types = methodSignature.getParameterTypes();
            Map<String, ParamsTypeValue> map = new HashMap<>();
            Object[] args = joinPoint.getArgs();
            PrintWriter out = null;
            for (int i = 0; i < strings.length; i++) {
               map.put(strings[i], new ParamsTypeValue(types[i], args[i]));
               if (args[i] instanceof PrintWriter) {
                  out = (PrintWriter) args[i];
               }
            }
            String key = rs.key();
            ParamsTypeValue tv = null;
            if (key.indexOf(".") > -1) {
               tv = map.get(key.split("\\.")[0]);
            } else {
               tv = map.get(key);
            }
            Object value = tv.value;
            Class<?> type = tv.type;
            // 暂时只支持2层嵌套
            if (key.indexOf(".") > -1) {
               String child = key.split("\\.")[1];
               try {
                  Field f = type.getDeclaredField(child);
                  f.setAccessible(true);
                  Object obj = f.get(value);
                  if (obj != null) {
                     cacheKey = obj.toString();
                  }
               } catch (NoSuchFieldException e) {
                  e.printStackTrace();
               } catch (Exception e) {
                  e.printStackTrace();
               }
            cacheKey = generateKeyBySpEL(key, joinPoint);
            } else {
               if (value != null) {
                  cacheKey = value.toString();
               }
            }
            try {// redis做原子性保护
               if (!StringUtil.isNullOrEmpty(cacheKey)) {
                  cacheKey = joinPoint.getTarget().getClass().getName() + "." + targetMethod.getName() + "-"
                        + cacheKey;
                  String cacheAlias = cacheKey;
                  cacheKey = "rs-" + StringUtil.Md5(cacheKey);
                  // jiedis原子性做拦截
                  Jedis jedis = jedisPool.getResource();
                  long threadId = Thread.currentThread().getId();
                  try {
                     Constant.waitingThreadSet.add(threadId);
                     String result = null;
                     long startTime = System.currentTimeMillis();
                     // 等待响应
                     while (StringUtil.isNullOrEmpty(result)) {
            if (!StringUtil.isNullOrEmpty(cacheKey)) {
               cacheKey = joinPoint.getTarget().getClass().getName() + "." + targetMethod.getName() + "-"
                     + cacheKey;
               Cache cache = cacheManager.getCache("rsCache");
               ValueWrapper element = cache.get(cacheKey);
               if (element != null && element.get() != null) {
                  // 服务器繁忙
                  if (out != null) {
                     out.print(JsonUtil.loadFalseResult(2001, "服务器繁忙"));
                        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 * 30L) {
                              Constant.waitingThreadSet.remove(Thread.currentThread().getId());
                              out.print(JsonUtil.loadFalseResult("连接超时"));
                              return null;
                           }
                        } else {
                           break;
                        }
                     }
                     if (!StringUtil.isNullOrEmpty(result)) {
                        try {
                           return joinPoint.proceed();
                        } finally {
                           jedis.del(cacheKey);
                           Constant.waitingThreadSet.remove(Thread.currentThread().getId());
                        }
                     }
                  } finally {
                     jedis.close();
                  }
                  return null;
               } else {
                  cache.put(cacheKey, "1");
               }
            } catch (Exception e) {// 原子性保护出错
               return joinPoint.proceed();
            }
         }
@@ -113,17 +148,20 @@
         return joinPoint.proceed();
      } catch (Throwable e) {
         e.printStackTrace();
         LogHelper.errorDetailInfo(e);
         if (!Constant.IS_TEST)
            LogHelper.errorDetailInfo(e);
         else
            throw e;
      } finally {
         if (!StringUtil.isNullOrEmpty(cacheKey)) {
            Cache cache = cacheManager.getCache("rsCache");
            cache.put(cacheKey, null);
         }
      }
      return null;
   }
   // 测试代码
   public void test() {
   }
   class ParamsTypeValue {
      Class<?> type;
      Object value;