| | |
| | | package com.yeshi.fanli.aspect;
|
| | |
|
| | | import java.lang.reflect.Method;
|
| | |
|
| | | 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.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 com.yeshi.fanli.util.Constant;
|
| | | import com.yeshi.fanli.util.StringUtil;
|
| | | import com.yeshi.fanli.util.annotation.RequestSerializableByKeyService;
|
| | |
|
| | | import redis.clients.jedis.Jedis;
|
| | | import redis.clients.jedis.JedisPool;
|
| | | import redis.clients.jedis.params.SetParams;
|
| | |
|
| | | @Component
|
| | | @Aspect
|
| | | public class RequestSerializableServiceAspect {
|
| | |
|
| | | @Resource
|
| | | private JedisPool jedisPool;
|
| | |
|
| | | private ExpressionParser parser = new SpelExpressionParser();
|
| | |
|
| | | private DefaultParameterNameDiscoverer nameDiscoverer = new DefaultParameterNameDiscoverer();
|
| | |
|
| | | 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.service.impl..*.*(..))")
|
| | | 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());
|
| | | if (realMethod.isAnnotationPresent(RequestSerializableByKeyService.class)) {
|
| | | RequestSerializableByKeyService rs = realMethod.getAnnotation(RequestSerializableByKeyService.class);
|
| | | String key = rs.key();
|
| | | cacheKey = generateKeyBySpEL(key, joinPoint);
|
| | | try {// redis做原子性保护
|
| | | if (!StringUtil.isNullOrEmpty(cacheKey)) {
|
| | | cacheKey = joinPoint.getTarget().getClass().getName() + "." + targetMethod.getName() + "-"
|
| | | + cacheKey;
|
| | | cacheKey = "service-" + StringUtil.Md5(cacheKey);
|
| | | // jiedis原子性做拦截
|
| | | Jedis jedis = jedisPool.getResource();
|
| | | try {
|
| | | Constant.waitingThreadSet.add(Thread.currentThread().getId());
|
| | | 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 * 60L) {
|
| | | Constant.waitingThreadSet.remove(Thread.currentThread().getId());
|
| | | return null;
|
| | | }
|
| | | }
|
| | | }
|
| | |
|
| | | 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();
|
| | | }
|
| | | return joinPoint.proceed();
|
| | | }
|
| | | }
|
| | | package com.yeshi.fanli.aspect; |
| | | |
| | | import java.lang.reflect.Method; |
| | | |
| | | 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.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 com.yeshi.fanli.util.Constant; |
| | | import com.yeshi.fanli.util.StringUtil; |
| | | import com.yeshi.fanli.util.annotation.RequestSerializableByKeyService; |
| | | |
| | | import redis.clients.jedis.Jedis; |
| | | import redis.clients.jedis.JedisPool; |
| | | import redis.clients.jedis.params.SetParams; |
| | | |
| | | @Component |
| | | @Aspect |
| | | public class RequestSerializableServiceAspect { |
| | | |
| | | @Resource |
| | | private RedisManager redisManager; |
| | | |
| | | private ExpressionParser parser = new SpelExpressionParser(); |
| | | |
| | | private DefaultParameterNameDiscoverer nameDiscoverer = new DefaultParameterNameDiscoverer(); |
| | | |
| | | 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.service.impl..*.*(..))") |
| | | 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()); |
| | | if (realMethod.isAnnotationPresent(RequestSerializableByKeyService.class)) { |
| | | RequestSerializableByKeyService rs = realMethod.getAnnotation(RequestSerializableByKeyService.class); |
| | | String key = rs.key(); |
| | | cacheKey = generateKeyBySpEL(key, joinPoint); |
| | | try {// redis做原子性保护 |
| | | if (!StringUtil.isNullOrEmpty(cacheKey)) { |
| | | cacheKey = joinPoint.getTarget().getClass().getName() + "." + targetMethod.getName() + "-" |
| | | + cacheKey; |
| | | cacheKey = "service-" + StringUtil.Md5(cacheKey); |
| | | // jiedis原子性做拦截 |
| | | Jedis jedis = redisManager.getJedis(); |
| | | try { |
| | | Constant.waitingThreadSet.add(Thread.currentThread().getId()); |
| | | 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 * 60L) { |
| | | Constant.waitingThreadSet.remove(Thread.currentThread().getId()); |
| | | return null; |
| | | } |
| | | } |
| | | } |
| | | |
| | | 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(); |
| | | } |
| | | return joinPoint.proceed(); |
| | | } |
| | | } |