|
@@ -12,6 +12,8 @@ import org.springframework.stereotype.Component;
|
|
|
|
|
|
import com.google.common.collect.Maps;
|
|
|
|
|
|
+import cn.com.qmth.examcloud.commons.logging.ExamCloudLog;
|
|
|
+import cn.com.qmth.examcloud.commons.logging.ExamCloudLogFactory;
|
|
|
import cn.com.qmth.examcloud.commons.util.DateUtil;
|
|
|
import cn.com.qmth.examcloud.web.bootstrap.PropertyHolder;
|
|
|
import cn.com.qmth.examcloud.web.support.SpringContextHolder;
|
|
@@ -27,6 +29,8 @@ import cn.com.qmth.examcloud.web.support.SpringContextHolder;
|
|
|
@Order(99)
|
|
|
public class MongodbDetector implements ApplicationRunner {
|
|
|
|
|
|
+ private static final ExamCloudLog LOG = ExamCloudLogFactory.getLog(MongodbDetector.class);
|
|
|
+
|
|
|
public void start() {
|
|
|
String appName = PropertyHolder.getString("spring.application.name");
|
|
|
|
|
@@ -39,10 +43,24 @@ public class MongodbDetector implements ApplicationRunner {
|
|
|
}
|
|
|
|
|
|
Class<?> mongoTemplateClass = null;
|
|
|
+ Object mongoTemplateObject = null;
|
|
|
+
|
|
|
try {
|
|
|
mongoTemplateClass = Class
|
|
|
.forName("org.springframework.data.mongodb.core.MongoTemplate");
|
|
|
- Object mongoTemplateObject = SpringContextHolder.getBean(mongoTemplateClass);
|
|
|
+ mongoTemplateObject = SpringContextHolder.getBean(mongoTemplateClass);
|
|
|
+ } catch (Exception e) {
|
|
|
+ // ingnore
|
|
|
+ LOG.info("mongodb useless");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ Method getDbMethod = mongoTemplateClass.getClass().getMethod("getDb");
|
|
|
+ Object mongoDatabaseObject = getDbMethod.invoke(getDbMethod);
|
|
|
+ Method getNameMethod = mongoDatabaseObject.getClass().getMethod("getName");
|
|
|
+ Object dbName = getNameMethod.invoke(mongoDatabaseObject);
|
|
|
+ LOG.info("mongo.db=" + dbName);
|
|
|
|
|
|
Map<String, String> data = Maps.newHashMap();
|
|
|
data.put("startupTime", DateUtil.chinaNow());
|
|
@@ -53,8 +71,7 @@ public class MongodbDetector implements ApplicationRunner {
|
|
|
String.class);
|
|
|
insertMethod.invoke(mongoTemplateObject, data, "startup");
|
|
|
} catch (Exception e) {
|
|
|
- // ingnore
|
|
|
- return;
|
|
|
+ LOG.error("unexpected mongodb exception", e);
|
|
|
}
|
|
|
|
|
|
}
|