|
@@ -2,12 +2,7 @@ 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.*;
|
|
@@ -22,41 +17,25 @@ 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 Connection connection = null;
|
|
|
|
|
|
- private Properties propsRedis = new Properties();
|
|
|
+ private Statement statement = null;
|
|
|
|
|
|
+ private Properties dbProps = null;
|
|
|
|
|
|
- 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);
|
|
|
- }
|
|
|
+ private Long lastModified =0L;
|
|
|
|
|
|
|
|
|
* 加载MySQL驱动
|
|
|
*/
|
|
|
- public DatabaseMonitor() {
|
|
|
+ public DatabaseMonitor(Properties dbProps) {
|
|
|
+
|
|
|
+ this.dbProps = dbProps;
|
|
|
|
|
|
SpecialPermission.check();
|
|
|
|
|
@@ -64,12 +43,7 @@ public class DatabaseMonitor implements Runnable {
|
|
|
|
|
|
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));
|
|
|
+ Class.forName(dbProps.getProperty(JDBC_DRIVER));
|
|
|
|
|
|
} catch (ClassNotFoundException e) {
|
|
|
|
|
@@ -100,21 +74,26 @@ public class DatabaseMonitor implements Runnable {
|
|
|
*/
|
|
|
private void loadExtDictByMysql() {
|
|
|
|
|
|
- Connection conn = null;
|
|
|
- Statement stmt = null;
|
|
|
ResultSet rs = null;
|
|
|
|
|
|
try {
|
|
|
|
|
|
- Long thisSynonymVersion = getRedisSynonymVersion();
|
|
|
+ if (connection == null || statement == null) {
|
|
|
|
|
|
- conn = DriverManager.getConnection(getUrl(),getUsername(),getPassword());
|
|
|
+ connection = DriverManager.getConnection(
|
|
|
+ dbProps.getProperty(JDBC_URL),
|
|
|
+ dbProps.getProperty(JDBC_USERNAME),
|
|
|
+ dbProps.getProperty(JDBC_PASSWORD)
|
|
|
+ );
|
|
|
|
|
|
- stmt = conn.createStatement();
|
|
|
+ statement = connection.createStatement();
|
|
|
+ }
|
|
|
|
|
|
- String sql = "select *,UNIX_TIMESTAMP(update_time) as version from sys_hot_word where UNIX_TIMESTAMP(update_time) >= "+thisSynonymVersion;
|
|
|
+ logger.info("获取新词库上一次修改的时间"+lastModified);
|
|
|
|
|
|
- rs = stmt.executeQuery(sql);
|
|
|
+ String sql = "select *,UNIX_TIMESTAMP(update_time) as version from sys_hot_word where UNIX_TIMESTAMP(update_time) >= "+lastModified;
|
|
|
+
|
|
|
+ rs = statement.executeQuery(sql);
|
|
|
|
|
|
List<String> stopWordList = new ArrayList<>();
|
|
|
List<String> mainWordList = new ArrayList<>();
|
|
@@ -129,9 +108,9 @@ public class DatabaseMonitor implements Runnable {
|
|
|
String stopWord = rs.getString("stop_word");
|
|
|
Long version = rs.getLong("version");
|
|
|
|
|
|
- if(version > thisSynonymVersion){
|
|
|
+ if(version > lastModified){
|
|
|
|
|
|
- thisSynonymVersion = version;
|
|
|
+ lastModified = version;
|
|
|
|
|
|
}
|
|
|
|
|
@@ -158,7 +137,7 @@ public class DatabaseMonitor implements Runnable {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- setRedisSynonymVersion(thisSynonymVersion);
|
|
|
+ logger.info("获取新词库下一次修改的时间"+lastModified);
|
|
|
|
|
|
if(stopWordList.size() > 0){
|
|
|
|
|
@@ -185,6 +164,7 @@ public class DatabaseMonitor implements Runnable {
|
|
|
logger.error("从mysql加载热更新词典失败,错误信息为:", e);
|
|
|
|
|
|
} finally {
|
|
|
+
|
|
|
if (rs != null) {
|
|
|
try {
|
|
|
rs.close();
|
|
@@ -192,103 +172,6 @@ public class DatabaseMonitor implements Runnable {
|
|
|
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();
|
|
|
- }
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
}
|