|
@@ -21,47 +21,51 @@ import cn.com.qmth.examcloud.reports.commons.enums.MqType;
|
|
import cn.com.qmth.examcloud.reports.commons.handler.KafkaSendResultHandler;
|
|
import cn.com.qmth.examcloud.reports.commons.handler.KafkaSendResultHandler;
|
|
import cn.com.qmth.examcloud.web.bootstrap.PropertyHolder;
|
|
import cn.com.qmth.examcloud.web.bootstrap.PropertyHolder;
|
|
|
|
|
|
-@SuppressWarnings("unchecked")
|
|
|
|
public class ReportsUtil {
|
|
public class ReportsUtil {
|
|
|
|
+
|
|
private final static Logger logger = LoggerFactory.getLogger(ReportsUtil.class);
|
|
private final static Logger logger = LoggerFactory.getLogger(ReportsUtil.class);
|
|
|
|
|
|
- private final static String key = "report";
|
|
|
|
|
|
+ private static final String KEY = "report";
|
|
|
|
|
|
private static KafkaProducer<String, String> kafkaProducer;
|
|
private static KafkaProducer<String, String> kafkaProducer;
|
|
|
|
|
|
- private static String mqType = PropertyHolder.getString("$report.mq-type");
|
|
|
|
|
|
+ private static String mqType = PropertyHolder.getString("$report.mq-type", "rocketmq");
|
|
|
|
|
|
private static Boolean reportEnable = PropertyHolder.getBoolean("$report.enable", false);
|
|
private static Boolean reportEnable = PropertyHolder.getBoolean("$report.enable", false);
|
|
|
|
+
|
|
static {
|
|
static {
|
|
if (reportEnable) {
|
|
if (reportEnable) {
|
|
if (MqType.KAFKA.getCode().equals(mqType)) {
|
|
if (MqType.KAFKA.getCode().equals(mqType)) {
|
|
Properties props = new Properties();
|
|
Properties props = new Properties();
|
|
- props.put("bootstrap.servers", PropertyHolder.getString("$kafka-bootstrap-servers"));
|
|
|
|
|
|
+ props.put("bootstrap.servers",
|
|
|
|
+ PropertyHolder.getString("$kafka-bootstrap-servers"));
|
|
props.put("key.serializer", PropertyHolder.getString("$kafka-key-serializer"));
|
|
props.put("key.serializer", PropertyHolder.getString("$kafka-key-serializer"));
|
|
props.put("value.serializer", PropertyHolder.getString("$kafka-value-serializer"));
|
|
props.put("value.serializer", PropertyHolder.getString("$kafka-value-serializer"));
|
|
kafkaProducer = new KafkaProducer<String, String>(props);
|
|
kafkaProducer = new KafkaProducer<String, String>(props);
|
|
} else if (MqType.ROCKETMQ.getCode().equals(mqType)) {
|
|
} else if (MqType.ROCKETMQ.getCode().equals(mqType)) {
|
|
// TODO
|
|
// TODO
|
|
} else {
|
|
} else {
|
|
- logger.error("no $report.mq-type property config!");
|
|
|
|
|
|
+ logger.error("value of property[$report.mq-type] is wrong!");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
private static void sendReportKafka(Boolean onException) {
|
|
private static void sendReportKafka(Boolean onException) {
|
|
- List<BaseReport> list = (List<BaseReport>) ThreadLocalUtil.get(key);
|
|
|
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
|
+ List<BaseReport> list = (List<BaseReport>) ThreadLocalUtil.get(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())) {
|
|
try {
|
|
try {
|
|
- String messageStr=JSON.toJSONString(b);
|
|
|
|
- kafkaProducer.send(new ProducerRecord<>(b.getTopic(),messageStr), new KafkaSendResultHandler(b.getTopic(), messageStr));
|
|
|
|
|
|
+ String messageStr = JSON.toJSONString(b);
|
|
|
|
+ kafkaProducer.send(new ProducerRecord<>(b.getTopic(), messageStr),
|
|
|
|
+ new KafkaSendResultHandler(b.getTopic(), messageStr));
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
logger.error("SendReport Error:" + JSON.toJSONString(b), e);
|
|
logger.error("SendReport Error:" + JSON.toJSONString(b), e);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- ThreadLocalUtil.set(key, null);
|
|
|
|
|
|
+ ThreadLocalUtil.set(KEY, null);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -75,10 +79,11 @@ public class ReportsUtil {
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
setReportCommonData(report);
|
|
setReportCommonData(report);
|
|
- List<BaseReport> list = (List<BaseReport>) ThreadLocalUtil.get(key);
|
|
|
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
|
+ List<BaseReport> list = (List<BaseReport>) ThreadLocalUtil.get(KEY);
|
|
if (list == null) {
|
|
if (list == null) {
|
|
list = new ArrayList<BaseReport>();
|
|
list = new ArrayList<BaseReport>();
|
|
- ThreadLocalUtil.set(key, list);
|
|
|
|
|
|
+ ThreadLocalUtil.set(KEY, list);
|
|
}
|
|
}
|
|
list.add(report);
|
|
list.add(report);
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
@@ -91,8 +96,6 @@ public class ReportsUtil {
|
|
sendReportKafka(onException);
|
|
sendReportKafka(onException);
|
|
} else if (MqType.ROCKETMQ.getCode().equals(mqType)) {
|
|
} else if (MqType.ROCKETMQ.getCode().equals(mqType)) {
|
|
sendReportRocket(onException);
|
|
sendReportRocket(onException);
|
|
- } else {
|
|
|
|
- logger.error("no $report.mq-type property config!");
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -102,9 +105,10 @@ public class ReportsUtil {
|
|
InetAddress localHost = Inet4Address.getLocalHost();
|
|
InetAddress localHost = Inet4Address.getLocalHost();
|
|
ip = localHost.getHostAddress();
|
|
ip = localHost.getHostAddress();
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
- ip ="";
|
|
|
|
|
|
+ ip = "";
|
|
}
|
|
}
|
|
report.setReportHost(ip);
|
|
report.setReportHost(ip);
|
|
report.setReportTime(new Date());
|
|
report.setReportTime(new Date());
|
|
}
|
|
}
|
|
|
|
+
|
|
}
|
|
}
|