From 505d8e50a24a22cda05d04da8485ccc87fe8761e Mon Sep 17 00:00:00 2001 From: yujian <yujian@123.com> Date: 星期四, 15 八月 2019 16:02:25 +0800 Subject: [PATCH] Merge branch 'div' of ssh://193.112.35.168:29418/fanli-server into div --- fanli/src/main/java/com/yeshi/fanli/aspect/RequestSerializableAspect.java | 164 +++++++++++++++++++++++++++++++++--------------------- 1 files changed, 101 insertions(+), 63 deletions(-) diff --git a/fanli/src/main/java/com/yeshi/fanli/aspect/RequestSerializableAspect.java b/fanli/src/main/java/com/yeshi/fanli/aspect/RequestSerializableAspect.java index d77e361..a2d5d62 100644 --- a/fanli/src/main/java/com/yeshi/fanli/aspect/RequestSerializableAspect.java +++ b/fanli/src/main/java/com/yeshi/fanli/aspect/RequestSerializableAspect.java @@ -2,7 +2,6 @@ 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,25 +13,55 @@ 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; @Component @Aspect @Order(4) public class RequestSerializableAspect { @Resource - private EhCacheCacheManager cacheManager; + private JedisPool jedisPool; - @Around("execution(public * com.yeshi.fanli.controller.client.*.*(..))") + 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 IOException { Signature signature = joinPoint.getSignature(); MethodSignature methodSignature = (MethodSignature) signature; @@ -41,64 +70,73 @@ 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]; + cacheKey = generateKeyBySpEL(key, joinPoint); + + try {// redis鍋氬師瀛愭�т繚鎶� + if (!StringUtil.isNullOrEmpty(cacheKey)) { + cacheKey = joinPoint.getTarget().getClass().getName() + "." + targetMethod.getName() + "-" + + cacheKey; + cacheKey = "rs-" + StringUtil.Md5(cacheKey); + // jiedis鍘熷瓙鎬у仛鎷︽埅 + Jedis jedis = jedisPool.getResource(); + try { + Constant.waitingThreadSet.add(Thread.currentThread().getId()); + long result = 0; + long startTime = System.currentTimeMillis(); + // 绛夊緟鍝嶅簲 + while (result <= 0) { + result = jedis.setnx(cacheKey, "1"); + if (result <= 0) { + try { + Thread.sleep(50); + } catch (InterruptedException e) { + e.printStackTrace(); + } + if (System.currentTimeMillis() - startTime > 1000 * 60L) { + Constant.waitingThreadSet.remove(Thread.currentThread().getId()); + out.print(JsonUtil.loadFalseResult("杩炴帴瓒呮椂")); + return null; + } + } + } + + if (result > 0) { + try { + return joinPoint.proceed(); + } catch (Throwable e) { + e.printStackTrace(); + LogHelper.errorDetailInfo(e); + } finally { + jedis.del(cacheKey); + Constant.waitingThreadSet.remove(Thread.currentThread().getId()); + } + } + } finally { + jedisPool.returnResource(jedis); + } + } + } catch (Exception e) {// 鍘熷瓙鎬т繚鎶ゅ嚭閿� try { - Field f = type.getDeclaredField(child); - f.setAccessible(true); - Object obj = f.get(value); - if (obj != null) { - cacheKey = obj.toString(); - } - } catch (NoSuchFieldException e) { + return joinPoint.proceed(); + } catch (Throwable e1) { e.printStackTrace(); - } catch (Exception e) { - e.printStackTrace(); - } - - } else { - if (value != null) { - cacheKey = value.toString(); - } - } - - 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, "鏈嶅姟鍣ㄧ箒蹇�")); - } - return null; - } else { - cache.put(cacheKey, "1"); + LogHelper.errorDetailInfo(e1); } } } @@ -114,16 +152,16 @@ } catch (Throwable e) { e.printStackTrace(); LogHelper.errorDetailInfo(e); - } finally { - if (!StringUtil.isNullOrEmpty(cacheKey)) { - Cache cache = cacheManager.getCache("rsCache"); - cache.put(cacheKey, null); - } } return null; } + // 娴嬭瘯浠g爜 + public void test() { + + } + class ParamsTypeValue { Class<?> type; Object value; -- Gitblit v1.8.0