|
@@ -4,6 +4,8 @@ import com.aliyun.openservices.ons.api.Action;
|
|
|
import com.aliyun.openservices.ons.api.ConsumeContext;
|
|
|
import com.aliyun.openservices.ons.api.Message;
|
|
|
import com.aliyun.openservices.ons.api.MessageListener;
|
|
|
+import com.dianping.cat.Cat;
|
|
|
+import com.dianping.cat.message.Transaction;
|
|
|
import com.elab.core.utils.DateUtils;
|
|
|
import com.elab.core.utils.ObjectUtils;
|
|
|
import com.elab.mq.dao.IConsumerDao;
|
|
@@ -32,6 +34,8 @@ public abstract class AbstractMessageListener implements MessageListener {
|
|
|
@Value("${spring.application.name}")
|
|
|
private String moduleName;
|
|
|
|
|
|
+ private final String MQ_CONSUMER = "MQ_CONSUMER";
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* 客户端关注的topic消息,实现了该方法,消息监听被触发时会与之匹配。
|
|
@@ -61,9 +65,7 @@ public abstract class AbstractMessageListener implements MessageListener {
|
|
|
String topic = message.getTopic();
|
|
|
String tag = message.getTag();
|
|
|
ConsumerEntity oldConsumerEntity = new ConsumerEntity();
|
|
|
- logger.info("进入消费者验证...");
|
|
|
ConsumerEntity consumerEntity = new ConsumerEntity();
|
|
|
-
|
|
|
consumerEntity.setProducerId(producerId);
|
|
|
consumerEntity.setTopicId(topic);
|
|
|
consumerEntity.setModuleName(moduleName);
|
|
@@ -77,12 +79,14 @@ public abstract class AbstractMessageListener implements MessageListener {
|
|
|
// throw new BusinessException("请不要重复消费,key:" + message);
|
|
|
return oldConsumerEntity;
|
|
|
}
|
|
|
- logger.info("消费者验证通过,消费者消费的数据匹配上生产者数据,");
|
|
|
+ logger.debug("消费者验证通过,消费者消费的数据匹配上生产者数据,");
|
|
|
if (oldConsumerEntity != null) {
|
|
|
oldConsumerEntity.setRetryCount(oldConsumerEntity.getRetryCount() + 1);
|
|
|
oldConsumerEntity.setConsumerStatus(-1);
|
|
|
oldConsumerEntity.setUpdated(DateUtils.getCurrentDateTime());
|
|
|
+ oldConsumerEntity.setCatId(Cat.getCurrentMessageId());
|
|
|
consumerDao.updateById(oldConsumerEntity);
|
|
|
+ logger.debug("触发消息重试 当前重试次数 : " + oldConsumerEntity.getRetryCount());
|
|
|
} else {
|
|
|
oldConsumerEntity = new ConsumerEntity();
|
|
|
oldConsumerEntity.setProducerId(producerId);
|
|
@@ -96,9 +100,11 @@ public abstract class AbstractMessageListener implements MessageListener {
|
|
|
oldConsumerEntity.setConsumerStatus(0);
|
|
|
oldConsumerEntity.setMsgId(msgID);
|
|
|
oldConsumerEntity.setTag(tag);
|
|
|
+ oldConsumerEntity.setCatId(Cat.getCurrentMessageId());
|
|
|
oldConsumerEntity.setModuleMethod(getClass().getName());
|
|
|
int id = consumerDao.insert(oldConsumerEntity);
|
|
|
oldConsumerEntity.setId(id);
|
|
|
+ logger.debug(" 一条全新的消息,消息编号:" + id);
|
|
|
}
|
|
|
return oldConsumerEntity;
|
|
|
}
|
|
@@ -115,6 +121,9 @@ public abstract class AbstractMessageListener implements MessageListener {
|
|
|
|
|
|
@Override
|
|
|
public Action consume(Message message, ConsumeContext consumeContext) {
|
|
|
+ String topic = message.getTopic();
|
|
|
+ String tag = message.getTag();
|
|
|
+ Transaction t = Cat.newTransaction(MQ_CONSUMER, topic + "_" + tag + "_" + getClass().getSimpleName());
|
|
|
MessageModel messageModel = new MessageModel(message);
|
|
|
logger.debug("消息处理被触发 : " + message.toString());
|
|
|
Action action = null;
|
|
@@ -123,10 +132,11 @@ public abstract class AbstractMessageListener implements MessageListener {
|
|
|
//如果已经成功过一次
|
|
|
if (oldConsumerEntity.getConsumerStatus() == 1) {
|
|
|
logger.warn(" 重复消费 过滤掉. 消费编号 : " + oldConsumerEntity.getId());
|
|
|
+ t.setSuccessStatus();
|
|
|
return Action.CommitMessage;
|
|
|
}
|
|
|
|
|
|
- logger.info("更新消费者数据: " + ObjectUtils.objectParseJsonStr(oldConsumerEntity));
|
|
|
+ logger.debug("更新消费者数据: " + ObjectUtils.objectParseJsonStr(oldConsumerEntity));
|
|
|
action = consume0(messageModel, consumeContext);
|
|
|
|
|
|
if (action == null || Action.ReconsumeLater.equals(action)) {
|
|
@@ -138,10 +148,14 @@ public abstract class AbstractMessageListener implements MessageListener {
|
|
|
oldConsumerEntity.setUpdated(new Date());
|
|
|
consumerDao.updateById(oldConsumerEntity);
|
|
|
}
|
|
|
+ t.setSuccessStatus();
|
|
|
} catch (Exception e) {
|
|
|
+ t.setStatus(e);
|
|
|
logger.debug("消息处理异常 : " + action);
|
|
|
e.printStackTrace();
|
|
|
return Action.ReconsumeLater;
|
|
|
+ } finally {
|
|
|
+ t.complete();
|
|
|
}
|
|
|
logger.debug("消息处理结果 : " + action);
|
|
|
return action;
|