| | |
| | | import org.springframework.expression.spel.support.StandardEvaluationContext;
|
| | | import org.springframework.stereotype.Component;
|
| | |
|
| | | import com.yeshi.fanli.log.LogHelper;
|
| | | 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
|
| | |
| | | Jedis jedis = jedisPool.getResource();
|
| | | try {
|
| | | Constant.waitingThreadSet.add(Thread.currentThread().getId());
|
| | | long result = 0;
|
| | | String result = null;
|
| | | long startTime = System.currentTimeMillis();
|
| | | // 等待响应
|
| | | while (result <= 0) {
|
| | | result = jedis.setnx(cacheKey, "1");
|
| | | if (result <= 0) {
|
| | | while (StringUtil.isNullOrEmpty(result)) {
|
| | | result = jedis.set(cacheKey, "1", new SetParams().nx().ex(30));
|
| | | if (StringUtil.isNullOrEmpty(result)) {
|
| | | try {
|
| | | System.out.println("__________已有线程在执行");
|
| | | Thread.sleep(50);
|
| | | } catch (InterruptedException e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | if (System.currentTimeMillis() - startTime > 1000 * 60L) {
|
| | | Constant.waitingThreadSet.remove(Thread.currentThread().getId());
|
| | | System.out.println("__________删除线程");
|
| | | return null;
|
| | | }
|
| | | } else {
|
| | | // 设置30秒处理时间
|
| | | jedis.expire(cacheKey, 30);
|
| | | }
|
| | | }
|
| | |
|
| | | if (result > 0) {
|
| | | if (!StringUtil.isNullOrEmpty(result)) {
|
| | | try {
|
| | | System.out.println("__________放行执行");
|
| | | 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);
|
| | | jedis.close();
|
| | | }
|
| | | }
|
| | | } catch (Exception e) {// 原子性保护出错
|
| | | try {
|
| | | return joinPoint.proceed();
|
| | | } catch (Throwable e1) {
|
| | | e.printStackTrace();
|
| | | LogHelper.errorDetailInfo(e1);
|
| | | }
|
| | | }
|
| | | }
|
| | | } catch (NoSuchMethodException e) {
|
| | |
| | | } catch (SecurityException e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | |
|
| | | return joinPoint.proceed();
|
| | | |
| | | }
|
| | |
|
| | | }
|