浏览代码

Merge branch 'branch_other' 7

deason 5 年之前
父节点
当前提交
64c2f31176

+ 51 - 0
src/main/java/cn/com/qmth/examcloud/reports/commons/bean/BaseReport.java

@@ -0,0 +1,51 @@
+package cn.com.qmth.examcloud.reports.commons.bean;
+
+import java.util.Date;
+
+public class BaseReport {
+	protected String topic;
+	private Boolean reportOnException=false;
+	private Date reportTime;
+	private String reportHost;
+
+	public Date getReportTime() {
+		return reportTime;
+	}
+
+	public void setReportTime(Date reportTime) {
+		this.reportTime = reportTime;
+	}
+
+	public String getReportHost() {
+		return reportHost;
+	}
+
+	public void setReportHost(String reportHost) {
+		this.reportHost = reportHost;
+	}
+
+	public Boolean getReportOnException() {
+		return reportOnException;
+	}
+
+	public void setReportOnException(Boolean reportOnException) {
+		this.reportOnException = reportOnException;
+	}
+
+	
+	public String getTopic() {
+		return topic;
+	}
+
+	public BaseReport(Boolean reportOnException, Date reportTime, String reportHost) {
+		super();
+		this.reportOnException = reportOnException;
+		this.reportTime = reportTime;
+		this.reportHost = reportHost;
+	}
+
+	public BaseReport() {
+		super();
+	}
+
+}

+ 66 - 0
src/main/java/cn/com/qmth/examcloud/reports/commons/bean/OnlineExamStudentReport.java

@@ -0,0 +1,66 @@
+package cn.com.qmth.examcloud.reports.commons.bean;
+
+import java.util.Date;
+
+import cn.com.qmth.examcloud.reports.commons.enums.Topic;
+
+public class OnlineExamStudentReport extends BaseReport {
+
+	private Long rootOrgId;
+	private Long studentId;
+	private Long examId;
+	private Long examStudentId;
+
+	public Long getRootOrgId() {
+		return rootOrgId;
+	}
+
+	public void setRootOrgId(Long rootOrgId) {
+		this.rootOrgId = rootOrgId;
+	}
+
+	public Long getStudentId() {
+		return studentId;
+	}
+
+	public void setStudentId(Long studentId) {
+		this.studentId = studentId;
+	}
+
+	public Long getExamId() {
+		return examId;
+	}
+
+	public void setExamId(Long examId) {
+		this.examId = examId;
+	}
+
+	public Long getExamStudentId() {
+		return examStudentId;
+	}
+
+	public void setExamStudentId(Long examStudentId) {
+		this.examStudentId = examStudentId;
+	}
+
+	public OnlineExamStudentReport(Long rootOrgId, Long studentId,
+			Long examId, Long examStudentId) {
+		super();
+		this.rootOrgId = rootOrgId;
+		this.studentId = studentId;
+		this.examId = examId;
+		this.examStudentId = examStudentId;
+		this.topic = Topic.EXAM_STUDENT.getCode();
+	}
+
+	public OnlineExamStudentReport(Boolean reportOnException, Date reportTime, String reportHost, Long rootOrgId,
+			Long studentId, Long examId, Long examStudentId) {
+		super(reportOnException, reportTime, reportHost);
+		this.rootOrgId = rootOrgId;
+		this.studentId = studentId;
+		this.examId = examId;
+		this.examStudentId = examStudentId;
+		this.topic = Topic.EXAM_STUDENT.getCode();
+	}
+
+}

+ 43 - 0
src/main/java/cn/com/qmth/examcloud/reports/commons/bean/OnlineStudentReport.java

@@ -0,0 +1,43 @@
+package cn.com.qmth.examcloud.reports.commons.bean;
+
+import java.util.Date;
+
+import cn.com.qmth.examcloud.reports.commons.enums.Topic;
+
+public class OnlineStudentReport extends BaseReport {
+	private Long rootOrgId;
+	private Long studentId;
+
+	public Long getRootOrgId() {
+		return rootOrgId;
+	}
+
+	public void setRootOrgId(Long rootOrgId) {
+		this.rootOrgId = rootOrgId;
+	}
+
+	public Long getStudentId() {
+		return studentId;
+	}
+
+	public void setStudentId(Long studentId) {
+		this.studentId = studentId;
+	}
+
+
+	public OnlineStudentReport(Long rootOrgId, Long studentId) {
+		super();
+		this.rootOrgId = rootOrgId;
+		this.studentId = studentId;
+		this.topic = Topic.STUDENT.getCode();
+	}
+
+	public OnlineStudentReport(Boolean reportOnException, Date reportTime, String reportHost, Long rootOrgId,
+			Long studentId) {
+		super(reportOnException, reportTime, reportHost);
+		this.rootOrgId = rootOrgId;
+		this.studentId = studentId;
+		this.topic = Topic.STUDENT.getCode();
+	}
+
+}

