fanli/src/main/java/com/yeshi/fanli/aspect/RequestSerializableServiceAspect.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
fanli/src/main/java/com/yeshi/fanli/service/impl/user/integral/IntegralGetServiceImpl.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
fanli/src/main/java/com/yeshi/fanli/util/annotation/RequestSerializableByKeyService.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 |
fanli/src/main/java/com/yeshi/fanli/aspect/RequestSerializableServiceAspect.java
New file @@ -0,0 +1,131 @@ 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.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; @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()); long result = 0; long startTime = System.currentTimeMillis(); // 等待响应 while (result <= 0) { result = jedis.setnx(cacheKey, "1"); if (result <= 0) { 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) { 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); } } } 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(); } return joinPoint.proceed(); } } fanli/src/main/java/com/yeshi/fanli/service/impl/user/integral/IntegralGetServiceImpl.java
@@ -25,6 +25,8 @@ import com.yeshi.fanli.service.inter.user.integral.IntegralTaskRecordService; import com.yeshi.fanli.service.inter.user.integral.IntegralTaskService; import com.yeshi.fanli.util.RedisManager; import com.yeshi.fanli.util.annotation.RequestSerializableByKey; import com.yeshi.fanli.util.annotation.RequestSerializableByKeyService; import com.yeshi.fanli.util.annotation.integral.IntegralGetFrequencyLimit; import com.yeshi.fanli.util.annotation.integral.IntegralGetVersionLimit; @@ -50,12 +52,12 @@ @Resource private IntegralTaskRankService integralTaskRankService; // TODO 有bug private UserInfo getBossByUid(Long uid) { return threeSaleMapper.selectBoss(uid); } @IntegralGetVersionLimit(uid = "#uid") @RequestSerializableByKeyService(key = "#uid + '-' + #event") @Override public IntegralTaskRecord addEventStatistic(Long uid, String event) throws IntegralGetException { Date nowDate = new Date(); @@ -257,6 +259,7 @@ } @IntegralGetVersionLimit(uid = "#uid") @RequestSerializableByKeyService(key = "#uid") @Async() @Override public void addInviteLevelOne(Long uid, Long workerId) { @@ -277,6 +280,7 @@ * @param uid */ @IntegralGetVersionLimit(uid = "#uid") @RequestSerializableByKeyService(key = "#uid") @Async() private void addInviteLevelTwo(Long uid) { try { @@ -323,6 +327,7 @@ } @IntegralGetVersionLimit(uid = "#uid") @RequestSerializableByKeyService(key = "#uid") @Async() @Override public void addGiveRebateCoupon(Long uid) { @@ -334,6 +339,7 @@ } @IntegralGetVersionLimit(uid = "#uid") @RequestSerializableByKeyService(key = "#uid") @Async() @Override public void addGiveFreeCoupon(Long uid) { @@ -345,6 +351,7 @@ } @IntegralGetVersionLimit(uid = "#uid") @RequestSerializableByKeyService(key = "#uid") @Async() @Override public void addGiveTaoLiJin(Long uid) { @@ -389,6 +396,7 @@ * @param uid */ @IntegralGetVersionLimit(uid = "#uid") @RequestSerializableByKeyService(key = "#uid") @Async() private void addCouponRebateLevelOne(Long uid) { try { @@ -408,6 +416,7 @@ * @param uid */ @IntegralGetVersionLimit(uid = "#uid") @RequestSerializableByKeyService(key = "#uid") @Async() private void addCouponRebateLevelTwo(Long uid) { try { @@ -418,6 +427,7 @@ } @IntegralGetVersionLimit(uid = "#uid") @RequestSerializableByKeyService(key = "#uid") @Async() @Override public void addRebateOrder(Long uid) { @@ -434,6 +444,7 @@ * @param uid */ @IntegralGetVersionLimit(uid = "#uid") @RequestSerializableByKeyService(key = "#uid") @Async() @Override public void addInviteOrderLevelOne(Long uid) { @@ -450,6 +461,7 @@ * @param uid */ @IntegralGetVersionLimit(uid = "#uid") @RequestSerializableByKeyService(key = "#uid") @Async() @Override public void addInviteOrderLevelTwo(Long uid) { @@ -461,6 +473,7 @@ } @IntegralGetVersionLimit(uid = "#uid") @RequestSerializableByKeyService(key = "#uid") @Async() @Override public void addShareOrder(Long uid) { @@ -472,6 +485,7 @@ } @IntegralGetVersionLimit(uid = "#uid") @RequestSerializableByKeyService(key = "#uid") @Async() @Override public void addBindWeiXin(Long uid) { @@ -483,6 +497,7 @@ } @IntegralGetVersionLimit(uid = "#uid") @RequestSerializableByKeyService(key = "#uid") @Async() @Override public void addBindPhone(Long uid) { @@ -494,6 +509,7 @@ } @IntegralGetVersionLimit(uid = "#uid") @RequestSerializableByKeyService(key = "#uid") @Async() @Override public void addBindTaoBao(Long uid) { @@ -505,6 +521,7 @@ } @IntegralGetVersionLimit(uid = "#uid") @RequestSerializableByKeyService(key = "#uid") @Async() @Override public void addSetWeiXinNum(Long uid) { @@ -516,6 +533,7 @@ } @IntegralGetVersionLimit(uid = "#uid") @RequestSerializableByKeyService(key = "#uid") @Async() @Override public void addSetGender(Long uid) { @@ -527,6 +545,7 @@ } @IntegralGetVersionLimit(uid = "#uid") @RequestSerializableByKeyService(key = "#uid") @Async() @Override public void addSetPortrait(Long uid) { @@ -538,6 +557,7 @@ } @IntegralGetVersionLimit(uid = "#uid") @RequestSerializableByKeyService(key = "#uid") @Async() @Override public void addSetNickname(Long uid) { @@ -549,6 +569,7 @@ } @IntegralGetVersionLimit(uid = "#uid") @RequestSerializableByKeyService(key = "#uid") @Async() @Override public void addBindAlipay(Long uid) { @@ -560,6 +581,7 @@ } @IntegralGetVersionLimit(uid = "#uid") @RequestSerializableByKeyService(key = "#uid") @Async() @Override public void addInviteActivate(Long uid) { fanli/src/main/java/com/yeshi/fanli/util/annotation/RequestSerializableByKeyService.java
New file @@ -0,0 +1,16 @@ package com.yeshi.fanli.util.annotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Documented @Target(ElementType.METHOD) @Inherited @Retention(RetentionPolicy.RUNTIME) public @interface RequestSerializableByKeyService { String key(); }