| | |
| | | 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(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);
|
| | | producer.send(message);
|
| | | // }
|
| | | } catch (Exception e) {
|
| | | |
| | | }
|
| | | }
|
| | | } finally {
|
| | | jedis.close();
|
| | | }
|
| | | }
|
| | | });
|
| | | }
|
| | | }
|
| | | } catch (Exception e) {
|
| | |
|
| | | }
|
| | | }
|
| | |
|
| | | }
|
| | | package com.yeshi.fanli.aspect; |
| | | |
| | | import java.lang.reflect.Method; |
| | | import java.util.Date; |
| | | |
| | | import javax.annotation.Resource; |
| | | |
| | | import com.yeshi.fanli.util.RedisManager; |
| | | 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 RedisManager redisManager; |
| | | |
| | | 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 =redisManager.getJedis(); |
| | | 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) { |
| | | |
| | | } |
| | | } |
| | | |
| | | } |