5 changed files with 185 additions and 1 deletions
@ -0,0 +1,110 @@ |
|||||
|
package com.epmet.mq.listener; |
||||
|
|
||||
|
import com.alibaba.fastjson.JSON; |
||||
|
import com.epmet.commons.rocketmq.constants.MQUserPropertys; |
||||
|
import com.epmet.commons.rocketmq.messages.LoginMQMsg; |
||||
|
import com.epmet.commons.tools.distributedlock.DistributedLock; |
||||
|
import com.epmet.commons.tools.exception.ExceptionUtils; |
||||
|
import com.epmet.commons.tools.exception.RenException; |
||||
|
import com.epmet.commons.tools.redis.RedisKeys; |
||||
|
import com.epmet.commons.tools.redis.RedisUtils; |
||||
|
import com.epmet.commons.tools.utils.SpringContextUtils; |
||||
|
import com.epmet.service.StaffLoginLogService; |
||||
|
import org.apache.commons.lang3.StringUtils; |
||||
|
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext; |
||||
|
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus; |
||||
|
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently; |
||||
|
import org.apache.rocketmq.common.message.MessageExt; |
||||
|
import org.redisson.api.RLock; |
||||
|
import org.slf4j.Logger; |
||||
|
import org.slf4j.LoggerFactory; |
||||
|
|
||||
|
import java.util.List; |
||||
|
import java.util.concurrent.TimeUnit; |
||||
|
|
||||
|
/** |
||||
|
* @author wxz |
||||
|
* @Description 登录操作日志监听器 |
||||
|
|
||||
|
* @return |
||||
|
* @date 2021.06.07 16:12 |
||||
|
*/ |
||||
|
public class StaffLoginLogListener implements MessageListenerConcurrently { |
||||
|
|
||||
|
private Logger logger = LoggerFactory.getLogger(getClass()); |
||||
|
|
||||
|
private RedisUtils redisUtils; |
||||
|
|
||||
|
@Override |
||||
|
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) { |
||||
|
|
||||
|
if (redisUtils == null) { |
||||
|
redisUtils = SpringContextUtils.getBean(RedisUtils.class); |
||||
|
} |
||||
|
|
||||
|
try { |
||||
|
msgs.forEach(msg -> consumeMessage(msg)); |
||||
|
} catch (Exception e) { |
||||
|
logger.error(ExceptionUtils.getErrorStackTrace(e)); |
||||
|
return ConsumeConcurrentlyStatus.RECONSUME_LATER; |
||||
|
} |
||||
|
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; |
||||
|
} |
||||
|
|
||||
|
private void consumeMessage(MessageExt messageExt) { |
||||
|
String tags = messageExt.getTags(); |
||||
|
// msg示例:
|
||||
|
// {
|
||||
|
// "userId": "5198ef9e3644c4f49457d2b551a1432e",
|
||||
|
// "appId": "数字社区登录",
|
||||
|
// "loginTime": "2023-04-04 14:05:37",
|
||||
|
// "ip": "219.146.91.110",
|
||||
|
// "fromApp": "gov",
|
||||
|
// "fromClient": "web"
|
||||
|
// }
|
||||
|
String msg = new String(messageExt.getBody()); |
||||
|
String pendingMsgLabel = messageExt.getUserProperty(MQUserPropertys.BLOCKED_MSG_LABEL); |
||||
|
logger.info("工作人员登录操作日志监听器-收到消息内容:{}", msg); |
||||
|
LoginMQMsg msgObj = JSON.parseObject(msg, LoginMQMsg.class); |
||||
|
|
||||
|
DistributedLock distributedLock = null; |
||||
|
RLock lock = null; |
||||
|
try { |
||||
|
distributedLock = SpringContextUtils.getBean(DistributedLock.class); |
||||
|
lock = distributedLock.getLock(String.format("lock:staff_login_log:%s:%s", tags, msgObj.getUserId()), |
||||
|
30L, 30L, TimeUnit.SECONDS); |
||||
|
SpringContextUtils.getBean(StaffLoginLogService.class).saveLog(msgObj.getUserId(),msgObj.getLoginTime()); |
||||
|
} catch (RenException e) { |
||||
|
// 如果是我们手动抛出的异常,说明在业务可控范围内。目前不需要MQ重试
|
||||
|
logger.error("【RocketMQ】工作人员登录添加操作日志失败:".concat(ExceptionUtils.getErrorStackTrace(e))); |
||||
|
} catch (Exception e) { |
||||
|
// 不是我们自己抛出的异常,可以让MQ重试
|
||||
|
logger.error("【RocketMQ】工作人员登录添加操作日志失败:".concat(ExceptionUtils.getErrorStackTrace(e))); |
||||
|
throw e; |
||||
|
} finally { |
||||
|
distributedLock.unLock(lock); |
||||
|
} |
||||
|
|
||||
|
if (StringUtils.isNotBlank(pendingMsgLabel)) { |
||||
|
try { |
||||
|
removePendingMqMsgCache(pendingMsgLabel); |
||||
|
} catch (Exception e) { |
||||
|
logger.error("【工作人员登录操作事件监听器】-删除mq阻塞消息缓存失败:{}", ExceptionUtils.getErrorStackTrace(e)); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @description |
||||
|
* |
||||
|
* @param pendingMsgLabel |
||||
|
* @return |
||||
|
* @author wxz |
||||
|
* @date 2021.10.14 16:32:32 |
||||
|
*/ |
||||
|
private void removePendingMqMsgCache(String pendingMsgLabel) { |
||||
|
String key = RedisKeys.blockedMqMsgKey(pendingMsgLabel); |
||||
|
redisUtils.delete(key); |
||||
|
//logger.info("【登录操作事件监听器】删除pendingMsgLabel成功:{}", pendingMsgLabel);
|
||||
|
} |
||||
|
} |
Loading…
Reference in new issue