| | |
| | | package com.yeshi.fanli.aspect;
|
| | |
|
| | | import java.lang.reflect.Method;
|
| | |
|
| | | import javax.annotation.Resource;
|
| | | import javax.servlet.http.HttpServletRequest;
|
| | |
|
| | | import org.aspectj.lang.ProceedingJoinPoint;
|
| | | import org.aspectj.lang.annotation.Around;
|
| | | import org.aspectj.lang.annotation.Aspect;
|
| | | import org.springframework.core.annotation.Order;
|
| | | import org.springframework.core.task.TaskExecutor;
|
| | | import org.springframework.stereotype.Component;
|
| | | import org.springframework.web.context.request.RequestContextHolder;
|
| | | import org.springframework.web.context.request.ServletRequestAttributes;
|
| | | import org.aspectj.lang.Signature;
|
| | | import org.aspectj.lang.annotation.Before;
|
| | | 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.yeshi.utils.NumberUtil;
|
| | |
|
| | | import com.yeshi.fanli.service.inter.user.UserSystemCouponService;
|
| | | import com.yeshi.fanli.util.Constant;
|
| | | import com.yeshi.fanli.util.RedisManager;
|
| | | import com.yeshi.fanli.util.StringUtil;
|
| | | import com.yeshi.fanli.util.ThreadUtil;
|
| | | import com.yeshi.fanli.util.annotation.UserActive;
|
| | |
|
| | | import redis.clients.jedis.Jedis;
|
| | | import redis.clients.jedis.JedisPool;
|
| | |
|
| | | /**
|
| | | * 活跃用户处理
|
| | |
| | | //@Order(6)
|
| | | public class ActiveUserAspect {
|
| | |
|
| | | @Resource(name = "taskExecutor")
|
| | | private TaskExecutor executor;
|
| | |
|
| | | @Resource
|
| | | private RedisManager redisManager;
|
| | | private JedisPool jedisPool;
|
| | |
|
| | | @Resource
|
| | | private UserSystemCouponService userSystemCouponService;
|
| | | private ExpressionParser parser = new SpelExpressionParser();
|
| | |
|
| | | private DefaultParameterNameDiscoverer nameDiscoverer = new DefaultParameterNameDiscoverer();
|
| | |
|
| | | public static final String EDP = "execution(* com.yeshi.fanli.controller.client.*.*(..))";
|
| | |
|
| | | @Around(EDP)
|
| | | public Object activeAround(ProceedingJoinPoint joinPoint) throws Throwable {
|
| | |
|
| | | Long uid = null;
|
| | | |
| | | ServletRequestAttributes servletContainer = (ServletRequestAttributes) RequestContextHolder
|
| | | .getRequestAttributes();
|
| | | HttpServletRequest request = servletContainer.getRequest();
|
| | | |
| | | String str_uid = request.getParameter("uid");
|
| | | if (str_uid != null && str_uid.trim().length() > 0) {
|
| | | uid = Long.parseLong(str_uid);
|
| | | 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();
|
| | | }
|
| | |
|
| | | public static final String EDP = "execution(* com.yeshi.fanli.controller.client.*.*.*(..))";
|
| | |
|
| | | final Long uuid = uid;
|
| | | |
| | | executor.execute(new Runnable() {
|
| | | @Before(EDP)
|
| | | public void activeBefore(ProceedingJoinPoint joinPoint) {
|
| | | try {
|
| | | Signature signature = joinPoint.getSignature();
|
| | | MethodSignature methodSignature = (MethodSignature) signature;
|
| | | Method targetMethod = methodSignature.getMethod();
|
| | | Method realMethod = joinPoint.getTarget().getClass().getDeclaredMethod(joinPoint.getSignature().getName(),
|
| | | targetMethod.getParameterTypes());
|
| | | if (realMethod.isAnnotationPresent(UserActive.class)) {
|
| | | UserActive rs = realMethod.getAnnotation(UserActive.class);
|
| | | String uidStr = generateKeyBySpEL(rs.uid(), joinPoint);
|
| | | if (!StringUtil.isNullOrEmpty(uidStr) && NumberUtil.isNumeric(uidStr)) {
|
| | | ThreadUtil.run(new Runnable() {
|
| | | @Override
|
| | | public void run() {
|
| | | String key = "useractive-" + uidStr;
|
| | | Jedis jedis = jedisPool.getResource();
|
| | | try {
|
| | |
|
| | | if (uuid == null) {
|
| | | return;
|
| | | }
|
| | | if (jedis.setnx(key, "1") > 0) {
|
| | | jedis.expire(key, 60);// 60s内不处理
|
| | | try {
|
| | | // TODO 用户活跃处理
|
| | |
|
| | | if (Constant.IS_OUTNET) {
|
| | | String key = "activeUid_" + uuid;
|
| | | // 缓存中是否存在uid
|
| | | String cacheValue = redisManager.getCommonString(key);
|
| | | if (cacheValue != null && cacheValue.trim().length() > 0) {
|
| | | return;
|
| | | } catch (Exception e) {
|
| | | }
|
| | | // 加入缓存 20分钟
|
| | | redisManager.cacheCommonString(key, uuid.toString(), 60 * 20);
|
| | | }
|
| | | } finally {
|
| | | jedisPool.returnResource(jedis);
|
| | | }
|
| | | }
|
| | | });
|
| | | }
|
| | | }
|
| | | } catch (Exception e) {
|
| | |
|
| | | return joinPoint.proceed(joinPoint.getArgs());
|
| | | }
|
| | | }
|
| | |
|
| | | }
|