|
@@ -0,0 +1,125 @@
|
|
|
+package cn.com.qmth.examcloud.reports.commons.util;
|
|
|
+
|
|
|
+import java.net.Inet4Address;
|
|
|
+import java.net.InetAddress;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+import org.apache.commons.collections.CollectionUtils;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.kafka.core.KafkaTemplate;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+
|
|
|
+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.LoginStudentReport;
|
|
|
+import cn.com.qmth.examcloud.reports.commons.bean.OnlineExamStudentReport;
|
|
|
+import cn.com.qmth.examcloud.reports.commons.bean.OnlineStudentReport;
|
|
|
+import cn.com.qmth.examcloud.reports.commons.bean.OnlineUserReport;
|
|
|
+import cn.com.qmth.examcloud.reports.commons.enums.MqType;
|
|
|
+import cn.com.qmth.examcloud.reports.commons.handler.KafkaSendResultHandler;
|
|
|
+import cn.com.qmth.examcloud.web.bootstrap.PropertyHolder;
|
|
|
+import cn.com.qmth.examcloud.web.support.SpringContextHolder;
|
|
|
+
|
|
|
+@SuppressWarnings("unchecked")
|
|
|
+public class ReportsUtil {
|
|
|
+ private final static Logger logger = LoggerFactory.getLogger(ReportsUtil.class);
|
|
|
+
|
|
|
+ private final static String key = "report";
|
|
|
+
|
|
|
+ private static KafkaTemplate<String, String> kafkaTemplate;
|
|
|
+
|
|
|
+ private static String mqType = PropertyHolder.getString("$report.mq-type");
|
|
|
+
|
|
|
+ private static Boolean reportEnable = PropertyHolder.getBoolean("$report.enable", false);
|
|
|
+
|
|
|
+ static {
|
|
|
+ if (reportEnable) {
|
|
|
+ if (MqType.KAFKA.getCode().equals(mqType)) {
|
|
|
+ kafkaTemplate = SpringContextHolder.getBean(KafkaTemplate.class);
|
|
|
+ kafkaTemplate.setProducerListener(new KafkaSendResultHandler());
|
|
|
+ } else if (MqType.ROCKETMQ.getCode().equals(mqType)) {
|
|
|
+ // TODO
|
|
|
+ } else {
|
|
|
+ logger.error("no $report.mq-type property config!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void sendReportKafka(Boolean onException) {
|
|
|
+ List<BaseReport> list = (List<BaseReport>) ThreadLocalUtil.get(key);
|
|
|
+ if (CollectionUtils.isNotEmpty(list)) {
|
|
|
+ for (BaseReport b : list) {
|
|
|
+ if (!onException || (onException && b.getReportOnException())) {
|
|
|
+ try {
|
|
|
+ if (b instanceof OnlineExamStudentReport) {
|
|
|
+ OnlineExamStudentReport ob = (OnlineExamStudentReport) b;
|
|
|
+ kafkaTemplate.send(ob.getTopic(), JSON.toJSONString(b));
|
|
|
+ LoginStudentReport lp = new LoginStudentReport(ob.getRootOrgId(), ob.getStudentId());
|
|
|
+ setReportCommonData(lp);
|
|
|
+ kafkaTemplate.send(lp.getTopic(), JSON.toJSONString(lp));
|
|
|
+ } else if (b instanceof OnlineStudentReport) {
|
|
|
+ OnlineStudentReport ob = (OnlineStudentReport) b;
|
|
|
+ kafkaTemplate.send(ob.getTopic(), JSON.toJSONString(b));
|
|
|
+ LoginStudentReport lp = new LoginStudentReport(ob.getRootOrgId(), ob.getStudentId());
|
|
|
+ setReportCommonData(lp);
|
|
|
+ kafkaTemplate.send(lp.getTopic(), JSON.toJSONString(lp));
|
|
|
+ } else if (b instanceof OnlineUserReport) {
|
|
|
+ OnlineUserReport ob = (OnlineUserReport) b;
|
|
|
+ kafkaTemplate.send(ob.getTopic(), JSON.toJSONString(b));
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error("SendReport Error:" + JSON.toJSONString(b), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ThreadLocalUtil.set(key, null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void sendReportRocket(Boolean onException) {
|
|
|
+ // TODO
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void report(BaseReport report) {
|
|
|
+ try {
|
|
|
+ if (!reportEnable) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ setReportCommonData(report);
|
|
|
+ List<BaseReport> list = (List<BaseReport>) ThreadLocalUtil.get(key);
|
|
|
+ if (list == null) {
|
|
|
+ list = new ArrayList<BaseReport>();
|
|
|
+ ThreadLocalUtil.set(key, list);
|
|
|
+ }
|
|
|
+ list.add(report);
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error(JSON.toJSONString(report), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void sendReport(Boolean onException) {
|
|
|
+ if (MqType.KAFKA.getCode().equals(mqType)) {
|
|
|
+ sendReportKafka(onException);
|
|
|
+ } else if (MqType.ROCKETMQ.getCode().equals(mqType)) {
|
|
|
+ sendReportRocket(onException);
|
|
|
+ } else {
|
|
|
+ logger.error("no $report.mq-type property config!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void setReportCommonData(BaseReport report) {
|
|
|
+ String ip = null;
|
|
|
+ try {
|
|
|
+ InetAddress localHost = Inet4Address.getLocalHost();
|
|
|
+ ip = localHost.getHostAddress();
|
|
|
+ } catch (Exception e) {
|
|
|
+ ip ="";
|
|
|
+ }
|
|
|
+ report.setReportHost(ip);
|
|
|
+ report.setReportTime(new Date());
|
|
|
+ }
|
|
|
+}
|