| | |
| | | package com.yeshi.fanli.aspect;
|
| | |
|
| | | import java.io.IOException;
|
| | | import java.io.PrintWriter;
|
| | | import java.lang.reflect.Method;
|
| | | import java.util.HashMap;
|
| | |
| | |
|
| | | import redis.clients.jedis.Jedis;
|
| | | import redis.clients.jedis.JedisPool;
|
| | | import redis.clients.jedis.params.SetParams;
|
| | |
|
| | | @Component
|
| | | @Aspect
|
| | |
| | | }
|
| | |
|
| | | @Around("execution(public * com.yeshi.fanli.controller.client.*.*.*(..))")
|
| | | public Object requestSerializable(ProceedingJoinPoint joinPoint) throws IOException {
|
| | | public Object requestSerializable(ProceedingJoinPoint joinPoint) throws Throwable {
|
| | | Signature signature = joinPoint.getSignature();
|
| | | MethodSignature methodSignature = (MethodSignature) signature;
|
| | | Method targetMethod = methodSignature.getMethod();
|
| | |
| | | cacheKey = "rs-" + StringUtil.Md5(cacheKey);
|
| | | // jiedis原子性做拦截
|
| | | Jedis jedis = jedisPool.getResource();
|
| | | long threadId= Thread.currentThread().getId();
|
| | | LogHelper.test("进入拦截:"+cacheKey+"-"+threadId);
|
| | | long threadId = Thread.currentThread().getId();
|
| | | try {
|
| | | Constant.waitingThreadSet.add(threadId);
|
| | | long result = 0;
|
| | | 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) {
|
| | | LogHelper.test("等待:"+threadId);
|
| | | 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) {
|
| | |
| | | return null;
|
| | | }
|
| | | } else {
|
| | | // 设置30秒处理时间
|
| | | jedis.expire(cacheKey, 30);
|
| | | LogHelper.test("获取到锁:"+threadId);
|
| | | break;
|
| | | }
|
| | | }
|
| | |
|
| | | if (result > 0) {
|
| | | if (!StringUtil.isNullOrEmpty(result)) {
|
| | | try {
|
| | | return joinPoint.proceed();
|
| | | } catch (Throwable e) {
|
| | | e.printStackTrace();
|
| | | LogHelper.errorDetailInfo(e);
|
| | | } finally {
|
| | | LogHelper.test("方法执行完毕:"+threadId);
|
| | | jedis.del(cacheKey);
|
| | | Constant.waitingThreadSet.remove(Thread.currentThread().getId());
|
| | | LogHelper.test("释放锁:"+threadId);
|
| | | }
|
| | | }
|
| | | } finally {
|
| | | jedisPool.returnResource(jedis);
|
| | | jedis.close();
|
| | | }
|
| | | }
|
| | | } catch (Exception e) {// 原子性保护出错
|
| | | try {
|
| | | return joinPoint.proceed();
|
| | | } catch (Throwable e1) {
|
| | | e.printStackTrace();
|
| | | LogHelper.errorDetailInfo(e1);
|
| | | }
|
| | | return joinPoint.proceed();
|
| | | }
|
| | | }
|
| | |
|
| | |
| | | return joinPoint.proceed();
|
| | | } catch (Throwable e) {
|
| | | e.printStackTrace();
|
| | | LogHelper.errorDetailInfo(e);
|
| | | if (!Constant.IS_TEST)
|
| | | LogHelper.errorDetailInfo(e);
|
| | | else
|
| | | throw e;
|
| | | } finally {
|
| | | }
|
| | | return null;
|