yujian
2020-05-06 a4637ae9d71aa4a624b217ed3a1483f0e3a3a7ed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package com.yeshi.fanli.aspect;
 
import java.lang.reflect.Method;
import java.util.Date;
 
import javax.annotation.Resource;
 
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
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.springframework.stereotype.Component;
import org.yeshi.utils.NumberUtil;
 
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.Producer;
import com.yeshi.fanli.dto.mq.user.UserTopicTagEnum;
import com.yeshi.fanli.dto.mq.user.body.UserActiveMQMsg;
import com.yeshi.fanli.service.inter.user.UserInfoExtraService;
import com.yeshi.fanli.service.inter.user.UserInfoModifyRecordService;
import com.yeshi.fanli.service.inter.user.integral.IntegralGetService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.ThreadUtil;
import com.yeshi.fanli.util.annotation.UserActive;
import com.yeshi.fanli.util.rocketmq.MQMsgBodyFactory;
import com.yeshi.fanli.util.rocketmq.MQTopicName;
 
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.params.SetParams;
 
/**
 * 活跃用户处理
 * 
 * @author Administrator
 *
 */
@Component
@Aspect
public class ActiveUserAspect {
    @Resource
    private JedisPool jedisPool;
 
    @Resource
    private UserInfoModifyRecordService userInfoModifyRecordService;
 
    @Resource
    private UserInfoExtraService userInfoExtraService;
 
    @Resource
    private IntegralGetService integralGetService;
    
    @Resource(name = "producer")
    private Producer producer;
    
 
    private ExpressionParser parser = new SpelExpressionParser();
 
    private DefaultParameterNameDiscoverer nameDiscoverer = new DefaultParameterNameDiscoverer();
 
    public String generateKeyBySpEL(String spELString, JoinPoint 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.*.*.*(..))";
 
    @Before(EDP)
    public void activeBefore(JoinPoint 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 (!StringUtil.isNullOrEmpty(jedis.set(key, "1", new SetParams().nx().ex(Constant.IS_TEST?10:300)))) {
                                    try {
//                                        Long uid = Long.parseLong(uidStr);
//                                        if (!Constant.IS_TEST) { // 活跃通知
//                                            UserActiveMQMsg msg = new UserActiveMQMsg(uid, new Date());
//                                            Message message = MQMsgBodyFactory.create(MQTopicName.TOPIC_USER, UserTopicTagEnum.userActve, msg);
//                                            message.setStartDeliverTime(System.currentTimeMillis()+1000*10L);//10s后发送活跃消息
//                                            producer.send(message);
//                                        }
                                    } catch (Exception e) {
                                        
                                    }
                                }
                            } finally {
                                jedis.close();
                            }
                        }
                    });
                }
            }
        } catch (Exception e) {
 
        }
    }
 
}