From 81da61b828e29b7745e1382dfbbaeb685dc083ef Mon Sep 17 00:00:00 2001
From: admin <weikou2014>
Date: 星期二, 23 一月 2024 17:17:55 +0800
Subject: [PATCH] 抖音转链修改
---
fanli/src/main/java/com/yeshi/fanli/aspect/RequestSerializableAspect.java | 359 ++++++++++++++++++++++++++++++-----------------------------
1 files changed, 180 insertions(+), 179 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 a2d5d62..8aa516d 100644
--- a/fanli/src/main/java/com/yeshi/fanli/aspect/RequestSerializableAspect.java
+++ b/fanli/src/main/java/com/yeshi/fanli/aspect/RequestSerializableAspect.java
@@ -1,179 +1,180 @@
-package com.yeshi.fanli.aspect;
-
-import java.io.IOException;
-import java.io.PrintWriter;
-import java.lang.reflect.Method;
-import java.util.HashMap;
-import java.util.Map;
-
-import javax.annotation.Resource;
-
-import org.aspectj.lang.ProceedingJoinPoint;
-import org.aspectj.lang.Signature;
-import org.aspectj.lang.annotation.Around;
-import org.aspectj.lang.annotation.Aspect;
-import org.aspectj.lang.reflect.MethodSignature;
-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 JedisPool jedisPool;
-
- 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;
- Method targetMethod = methodSignature.getMethod();
- String cacheKey = null;
- 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 key = rs.key();
- 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 {
- return joinPoint.proceed();
- } catch (Throwable e1) {
- e.printStackTrace();
- LogHelper.errorDetailInfo(e1);
- }
- }
- }
-
- } catch (NoSuchMethodException e) {
- e.printStackTrace();
- } catch (SecurityException e) {
- e.printStackTrace();
- }
-
- try {
- return joinPoint.proceed();
- } catch (Throwable e) {
- e.printStackTrace();
- LogHelper.errorDetailInfo(e);
- } finally {
- }
- return null;
- }
-
- // 娴嬭瘯浠g爜
- public void test() {
-
- }
-
- class ParamsTypeValue {
- Class<?> type;
- Object value;
-
- public ParamsTypeValue(Class<?> type, Object value) {
- this.type = type;
- this.value = value;
- }
-
- public ParamsTypeValue() {
-
- }
- }
-
-}
+package com.yeshi.fanli.aspect;
+
+import java.io.PrintWriter;
+import java.lang.reflect.Method;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.annotation.Resource;
+
+import com.yeshi.fanli.util.RedisManager;
+import org.aspectj.lang.ProceedingJoinPoint;
+import org.aspectj.lang.Signature;
+import org.aspectj.lang.annotation.Around;
+import org.aspectj.lang.annotation.Aspect;
+import org.aspectj.lang.reflect.MethodSignature;
+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 RedisManager redisManager;
+
+ 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();
+ String cacheKey = null;
+ 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 key = rs.key();
+ cacheKey = generateKeyBySpEL(key, joinPoint);
+
+ 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 = redisManager.getJedis();
+ long threadId = Thread.currentThread().getId();
+ try {
+ Constant.waitingThreadSet.add(threadId);
+ String result = null;
+ long startTime = System.currentTimeMillis();
+ // 绛夊緟鍝嶅簲
+ 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 * 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();
+ }
+ }
+ } catch (Exception e) {// 鍘熷瓙鎬т繚鎶ゅ嚭閿�
+ return joinPoint.proceed();
+ }
+ }
+
+ } catch (NoSuchMethodException e) {
+ e.printStackTrace();
+ } catch (SecurityException e) {
+ e.printStackTrace();
+ }
+
+ try {
+ return joinPoint.proceed();
+ } catch (Throwable e) {
+ e.printStackTrace();
+ if (!Constant.IS_TEST)
+ LogHelper.errorDetailInfo(e);
+ else
+ throw e;
+ } finally {
+ }
+ return null;
+ }
+
+ // 娴嬭瘯浠g爜
+ public void test() {
+
+ }
+
+ class ParamsTypeValue {
+ Class<?> type;
+ Object value;
+
+ public ParamsTypeValue(Class<?> type, Object value) {
+ this.type = type;
+ this.value = value;
+ }
+
+ public ParamsTypeValue() {
+
+ }
+ }
+
+}
--
Gitblit v1.8.0