|
@@ -0,0 +1,294 @@
|
|
|
+package org.wltea.analyzer.dic;
|
|
|
+
|
|
|
+import org.apache.logging.log4j.Logger;
|
|
|
+import org.elasticsearch.SpecialPermission;
|
|
|
+import org.redisson.api.RBucket;
|
|
|
+import org.redisson.api.RedissonClient;
|
|
|
+import org.wltea.analyzer.help.ESPluginLoggerFactory;
|
|
|
+import org.wltea.analyzer.util.RedisUtils;
|
|
|
+
|
|
|
+import java.net.InetAddress;
|
|
|
+import java.security.AccessController;
|
|
|
+import java.security.PrivilegedAction;
|
|
|
+import java.sql.*;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Properties;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 建立数据库监控线程
|
|
|
+ */
|
|
|
+public class DatabaseMonitor implements Runnable {
|
|
|
+
|
|
|
+ private static final Logger logger = ESPluginLoggerFactory.getLogger(DatabaseMonitor.class.getName());
|
|
|
+
|
|
|
+ public static final String PATH_JDBC_YML = "jdbc.yml";
|
|
|
+
|
|
|
+ private static final String JDBC_URL = "jdbc.url";
|
|
|
+ private static final String JDBC_USERNAME = "jdbc.username";
|
|
|
+ private static final String JDBC_PASSWORD = "jdbc.password";
|
|
|
+ private static final String JDBC_DRIVER = "jdbc.driver";
|
|
|
+
|
|
|
+ private static final String REDIS_HOST = "redis.host";
|
|
|
+ private static final String REDIS_PORT = "redis.port";
|
|
|
+ private static final String REDIS_TIMEOUT = "redis.timeout";
|
|
|
+ private static final String REDIS_PASSWORD = "redis.password";
|
|
|
+
|
|
|
+ private Properties propsRedis = new Properties();
|
|
|
+
|
|
|
+
|
|
|
+ public String getUrl() {
|
|
|
+ return Dictionary.getSingleton().getProperty(JDBC_URL);
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getUsername() {
|
|
|
+ return Dictionary.getSingleton().getProperty(JDBC_USERNAME);
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getPassword() {
|
|
|
+ return Dictionary.getSingleton().getProperty(JDBC_PASSWORD);
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getDriver() {
|
|
|
+ return Dictionary.getSingleton().getProperty(JDBC_DRIVER);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 加载MySQL驱动
|
|
|
+ */
|
|
|
+ public DatabaseMonitor() {
|
|
|
+
|
|
|
+ SpecialPermission.check();
|
|
|
+
|
|
|
+ AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
|
|
|
+
|
|
|
+ try {
|
|
|
+
|
|
|
+ Class.forName(getDriver());
|
|
|
+
|
|
|
+ propsRedis.put(REDIS_HOST, Dictionary.getSingleton().getProperty(REDIS_HOST));
|
|
|
+ propsRedis.put(REDIS_PORT, Dictionary.getSingleton().getProperty(REDIS_PORT));
|
|
|
+ propsRedis.put(REDIS_TIMEOUT, Dictionary.getSingleton().getProperty(REDIS_TIMEOUT));
|
|
|
+ propsRedis.put(REDIS_PASSWORD, Dictionary.getSingleton().getProperty(REDIS_PASSWORD));
|
|
|
+
|
|
|
+ } catch (ClassNotFoundException e) {
|
|
|
+
|
|
|
+ logger.error("mysql jdbc driver not found", e);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ });
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+
|
|
|
+ SpecialPermission.check();
|
|
|
+ AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
|
|
|
+
|
|
|
+ //从mysql加载热更新词典
|
|
|
+ loadExtDictByMysql();
|
|
|
+
|
|
|
+ return null;
|
|
|
+ });
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从mysql加载热更新词典
|
|
|
+ */
|
|
|
+ private void loadExtDictByMysql() {
|
|
|
+
|
|
|
+ Connection conn = null;
|
|
|
+ Statement stmt = null;
|
|
|
+ ResultSet rs = null;
|
|
|
+
|
|
|
+ try {
|
|
|
+
|
|
|
+ Long thisSynonymVersion = getRedisSynonymVersion();
|
|
|
+
|
|
|
+ conn = DriverManager.getConnection(getUrl(),getUsername(),getPassword());
|
|
|
+
|
|
|
+ stmt = conn.createStatement();
|
|
|
+
|
|
|
+ String sql = "select *,UNIX_TIMESTAMP(update_time) as version from sys_hot_word where UNIX_TIMESTAMP(update_time) >= "+thisSynonymVersion;
|
|
|
+
|
|
|
+ rs = stmt.executeQuery(sql);
|
|
|
+
|
|
|
+ List<String> stopWordList = new ArrayList<>();
|
|
|
+ List<String> mainWordList = new ArrayList<>();
|
|
|
+
|
|
|
+ List<String> disStopWordList = new ArrayList<>();
|
|
|
+ List<String> disMainWordList = new ArrayList<>();
|
|
|
+
|
|
|
+ while (rs.next()) {
|
|
|
+
|
|
|
+ String theWord = rs.getString("word");
|
|
|
+ String status = rs.getString("status");
|
|
|
+ String stopWord = rs.getString("stop_word");
|
|
|
+ Long version = rs.getLong("version");
|
|
|
+
|
|
|
+ if(version > thisSynonymVersion){
|
|
|
+
|
|
|
+ thisSynonymVersion = version;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ if("1".equals(status)){
|
|
|
+
|
|
|
+ if("1".equals(stopWord)){
|
|
|
+
|
|
|
+ stopWordList.add(theWord);
|
|
|
+
|
|
|
+ }else{
|
|
|
+ mainWordList.add(theWord);
|
|
|
+ }
|
|
|
+
|
|
|
+ }else{
|
|
|
+
|
|
|
+ if("1".equals(stopWord)){
|
|
|
+
|
|
|
+ disStopWordList.add(theWord);
|
|
|
+
|
|
|
+ }else{
|
|
|
+ disMainWordList.add(theWord);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ setRedisSynonymVersion(thisSynonymVersion);
|
|
|
+
|
|
|
+ if(stopWordList.size() > 0){
|
|
|
+
|
|
|
+ Dictionary.getSingleton().addStopWords(stopWordList);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ if(mainWordList.size() > 0){
|
|
|
+ Dictionary.getSingleton().addMainWords(mainWordList);
|
|
|
+ }
|
|
|
+
|
|
|
+ if(disStopWordList.size() > 0){
|
|
|
+ Dictionary.getSingleton().disableStopWords(disStopWordList);
|
|
|
+ }
|
|
|
+
|
|
|
+ if(disMainWordList.size() > 0){
|
|
|
+ Dictionary.getSingleton().disableMainWords(disMainWordList);
|
|
|
+ }
|
|
|
+
|
|
|
+ logger.info("从mysql加载热更新词典成功!");
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+
|
|
|
+ logger.error("从mysql加载热更新词典失败,错误信息为:", e);
|
|
|
+
|
|
|
+ } finally {
|
|
|
+ if (rs != null) {
|
|
|
+ try {
|
|
|
+ rs.close();
|
|
|
+ } catch (SQLException e) {
|
|
|
+ logger.error("error", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (stmt != null) {
|
|
|
+ try {
|
|
|
+ stmt.close();
|
|
|
+ } catch (SQLException e) {
|
|
|
+ logger.error("error", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (conn != null) {
|
|
|
+ try {
|
|
|
+ conn.close();
|
|
|
+ } catch (SQLException e) {
|
|
|
+ logger.error("error", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取redis中同义词版本号信息
|
|
|
+ * 用于判断同义词是否需要进行重新加载
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public Long getRedisSynonymVersion() {
|
|
|
+
|
|
|
+ Long thisSynonymVersion = 0L;
|
|
|
+
|
|
|
+ RedissonClient redissonClient = RedisUtils.getRedissonClient(propsRedis);
|
|
|
+
|
|
|
+ try{
|
|
|
+
|
|
|
+ String ip = InetAddress.getLocalHost().getHostAddress();
|
|
|
+
|
|
|
+ RBucket<String> ipRBucket = redissonClient.getBucket("es_version_"+ip);
|
|
|
+
|
|
|
+ if(ipRBucket.isExists()){
|
|
|
+
|
|
|
+ String version = ipRBucket.get();
|
|
|
+
|
|
|
+ if(version !=null && !"".equals(version)){
|
|
|
+
|
|
|
+ thisSynonymVersion = Long.parseLong(version);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }catch (Exception exception){
|
|
|
+
|
|
|
+ exception.printStackTrace();
|
|
|
+
|
|
|
+ logger.error("获取redis中同义词版本号信息失败!"+exception);
|
|
|
+
|
|
|
+ }finally {
|
|
|
+
|
|
|
+ if(redissonClient !=null && !redissonClient.isShutdown()){
|
|
|
+
|
|
|
+ redissonClient.shutdown();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return thisSynonymVersion;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置redis中同义词版本号信息
|
|
|
+ * 用于判断同义词是否需要进行重新加载
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public void setRedisSynonymVersion(Long setSynonymVersion) {
|
|
|
+
|
|
|
+
|
|
|
+ RedissonClient redissonClient = RedisUtils.getRedissonClient(propsRedis);
|
|
|
+
|
|
|
+ try{
|
|
|
+
|
|
|
+ String ip = InetAddress.getLocalHost().getHostAddress();
|
|
|
+
|
|
|
+ RBucket<String> ipRBucket = redissonClient.getBucket("es_version_"+ip);
|
|
|
+
|
|
|
+ ipRBucket.set(String.valueOf(setSynonymVersion));
|
|
|
+
|
|
|
+ }catch (Exception exception){
|
|
|
+
|
|
|
+ exception.printStackTrace();
|
|
|
+
|
|
|
+ logger.error("设置redis中同义词版本号信息失败!"+exception);
|
|
|
+
|
|
|
+ }finally {
|
|
|
+
|
|
|
+ if(redissonClient !=null && !redissonClient.isShutdown()){
|
|
|
+
|
|
|
+ redissonClient.shutdown();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+}
|