|
@@ -1,15 +1,14 @@
|
|
package cn.com.qmth.examcloud.reports.commons.util;
|
|
package cn.com.qmth.examcloud.reports.commons.util;
|
|
|
|
|
|
-import cn.com.qmth.examcloud.commons.exception.StatusException;
|
|
|
|
import cn.com.qmth.examcloud.commons.util.ThreadLocalUtil;
|
|
import cn.com.qmth.examcloud.commons.util.ThreadLocalUtil;
|
|
import cn.com.qmth.examcloud.reports.commons.bean.BaseReport;
|
|
import cn.com.qmth.examcloud.reports.commons.bean.BaseReport;
|
|
|
|
+import cn.com.qmth.examcloud.reports.commons.enums.Tag;
|
|
import cn.com.qmth.examcloud.web.bootstrap.PropertyHolder;
|
|
import cn.com.qmth.examcloud.web.bootstrap.PropertyHolder;
|
|
import cn.com.qmth.examcloud.web.support.IpUtil;
|
|
import cn.com.qmth.examcloud.web.support.IpUtil;
|
|
import cn.com.qmth.examcloud.web.support.ServletUtil;
|
|
import cn.com.qmth.examcloud.web.support.ServletUtil;
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.aliyun.openservices.ons.api.*;
|
|
import com.aliyun.openservices.ons.api.*;
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
-import org.apache.commons.lang3.StringUtils;
|
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
@@ -24,7 +23,13 @@ public class ReportsUtil {
|
|
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(ReportsUtil.class);
|
|
private static final Logger LOG = LoggerFactory.getLogger(ReportsUtil.class);
|
|
|
|
|
|
- private static final String KEY = "mq-report-data";
|
|
|
|
|
|
+ private static final String GROUP_PRODUCER_PREFIX = "ec_producer_";
|
|
|
|
+
|
|
|
|
+ private static final String GROUP_CONSUMER_PREFIX = "ec_consumer_";
|
|
|
|
+
|
|
|
|
+ private static final String TOPIC_PREFIX = "ec_topic_";
|
|
|
|
+
|
|
|
|
+ private static final String DATA_KEY = "ec_mq_data";
|
|
|
|
|
|
private static Producer producer;
|
|
private static Producer producer;
|
|
|
|
|
|
@@ -43,7 +48,7 @@ public class ReportsUtil {
|
|
properties.put(PropertyKeyConst.NAMESRV_ADDR, PropertyHolder.getString("examcloud.rocketmq.namesrv_addr"));
|
|
properties.put(PropertyKeyConst.NAMESRV_ADDR, PropertyHolder.getString("examcloud.rocketmq.namesrv_addr"));
|
|
|
|
|
|
// 生产组名称
|
|
// 生产组名称
|
|
- properties.put(PropertyKeyConst.GROUP_ID, PropertyHolder.getString("examcloud.rocketmq.producer_group"));
|
|
|
|
|
|
+ properties.put(PropertyKeyConst.GROUP_ID, curProducerGroup());
|
|
|
|
|
|
producer = ONSFactory.createProducer(properties);
|
|
producer = ONSFactory.createProducer(properties);
|
|
// 在发送消息前,必须调用 start 方法来启动 Producer,只需调用一次即可。
|
|
// 在发送消息前,必须调用 start 方法来启动 Producer,只需调用一次即可。
|
|
@@ -54,7 +59,7 @@ public class ReportsUtil {
|
|
|
|
|
|
private static void sendReportRocket(Boolean onException) {
|
|
private static void sendReportRocket(Boolean onException) {
|
|
@SuppressWarnings("unchecked")
|
|
@SuppressWarnings("unchecked")
|
|
- List<BaseReport> list = (List<BaseReport>) ThreadLocalUtil.get(KEY);
|
|
|
|
|
|
+ List<BaseReport> list = (List<BaseReport>) ThreadLocalUtil.get(DATA_KEY);
|
|
if (CollectionUtils.isNotEmpty(list)) {
|
|
if (CollectionUtils.isNotEmpty(list)) {
|
|
for (BaseReport b : list) {
|
|
for (BaseReport b : list) {
|
|
if (!onException || (onException && b.getReportOnException())) {
|
|
if (!onException || (onException && b.getReportOnException())) {
|
|
@@ -79,7 +84,7 @@ public class ReportsUtil {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- ThreadLocalUtil.set(KEY, null);
|
|
|
|
|
|
+ ThreadLocalUtil.set(DATA_KEY, null);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -90,10 +95,10 @@ public class ReportsUtil {
|
|
}
|
|
}
|
|
setReportCommonData(report);
|
|
setReportCommonData(report);
|
|
@SuppressWarnings("unchecked")
|
|
@SuppressWarnings("unchecked")
|
|
- List<BaseReport> list = (List<BaseReport>) ThreadLocalUtil.get(KEY);
|
|
|
|
|
|
+ List<BaseReport> list = (List<BaseReport>) ThreadLocalUtil.get(DATA_KEY);
|
|
if (list == null) {
|
|
if (list == null) {
|
|
list = new ArrayList<BaseReport>();
|
|
list = new ArrayList<BaseReport>();
|
|
- ThreadLocalUtil.set(KEY, list);
|
|
|
|
|
|
+ ThreadLocalUtil.set(DATA_KEY, list);
|
|
}
|
|
}
|
|
list.add(report);
|
|
list.add(report);
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
@@ -119,15 +124,19 @@ public class ReportsUtil {
|
|
report.setReportTime(new Date());
|
|
report.setReportTime(new Date());
|
|
}
|
|
}
|
|
|
|
|
|
- public static String getReportTopic() {
|
|
|
|
- String topic = PropertyHolder.getString("examcloud.rocketmq.topic");
|
|
|
|
- if (StringUtils.isBlank(topic)) {
|
|
|
|
- throw new StatusException("examcloud.rocketmq.topic 未配置");
|
|
|
|
- }
|
|
|
|
- return topic;
|
|
|
|
|
|
+ public static String curTopic(Tag tag) {
|
|
|
|
+ return TOPIC_PREFIX + tag.name().toLowerCase() + "_" + curProfile();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String curConsumerGroup(Tag tag) {
|
|
|
|
+ return GROUP_CONSUMER_PREFIX + tag.name().toLowerCase() + "_" + curProfile();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String curProducerGroup() {
|
|
|
|
+ return GROUP_PRODUCER_PREFIX + "_" + curProfile();
|
|
}
|
|
}
|
|
|
|
|
|
- public static String getProfile() {
|
|
|
|
|
|
+ public static String curProfile() {
|
|
return PropertyHolder.getString("spring.profiles.active", "");
|
|
return PropertyHolder.getString("spring.profiles.active", "");
|
|
}
|
|
}
|
|
|
|
|