|
@@ -0,0 +1,120 @@
|
|
|
+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 java.util.Properties;
|
|
|
+
|
|
|
+import org.apache.commons.collections.CollectionUtils;
|
|
|
+import org.apache.kafka.clients.producer.KafkaProducer;
|
|
|
+import org.apache.kafka.clients.producer.ProducerRecord;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+
|
|
|
+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.enums.MqType;
|
|
|
+import cn.com.qmth.examcloud.reports.commons.handler.KafkaSendResultHandler;
|
|
|
+import cn.com.qmth.examcloud.web.bootstrap.PropertyHolder;
|
|
|
+
|
|
|
+public class ReportsUtil {
|
|
|
+
|
|
|
+ private final static Logger logger = LoggerFactory.getLogger(ReportsUtil.class);
|
|
|
+
|
|
|
+ private static final String KEY = "report";
|
|
|
+
|
|
|
+ private static KafkaProducer<String, String> kafkaProducer;
|
|
|
+
|
|
|
+ private static String mqType = PropertyHolder.getString("$report.mq-type", "kafka");
|
|
|
+
|
|
|
+ private static Boolean reportEnable = PropertyHolder.getBoolean("$report.enable", false);
|
|
|
+
|
|
|
+ static {
|
|
|
+ if (reportEnable) {
|
|
|
+ if (MqType.KAFKA.getCode().equals(mqType)) {
|
|
|
+
|
|
|
+ Properties props = new Properties();
|
|
|
+
|
|
|
+ props.put("bootstrap.servers",
|
|
|
+ PropertyHolder.getString("$kafka-bootstrap-servers"));
|
|
|
+ props.put("key.serializer", PropertyHolder.getString("$kafka-key-serializer",
|
|
|
+ "org.apache.kafka.common.serialization.StringSerializer"));
|
|
|
+ props.put("value.serializer", PropertyHolder.getString("$kafka-value-serializer",
|
|
|
+ "org.apache.kafka.common.serialization.StringSerializer"));
|
|
|
+
|
|
|
+ kafkaProducer = new KafkaProducer<String, String>(props);
|
|
|
+
|
|
|
+ } else if (MqType.ROCKETMQ.getCode().equals(mqType)) {
|
|
|
+ // TODO
|
|
|
+ } else {
|
|
|
+ logger.error("value of property[$report.mq-type] is wrong!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void sendReportKafka(Boolean onException) {
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ List<BaseReport> list = (List<BaseReport>) ThreadLocalUtil.get(KEY);
|
|
|
+ if (CollectionUtils.isNotEmpty(list)) {
|
|
|
+ for (BaseReport b : list) {
|
|
|
+ if (!onException || (onException && b.getReportOnException())) {
|
|
|
+ try {
|
|
|
+ String messageStr = JSON.toJSONString(b);
|
|
|
+ kafkaProducer.send(new ProducerRecord<>(b.getTopic(), messageStr),
|
|
|
+ new KafkaSendResultHandler(b.getTopic(), messageStr));
|
|
|
+ } 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);
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ 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());
|
|
|
+ }
|
|
|
+
|
|
|
+}
|