+ 42 - 0
src/main/java/cn/com/qmth/examcloud/reports/commons/bean/OnlineUserReport.java

@@ -0,0 +1,42 @@
+package cn.com.qmth.examcloud.reports.commons.bean;
+
+import java.util.Date;
+
+import cn.com.qmth.examcloud.reports.commons.enums.Topic;
+
+public class OnlineUserReport extends BaseReport {
+	private Long rootOrgId;
+	private Long userId;
+
+	public Long getRootOrgId() {
+		return rootOrgId;
+	}
+
+	public void setRootOrgId(Long rootOrgId) {
+		this.rootOrgId = rootOrgId;
+	}
+
+	public Long getUserId() {
+		return userId;
+	}
+
+	public void setUserId(Long userId) {
+		this.userId = userId;
+	}
+
+	public OnlineUserReport(Long rootOrgId, Long userId) {
+		super();
+		this.rootOrgId = rootOrgId;
+		this.userId = userId;
+		this.topic = Topic.USER.getCode();
+	}
+
+	public OnlineUserReport(Boolean reportOnException, Date reportTime, String reportHost, Long rootOrgId,
+			Long userId) {
+		super(reportOnException, reportTime, reportHost);
+		this.rootOrgId = rootOrgId;
+		this.userId = userId;
+		this.topic = Topic.USER.getCode();
+	}
+
+}

+ 37 - 0
src/main/java/cn/com/qmth/examcloud/reports/commons/enums/MqType.java

@@ -0,0 +1,37 @@
+package cn.com.qmth.examcloud.reports.commons.enums;
+
+public enum MqType {
+	KAFKA("kafka", "kafka"),
+	ROCKETMQ("rocketmq", "rocketmq"),
+	;
+
+	// ===========================================================================
+
+	/**
+	 * 码
+	 */
+	private String code;
+
+	/**
+	 * 描述
+	 */
+	private String desc;
+
+	/**
+	 * 构造函数
+	 *
+	 * @param desc
+	 */
+	private MqType(String code, String desc) {
+		this.code = code;
+		this.desc = desc;
+	}
+
+	public String getDesc() {
+		return desc;
+	}
+
+	public String getCode() {
+		return code;
+	}
+}

+ 49 - 0
src/main/java/cn/com/qmth/examcloud/reports/commons/enums/Topic.java

@@ -0,0 +1,49 @@
+package cn.com.qmth.examcloud.reports.commons.enums;
+
+public enum Topic {
+
+	/**
+	 * 学生
+	 */
+	STUDENT("STUDENT", "学生"),
+
+	/**
+	 * 考生
+	 */
+	EXAM_STUDENT("EXAM_STUDENT", "考生"),
+
+	/**
+	 * 后台用户
+	 */
+	USER("USER", "后台用户");
+
+	// ===========================================================================
+
+	/**
+	 * 码
+	 */
+	private String code;
+
+	/**
+	 * 描述
+	 */
+	private String desc;
+
+	/**
+	 * 构造函数
+	 *
+	 * @param desc
+	 */
+	private Topic(String code, String desc) {
+		this.code = code;
+		this.desc = desc;
+	}
+
+	public String getDesc() {
+		return desc;
+	}
+
+	public String getCode() {
+		return code;
+	}
+}

+ 29 - 0
src/main/java/cn/com/qmth/examcloud/reports/commons/handler/KafkaSendResultHandler.java

@@ -0,0 +1,29 @@
+package cn.com.qmth.examcloud.reports.commons.handler;
+
+import org.apache.kafka.clients.producer.Callback;
+import org.apache.kafka.clients.producer.RecordMetadata;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class KafkaSendResultHandler implements Callback {
+	private final static Logger logger = LoggerFactory.getLogger(KafkaSendResultHandler.class);
+	private final String topic;
+    private final String message;
+    
+    
+	public KafkaSendResultHandler(String topic,String message) {
+		super();
+		this.topic = topic;
+		this.message = message;
+	}
+
+
+	@Override
+	public void onCompletion(RecordMetadata metadata, Exception exception) {
+		if(exception==null) {
+			logger.debug("kafka message send success : "+" "+topic+" "+message);
+		}else {
+			logger.error("kafka message send error :"+" "+topic+" "+message,exception);
+		}
+	}
+}

+ 120 - 0
src/main/java/cn/com/qmth/examcloud/reports/commons/util/ReportsUtil.java

@@ -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());
+	}
+
+}